Skip to content

Commit 50ead0f

Browse files
committed
log fn
1 parent 16c8b7f commit 50ead0f

File tree

4 files changed

+139
-15
lines changed

4 files changed

+139
-15
lines changed

index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11

22

33
Object.assign(module.exports, {
4+
node: require('./src/node'),
45
log: require('./src/log')
56
});

package.json

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,25 @@
11
{
22
"name": "@rightech/utils",
3-
"version": "0.1.0",
3+
"version": "0.1.1",
44
"description": "",
55
"main": "index.js",
6+
"private": false,
67
"scripts": {
78
"test": "echo \"Error: no test specified\" && exit 1"
89
},
910
"repository": {
1011
"type": "git",
1112
"url": "git+https://github.com/Rightech/node-utils.git"
1213
},
13-
"author": "",
14+
"author": "[email protected]",
1415
"license": "Apache-2.0",
1516
"bugs": {
1617
"url": "https://github.com/Rightech/node-utils/issues"
1718
},
1819
"homepage": "https://github.com/Rightech/node-utils#readme",
1920
"dependencies": {
20-
"colors": "^1.3.3"
21+
"colors": "^1.3.3",
22+
"deepmerge": "^4.0.0",
23+
"stack-trace": "0.0.10"
2124
}
2225
}

src/log.js

Lines changed: 90 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
const colors = require('colors/safe');
2+
const merge = require('deepmerge');
3+
const { inspect } = require('util');
4+
5+
const { caller } = require('./node');
26

37
const LEVELS = Object.freeze({
4-
NONE: 0,
5-
ERROR: 1,
6-
WARN: 2,
7-
INFO: 3,
8-
DEBUG: 4
8+
none: 0,
9+
error: 1,
10+
warn: 2,
11+
info: 3,
12+
debug: 4
913
});
1014

1115
const COLORS = Object.freeze({
@@ -16,24 +20,98 @@ const COLORS = Object.freeze({
1620
});
1721

1822
const FORMAT = Object.freeze({
19-
ascii: 'ascii',
23+
term: 'term',
2024
text: 'text',
2125
json: 'json'
2226
});
2327

28+
const TIMEOF = Object.freeze({
29+
iso: 'iso',
30+
ru: 'ru',
31+
en: 'en'
32+
});
33+
2434
function getDefaultOptions() {
2535
return {
26-
level: LEVELS.INFO,
36+
level: LEVELS.info,
37+
time : TIMEOF.iso,
2738
width: {
28-
category: 40
39+
object: 40
2940
}
3041
};
3142
}
3243

44+
let options = getDefaultOptions();
45+
46+
function setOptions(opts = {}) {
47+
options = getOptions(opts);
48+
}
3349

34-
module.exports = {
35-
LEVELS,
36-
COLORS,
37-
FORMAT
50+
function getOptions(opts = {}) {
51+
return merge.all([getDefaultOptions(), options || {}, opts]);
52+
}
3853

54+
function summary(message) {
55+
if (!message) {
56+
return '[undefined]';
57+
}
58+
if (message instanceof Error) {
59+
const joiner = colors.red('* ');
60+
return message.stack.split(' at').join(joiner);
61+
}
62+
return inspect(message);
3963
}
64+
65+
function log(message = '?', object = caller(), level = LEVELS.info) {
66+
const opts = getOptions();
67+
68+
if (level > opts.level || level === LEVELS.NONE) {
69+
return;
70+
}
71+
if (message && message._silent) {
72+
return;
73+
}
74+
const color = COLORS[level];
75+
let date = new Date().toISOString(); //utils.formatDate(new Date());
76+
77+
let line = '';
78+
79+
if (typeof object === 'object' && object.module) {
80+
object = object.module;
81+
}
82+
83+
if (object) {
84+
if (!isNaN(+object)) {
85+
object = `[${object}]`;
86+
}
87+
object = (object || '').toString().substring(0, opts.width.object);
88+
object = `${object.padEnd(opts.width.object, ' ')} : `;
89+
line += colors.bold(object);
90+
}
91+
92+
if (typeof message === 'object') {
93+
message = summary(message);
94+
} else {
95+
message = message + '';
96+
}
97+
if (color) {
98+
date = colors[color](date);
99+
line = colors[color](line);
100+
}
101+
console.log(`${date} ${line} ${message}`);
102+
}
103+
104+
log.LEVELS = LEVELS;
105+
log.COLORS = COLORS;
106+
log.FORMAT = FORMAT;
107+
108+
log.setOptions = setOptions;
109+
log.getOptions = getOptions;
110+
111+
log.debug = (message, object) => log(message, object || caller(), LEVELS.debug);
112+
log.info = (message, object) => log(message, object || caller(), LEVELS.info);
113+
log.warn = (message, object) => log(message, object || caller(), LEVELS.warn);
114+
log.error = (message, object) => log(message, object || caller(), LEVELS.error);
115+
116+
117+
module.exports = log;

src/node.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
2+
function isMongoId(id) {
3+
return id && id._bsontype && id._bsontype === 'ObjectID';
4+
}
5+
6+
function callers() {
7+
if (!require.main) {
8+
return [];
9+
}
10+
11+
const rootPath = path.dirname(require.main.filename);
12+
const modulesPath = path.join(rootPath, '..', 'modules');
13+
14+
return stackTrace.get()
15+
.filter(entry => {
16+
const name = entry.getFileName();
17+
return name && name.includes(modulesPath);
18+
})
19+
.map(origin => {
20+
const filename = origin.getFileName();
21+
const parts = filename.split(path.sep);
22+
const name = parts[parts.indexOf('modules') + 1];
23+
return {
24+
name,
25+
line: origin.getLineNumber(),
26+
file: path.basename(filename),
27+
module: `system [${name || 'unknown'}]`
28+
};
29+
});
30+
}
31+
32+
function caller(skip, name) {
33+
skip = skip || 0;
34+
name = name || 'system';
35+
return callers()[skip] || { name, module: name };
36+
}
37+
38+
module.exports = {
39+
isMongoId,
40+
callers,
41+
caller,
42+
}

0 commit comments

Comments
 (0)