hermodlog 1.2.0 → 1.4.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +98 -13
- package/package.json +1 -1
- package/src/LOG_COLORS.js +13 -0
- package/src/LOG_LEVELS.js +1 -0
- package/src/Logger.js +36 -8
- package/src/Logger.spec.js +27 -2
- package/src/methods/child.js +6 -0
- package/src/methods/context.js +0 -4
- package/src/methods/debug.js +2 -60
- package/src/methods/error.js +3 -59
- package/src/methods/fatal.js +8 -0
- package/src/methods/info.js +2 -60
- package/src/methods/listener.js +0 -2
- package/src/methods/log.js +5 -3
- package/src/methods/method.js +0 -3
- package/src/methods/module.js +1 -5
- package/src/methods/object.js +8 -0
- package/src/methods/trace.js +2 -58
- package/src/methods/warn.js +2 -59
- package/src/utils/log.js +72 -0
- package/usage.example.js +6 -1
package/README.md
CHANGED
@@ -6,7 +6,7 @@ Stupid simple logging for JS but with context for heavy log environments.
|
|
6
6
|
|
7
7
|
This is just to only pass along a single logger instance between modules and have a hierarchical way to sort and display such modules.
|
8
8
|
|
9
|
-
<img width="
|
9
|
+
<img width="1065" alt="image" src="https://github.com/Alex-Werner/hermodlog/assets/5849920/1df7e7dd-cf25-4ce3-872b-dab1288966e4">
|
10
10
|
|
11
11
|
|
12
12
|
## Install
|
@@ -20,13 +20,14 @@ npm i hermodlog
|
|
20
20
|
```js
|
21
21
|
import Logger from 'hermodlog';
|
22
22
|
|
23
|
-
logger = new Logger(
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
23
|
+
const logger = new Logger({
|
24
|
+
level: 'debug', // Optional, default: 'info'
|
25
|
+
});
|
26
|
+
const contextLogger = logger.context('Node#42');
|
27
|
+
const moduleLogger = logger.module('WSServer');
|
28
|
+
moduleLogger.method('init').log("Initializing server...");
|
29
|
+
const listenerLogger = moduleLogger.listener('onMessage');
|
30
|
+
listenerLogger.debug("Message received!");
|
30
31
|
```
|
31
32
|
|
32
33
|
## API
|
@@ -36,11 +37,7 @@ moduleLogger = contextLogger.module('myModule');
|
|
36
37
|
#### `new Logger(options)`
|
37
38
|
Create a new logger instance.
|
38
39
|
|
39
|
-
|
40
|
-
Create a new context logger with the given name.
|
41
|
-
|
42
|
-
##### `module(name)`
|
43
|
-
Create a new module logger with the given name.
|
40
|
+
#### Level methods
|
44
41
|
|
45
42
|
##### `log(message, level)`
|
46
43
|
Log a message with the given level.
|
@@ -54,12 +51,100 @@ Log a message with the info level.
|
|
54
51
|
##### `warn(message)`
|
55
52
|
Log a message with the warn level.
|
56
53
|
|
54
|
+
##### `fatal(message)`
|
55
|
+
Log a message with the fatal level.
|
56
|
+
|
57
57
|
##### `error(message)`
|
58
58
|
Log a message with the error level.
|
59
59
|
|
60
60
|
##### `fatal(message)`
|
61
61
|
Log a message with the fatal level.
|
62
62
|
|
63
|
+
#### Context methods
|
64
|
+
|
65
|
+
##### `context(name)`
|
66
|
+
Create a new context logger with the given context name.
|
67
|
+
|
68
|
+
##### `module(name)`
|
69
|
+
Create a new module logger with the given module name.
|
70
|
+
|
71
|
+
##### `listener(name)`
|
72
|
+
Create a new listener sublogger with the given listener name.
|
73
|
+
|
74
|
+
##### `method(name)`
|
75
|
+
Create a new method sublogger with the given method name.
|
76
|
+
|
77
|
+
##### `child(name)`
|
78
|
+
Create a new child logger with the given child name.
|
79
|
+
|
80
|
+
##### `object(object)`
|
81
|
+
Create a new object logger with the given object.
|
82
|
+
|
83
|
+
#### Other methods
|
84
|
+
|
85
|
+
#### `clone(opts)`
|
86
|
+
Clone the logger. The `opts` object can have the following properties:
|
87
|
+
- keepHistory: Whether to keep the history of the logger. Default: `false` (unique falsy one).
|
88
|
+
- keepContext: Whether to keep the context of the logger. Default: `true`.
|
89
|
+
- keepModule: Whether to keep the module of the logger. Default: `true`.
|
90
|
+
- keepListener: Whether to keep the listener of the logger. Default: `true`.
|
91
|
+
- keepMethod: Whether to keep the method of the logger. Default: `true`.
|
92
|
+
- keepObject: Whether to keep the object of the logger. Default: `true`.
|
93
|
+
- keepChild: Whether to keep the child of the logger. Default: `true`.
|
94
|
+
- keepLevel: Whether to keep the level of the logger. Default: `true`.
|
95
|
+
- keepColors: Whether to keep the colors of the logger. Default: `true`.
|
96
|
+
- keepDate: Whether to keep the date of the logger. Default: `true`.
|
97
|
+
|
98
|
+
#### constructor options
|
99
|
+
The `options` object can have the following properties:
|
100
|
+
|
101
|
+
##### `date`
|
102
|
+
A date-compatible format. Default: `YYYY-MM-DD HH:mm:ss.SSS`.
|
103
|
+
|
104
|
+
##### `level`
|
105
|
+
A default log level. Can be one of 'fatal', 'error', 'warn', 'info', 'debug', 'trace'. Default: `info`.
|
106
|
+
|
107
|
+
##### `history`
|
108
|
+
The history of the logger. Default: `[]`.
|
109
|
+
|
110
|
+
##### `contextName`
|
111
|
+
The context name of the logger. Default: `null`.
|
112
|
+
|
113
|
+
##### `moduleName`
|
114
|
+
The module name of the logger. Default: `null`.
|
115
|
+
|
116
|
+
##### `listenerName`
|
117
|
+
The listener name of the logger. Default: `null`.
|
118
|
+
|
119
|
+
##### `methodName`
|
120
|
+
The method name of the logger. Default: `null`.
|
121
|
+
|
122
|
+
##### `objectName`
|
123
|
+
The object name of the logger. Default: `null`.
|
124
|
+
|
125
|
+
##### `colors`
|
126
|
+
An object where each key is a log level and the value is an array of colors for different parts of the log message. The array should have the following order: [date color, type color, context color, module color, listener color, method color, object color, string color, function color, number/default color].
|
127
|
+
|
128
|
+
```js
|
129
|
+
{
|
130
|
+
const options = {
|
131
|
+
date: new Date('2024-01-01T00:01:01.001Z'),
|
132
|
+
level: 'info', // Set the default log level
|
133
|
+
colors: {
|
134
|
+
// Set the colors for different parts of the log message
|
135
|
+
fatal: [chalk.gray, chalk.red, chalk.blueBright, chalk.gray, chalk.cyan, chalk.yellow, chalk.magenta, chalk.white, chalk.blue, chalk.cyan],
|
136
|
+
error: [chalk.gray, chalk.red, chalk.blueBright, chalk.gray, chalk.cyan, chalk.yellow, chalk.red, chalk.yellow, chalk.green, chalk.blue],
|
137
|
+
warn: [chalk.gray, chalk.red, chalk.blueBright, chalk.gray, chalk.cyan, chalk.yellow, chalk.yellow, chalk.blue, chalk.magenta, chalk.red],
|
138
|
+
info: [chalk.gray, chalk.red, chalk.blueBright, chalk.gray, chalk.cyan, chalk.yellow, chalk.blue, chalk.green, chalk.red, chalk.yellow],
|
139
|
+
debug: [chalk.gray, chalk.red, chalk.blueBright, chalk.gray, chalk.cyan, chalk.yellow, chalk.green, chalk.red, chalk.yellow, chalk.blue],
|
140
|
+
trace: [chalk.gray, chalk.red, chalk.blueBright, chalk.gray, chalk.cyan, chalk.yellow, chalk.magenta, chalk.yellow, chalk.blue, chalk.green],
|
141
|
+
}
|
142
|
+
};
|
143
|
+
const logger = new Logger(options);
|
144
|
+
|
145
|
+
logger.log('Hello World');
|
146
|
+
}
|
147
|
+
```
|
63
148
|
|
64
149
|
### License
|
65
150
|
|
package/package.json
CHANGED
@@ -0,0 +1,13 @@
|
|
1
|
+
import chalk from "chalk";
|
2
|
+
|
3
|
+
const LOG_COLORS = {
|
4
|
+
// [date color, type color, context color, module color, listener color, method color, object color, string color, function color, number/default color]
|
5
|
+
debug: [chalk.gray, chalk.blue, chalk.blueBright, chalk.gray, chalk.cyan, chalk.yellow, chalk.blue, chalk.blue, chalk.blue, chalk.blue],
|
6
|
+
error: [chalk.gray, chalk.red, chalk.blueBright, chalk.gray, chalk.cyan, chalk.yellow, chalk.red, chalk.red, chalk.red, chalk.red],
|
7
|
+
fatal: [chalk.gray, chalk.magenta, chalk.blueBright, chalk.gray, chalk.cyan, chalk.yellow, chalk.magenta, chalk.magenta, chalk.magenta, chalk.magenta],
|
8
|
+
info: [chalk.gray, chalk.green, chalk.blueBright, chalk.gray, chalk.cyan, chalk.yellow, chalk.green, chalk.green, chalk.green, chalk.green],
|
9
|
+
trace: [chalk.gray, chalk.gray, chalk.blueBright, chalk.gray, chalk.cyan, chalk.yellow, chalk.gray, chalk.gray, chalk.gray, chalk.gray],
|
10
|
+
warn: [chalk.gray, chalk.yellow, chalk.blueBright, chalk.gray, chalk.cyan, chalk.yellow, chalk.yellow, chalk.yellow, chalk.yellow, chalk.yellow],
|
11
|
+
}
|
12
|
+
|
13
|
+
export default LOG_COLORS;
|
package/src/LOG_LEVELS.js
CHANGED
package/src/Logger.js
CHANGED
@@ -10,7 +10,11 @@ import method from "./methods/method.js";
|
|
10
10
|
import module from "./methods/module.js";
|
11
11
|
import trace from "./methods/trace.js";
|
12
12
|
import warn from "./methods/warn.js";
|
13
|
+
import fatal from "./methods/fatal.js";
|
14
|
+
import child from "./methods/child.js";
|
13
15
|
|
16
|
+
import LOG_COLORS from "./LOG_COLORS.js";
|
17
|
+
import object from "./methods/object.js";
|
14
18
|
class Logger {
|
15
19
|
constructor(props = {}) {
|
16
20
|
this.level = props.level ?? 'info';
|
@@ -18,25 +22,46 @@ class Logger {
|
|
18
22
|
throw new Error(`Unknown log level ${this.level}`)
|
19
23
|
}
|
20
24
|
|
21
|
-
this.history = [];
|
25
|
+
this.history = props.history ?? [];
|
26
|
+
|
27
|
+
this.contextName = props.contextName ?? null;
|
28
|
+
this.methodName = props.methodName ?? null;
|
29
|
+
this.moduleName = props.moduleName ?? null;
|
30
|
+
this.listenerName = props.listenerName ?? null;
|
31
|
+
this.objectName = props.objectName ?? null;
|
22
32
|
|
23
|
-
this.contextName = null
|
24
|
-
this.methodName = null
|
25
|
-
this.moduleName = null
|
26
|
-
this.listenerName = null
|
27
33
|
if (props.date) {
|
28
34
|
this.date = props.date;
|
29
35
|
}
|
36
|
+
|
37
|
+
this.LOG_COLORS = (props.colors) ?? LOG_COLORS;
|
30
38
|
}
|
31
39
|
|
32
40
|
_log(message) {
|
33
41
|
console.log(message)
|
34
42
|
}
|
35
43
|
|
36
|
-
clone() {
|
44
|
+
clone(opts = {}) {
|
45
|
+
const keepHistory = opts?.keepHistory ?? false;
|
46
|
+
const keepLevel = opts?.keepLevel ?? true;
|
47
|
+
const keepDate = opts?.keepDate ?? true;
|
48
|
+
const keepColors = opts?.keepColors ?? true;
|
49
|
+
const keepContext = opts?.keepContext ?? true;
|
50
|
+
const keepMethod = opts?.keepMethod ?? true;
|
51
|
+
const keepModule = opts?.keepModule ?? true;
|
52
|
+
const keepListener = opts?.keepListener ?? true;
|
53
|
+
const keepObject = opts?.keepObject ?? true;
|
54
|
+
|
37
55
|
return new Logger({
|
38
|
-
level: this.level,
|
39
|
-
date: this.date,
|
56
|
+
level: (keepLevel) ? this.level : opts.level ?? this.level,
|
57
|
+
date: (keepDate) ? this.date : opts.date ?? null,
|
58
|
+
history: keepHistory ? this.history : [],
|
59
|
+
colors: (keepColors) ? this.LOG_COLORS : opts.colors ?? this.LOG_COLORS,
|
60
|
+
contextName: (keepContext) ? this.contextName : opts.contextName ?? null,
|
61
|
+
methodName: (keepMethod) ? this.methodName : opts.methodName ?? null,
|
62
|
+
moduleName: (keepModule) ? this.moduleName : opts.moduleName ?? null,
|
63
|
+
listenerName: (keepListener) ? this.listenerName : opts.listenerName ?? null,
|
64
|
+
objectName: (keepObject) ? this.objectName : opts.objectName ?? null,
|
40
65
|
})
|
41
66
|
}
|
42
67
|
|
@@ -45,12 +70,15 @@ class Logger {
|
|
45
70
|
Logger.prototype.context = context;
|
46
71
|
Logger.prototype.debug = debug;
|
47
72
|
Logger.prototype.error = error;
|
73
|
+
Logger.prototype.fatal = fatal;
|
48
74
|
Logger.prototype.info = info;
|
49
75
|
Logger.prototype.listener = listener;
|
50
76
|
Logger.prototype.log = log;
|
51
77
|
Logger.prototype.method = method;
|
78
|
+
Logger.prototype.object = object;
|
52
79
|
Logger.prototype.module = module;
|
53
80
|
Logger.prototype.trace = trace;
|
54
81
|
Logger.prototype.warn = warn;
|
82
|
+
Logger.prototype.child = child;
|
55
83
|
|
56
84
|
export default Logger;
|
package/src/Logger.spec.js
CHANGED
@@ -5,7 +5,7 @@ describe('Logger', () => {
|
|
5
5
|
let silentLogger
|
6
6
|
let silentModuleLogger
|
7
7
|
it('should create an logger', () => {
|
8
|
-
silentLogger = new Logger(new Date('2023-07-29T01:38:00.482Z'));
|
8
|
+
silentLogger = new Logger({date:new Date('2023-07-29T01:38:00.482Z')});
|
9
9
|
silentLogger._log = () => {}
|
10
10
|
assert.equal(silentLogger.history.length, 0)
|
11
11
|
silentLogger.log('Hello');
|
@@ -28,7 +28,7 @@ describe('Logger', () => {
|
|
28
28
|
const silentListenerLogger = silentModuleLogger.listener('onTest')
|
29
29
|
silentListenerLogger.log('Hello');
|
30
30
|
assert.equal(silentListenerLogger.history.length, 1)
|
31
|
-
assert.equal(silentListenerLogger.history[0], "[\u001b[90m2023-07-29T01:38:00.482Z\u001b[39m] module:\u001b[90mtest-module\u001b[39m | listener: \u001b[
|
31
|
+
assert.equal(silentListenerLogger.history[0], "[\u001b[90m2023-07-29T01:38:00.482Z\u001b[39m] module:\u001b[90mtest-module\u001b[39m | listener: \u001b[36monTest\u001b[39m \u001b[32mHello\u001b[39m")
|
32
32
|
})
|
33
33
|
it('should create an logger with method', () => {
|
34
34
|
const silentMethodLogger = silentModuleLogger.method('test-method')
|
@@ -39,6 +39,7 @@ describe('Logger', () => {
|
|
39
39
|
})
|
40
40
|
it('should handle level', function () {
|
41
41
|
const levelLogger = new Logger({level: 'error'})
|
42
|
+
levelLogger._log = () => {}
|
42
43
|
levelLogger.error('Hello');
|
43
44
|
assert.equal(levelLogger.history.length, 1)
|
44
45
|
levelLogger.warn('Hello');
|
@@ -48,4 +49,28 @@ describe('Logger', () => {
|
|
48
49
|
levelLogger.trace('Hello');
|
49
50
|
assert.equal(levelLogger.history.length, 1)
|
50
51
|
});
|
52
|
+
|
53
|
+
it('should automatically beautify instead of [Object Object]', () => {
|
54
|
+
const object = {
|
55
|
+
x: 42,
|
56
|
+
y: {
|
57
|
+
b: [1, 2, 3]
|
58
|
+
},
|
59
|
+
req: 'getCar'
|
60
|
+
}
|
61
|
+
silentModuleLogger.log('Received from ws client', object)
|
62
|
+
assert.equal(silentModuleLogger.history.length, 2)
|
63
|
+
// If you change the styling of below it will assume spaces are part of the string
|
64
|
+
assert.equal(silentModuleLogger.history[1], `[\u001b[90m2023-07-29T01:38:00.482Z\u001b[39m] module:\u001b[90mtest-module\u001b[39m \u001b[32mReceived from ws client\u001b[39m \u001b[32mObject(\u001b[39m\u001b[32m{\u001b[39m
|
65
|
+
\u001b[32m "x": 42,\u001b[39m
|
66
|
+
\u001b[32m "y": {\u001b[39m
|
67
|
+
\u001b[32m "b": [\u001b[39m
|
68
|
+
\u001b[32m 1,\u001b[39m
|
69
|
+
\u001b[32m 2,\u001b[39m
|
70
|
+
\u001b[32m 3\u001b[39m
|
71
|
+
\u001b[32m ]\u001b[39m
|
72
|
+
\u001b[32m },\u001b[39m
|
73
|
+
\u001b[32m "req": "getCar"\u001b[39m
|
74
|
+
\u001b[32m}\u001b[39m \u001b[32m)\u001b[39m`)
|
75
|
+
});
|
51
76
|
})
|
package/src/methods/context.js
CHANGED
@@ -3,10 +3,6 @@ import Logger from "../Logger.js";
|
|
3
3
|
export default function context(_context) {
|
4
4
|
const logger = this.clone();
|
5
5
|
logger.contextName = _context;
|
6
|
-
logger.methodName = this.methodName;
|
7
|
-
logger.moduleName = this.moduleName;
|
8
|
-
logger.listenerName = this.listenerName;
|
9
|
-
|
10
6
|
logger._log = this._log.bind(logger);
|
11
7
|
return logger;
|
12
8
|
}
|
package/src/methods/debug.js
CHANGED
@@ -1,66 +1,8 @@
|
|
1
1
|
import LOG_LEVELS from "../LOG_LEVELS.js";
|
2
|
-
import
|
3
|
-
|
2
|
+
import log from "../utils/log.js";
|
4
3
|
export default function debug(...args) {
|
5
4
|
if(LOG_LEVELS[this.level] > LOG_LEVELS['debug']){
|
6
5
|
return;
|
7
6
|
}
|
8
|
-
|
9
|
-
if(args.length > 1){
|
10
|
-
_message = args.slice(0).join(' | ');
|
11
|
-
}
|
12
|
-
const date = this.date || new Date();
|
13
|
-
let message = `[${chalk.gray(date.toISOString())}][${chalk.blue('D')}]`;
|
14
|
-
|
15
|
-
|
16
|
-
if(this.contextName){
|
17
|
-
message += ` context: ${chalk.blueBright(this.contextName)} |`;
|
18
|
-
}
|
19
|
-
if(this.moduleName){
|
20
|
-
message += ` module:${chalk.gray(this.moduleName)} |`;
|
21
|
-
}
|
22
|
-
if(this.listenerName){
|
23
|
-
message += ` listener: ${chalk.cyan(this.listenerName)} |`;
|
24
|
-
}
|
25
|
-
if(this.methodName){
|
26
|
-
message += ` method: ${chalk.yellow(this.methodName)} |`;
|
27
|
-
}
|
28
|
-
if(message.endsWith(' |')){
|
29
|
-
message = message.slice(0, -1);
|
30
|
-
}
|
31
|
-
|
32
|
-
for(let i = 0; i < args.length; i++){
|
33
|
-
|
34
|
-
const typeOfArg = typeof args[i];
|
35
|
-
|
36
|
-
switch (typeOfArg){
|
37
|
-
case 'object':
|
38
|
-
const constructorName = args[i].constructor.name;
|
39
|
-
message += ` ${chalk.blue(`${constructorName}(`)}`
|
40
|
-
if(constructorName === 'Error'){
|
41
|
-
message += chalk.blue(JSON.stringify(args[i].message, null, 2))
|
42
|
-
}
|
43
|
-
message += chalk.blue(JSON.stringify(args[i], null, 2))
|
44
|
-
message += ` ${chalk.blue(")")}`
|
45
|
-
break;
|
46
|
-
case 'string':
|
47
|
-
message += chalk.blue(args[i])
|
48
|
-
break;
|
49
|
-
case "function":
|
50
|
-
message += chalk.blue(args[i].toString())
|
51
|
-
break;
|
52
|
-
case "number":
|
53
|
-
default:
|
54
|
-
message += chalk.blue(args[i])
|
55
|
-
break;
|
56
|
-
}
|
57
|
-
|
58
|
-
}
|
59
|
-
|
60
|
-
this.history.push(message);
|
61
|
-
if(this.history.length > 100){
|
62
|
-
this.history.shift();
|
63
|
-
}
|
64
|
-
|
65
|
-
this._log(message);
|
7
|
+
log('debug', args, this);
|
66
8
|
}
|
package/src/methods/error.js
CHANGED
@@ -1,64 +1,8 @@
|
|
1
1
|
import LOG_LEVELS from "../LOG_LEVELS.js";
|
2
|
-
import
|
3
|
-
|
4
|
-
export default function error(...args) {
|
2
|
+
import log from "../utils/log.js";
|
3
|
+
export default function info(...args) {
|
5
4
|
if(LOG_LEVELS[this.level] > LOG_LEVELS['error']){
|
6
5
|
return;
|
7
6
|
}
|
8
|
-
|
9
|
-
if(args.length > 1){
|
10
|
-
_message = args.slice(0).join(' | ');
|
11
|
-
}
|
12
|
-
const date = this.date || new Date();
|
13
|
-
let message = `[${chalk.gray(date.toISOString())}][${chalk.red('E')}]`;
|
14
|
-
|
15
|
-
if(this.contextName){
|
16
|
-
message += ` context: ${chalk.blueBright(this.contextName)} |`;
|
17
|
-
}
|
18
|
-
if(this.moduleName){
|
19
|
-
message += ` module:${chalk.gray(this.moduleName)} |`;
|
20
|
-
}
|
21
|
-
if(this.listenerName){
|
22
|
-
message += ` listener: ${chalk.cyan(this.listenerName)} |`;
|
23
|
-
}
|
24
|
-
if(this.methodName){
|
25
|
-
message += ` method: ${chalk.yellow(this.methodName)} |`;
|
26
|
-
}
|
27
|
-
if(message.endsWith(' |')){
|
28
|
-
message = message.slice(0, -1);
|
29
|
-
}
|
30
|
-
|
31
|
-
for(let i = 0; i < args.length; i++){
|
32
|
-
|
33
|
-
const typeOfArg = typeof args[i];
|
34
|
-
|
35
|
-
switch (typeOfArg){
|
36
|
-
case 'object':
|
37
|
-
const constructorName = args[i].constructor.name;
|
38
|
-
message += ` ${chalk.red(`${constructorName}(`)}`
|
39
|
-
if(constructorName === 'Error'){
|
40
|
-
message += chalk.red(JSON.stringify(args[i].message, null, 2))
|
41
|
-
}
|
42
|
-
message += chalk.red(JSON.stringify(args[i], null, 2))
|
43
|
-
message += ` ${chalk.red(")")}`
|
44
|
-
break;
|
45
|
-
case 'string':
|
46
|
-
message += chalk.red(args[i])
|
47
|
-
break;
|
48
|
-
case "function":
|
49
|
-
message += chalk.red(args[i].toString())
|
50
|
-
break;
|
51
|
-
case "number":
|
52
|
-
default:
|
53
|
-
message += chalk.red(args[i])
|
54
|
-
break;
|
55
|
-
}
|
56
|
-
}
|
57
|
-
|
58
|
-
this.history.push(message);
|
59
|
-
if(this.history.length > 100){
|
60
|
-
this.history.shift();
|
61
|
-
}
|
62
|
-
|
63
|
-
this._log(message);
|
7
|
+
log('error', args, this);
|
64
8
|
}
|
package/src/methods/info.js
CHANGED
@@ -1,66 +1,8 @@
|
|
1
1
|
import LOG_LEVELS from "../LOG_LEVELS.js";
|
2
|
-
import
|
3
|
-
|
2
|
+
import log from "../utils/log.js";
|
4
3
|
export default function info(...args) {
|
5
4
|
if(LOG_LEVELS[this.level] > LOG_LEVELS['info']){
|
6
5
|
return;
|
7
6
|
}
|
8
|
-
|
9
|
-
if(args.length > 1){
|
10
|
-
_message = args.slice(0).join(' | ');
|
11
|
-
}
|
12
|
-
const date = this.date || new Date();
|
13
|
-
let message = `[${chalk.gray(date.toISOString())}][${chalk.green('I')}]`;
|
14
|
-
|
15
|
-
|
16
|
-
if(this.contextName){
|
17
|
-
message += ` context: ${chalk.blueBright(this.contextName)} |`;
|
18
|
-
}
|
19
|
-
if(this.moduleName){
|
20
|
-
message += ` module:${chalk.gray(this.moduleName)} |`;
|
21
|
-
}
|
22
|
-
if(this.listenerName){
|
23
|
-
message += ` listener: ${chalk.cyan(this.listenerName)} |`;
|
24
|
-
}
|
25
|
-
if(this.methodName){
|
26
|
-
message += ` method: ${chalk.yellow(this.methodName)} |`;
|
27
|
-
}
|
28
|
-
if(message.endsWith(' |')){
|
29
|
-
message = message.slice(0, -1);
|
30
|
-
}
|
31
|
-
|
32
|
-
for(let i = 0; i < args.length; i++){
|
33
|
-
|
34
|
-
const typeOfArg = typeof args[i];
|
35
|
-
|
36
|
-
switch (typeOfArg){
|
37
|
-
case 'object':
|
38
|
-
const constructorName = args[i].constructor.name;
|
39
|
-
message += ` ${chalk.green(`${constructorName}(`)}`
|
40
|
-
if(constructorName === 'Error'){
|
41
|
-
message += chalk.green(JSON.stringify(args[i].message, null, 2))
|
42
|
-
}
|
43
|
-
message += chalk.green(JSON.stringify(args[i], null, 2))
|
44
|
-
message += ` ${chalk.green(")")}`
|
45
|
-
break;
|
46
|
-
case 'string':
|
47
|
-
message += chalk.green(args[i])
|
48
|
-
break;
|
49
|
-
case "function":
|
50
|
-
message += chalk.green(args[i].toString())
|
51
|
-
break;
|
52
|
-
case "number":
|
53
|
-
default:
|
54
|
-
message += chalk.green(args[i])
|
55
|
-
break;
|
56
|
-
}
|
57
|
-
|
58
|
-
}
|
59
|
-
|
60
|
-
this.history.push(message);
|
61
|
-
if(this.history.length > 100){
|
62
|
-
this.history.shift();
|
63
|
-
}
|
64
|
-
|
65
|
-
this._log(message);
|
7
|
+
log('info', args, this);
|
66
8
|
}
|
package/src/methods/listener.js
CHANGED
@@ -2,8 +2,6 @@ import Logger from "../Logger.js";
|
|
2
2
|
|
3
3
|
export default function listener(_listener) {
|
4
4
|
const logger = this.clone();
|
5
|
-
logger.moduleName = this.moduleName;
|
6
|
-
logger.contextName = this.contextName;
|
7
5
|
logger.listenerName = _listener;
|
8
6
|
logger._log = this._log.bind(logger);
|
9
7
|
return logger;
|
package/src/methods/log.js
CHANGED
@@ -1,6 +1,8 @@
|
|
1
|
-
import chalk from "chalk";
|
2
1
|
import LOG_LEVELS from "../LOG_LEVELS.js";
|
3
|
-
|
2
|
+
import _log from "../utils/log.js";
|
4
3
|
export default function log(...args) {
|
5
|
-
|
4
|
+
if(LOG_LEVELS[this.level] > LOG_LEVELS['info']){
|
5
|
+
return;
|
6
|
+
}
|
7
|
+
_log('log', args, this);
|
6
8
|
}
|
package/src/methods/method.js
CHANGED
@@ -2,9 +2,6 @@ import Logger from "../Logger.js";
|
|
2
2
|
|
3
3
|
export default function method(_method) {
|
4
4
|
const logger = this.clone();
|
5
|
-
logger.moduleName = this.moduleName;
|
6
|
-
logger.contextName = this.contextName;
|
7
|
-
logger.listenerName = this.listenerName;
|
8
5
|
logger.methodName = _method;
|
9
6
|
logger._log = this._log.bind(logger);
|
10
7
|
return logger;
|
package/src/methods/module.js
CHANGED
@@ -1,12 +1,8 @@
|
|
1
1
|
import Logger from "../Logger.js";
|
2
2
|
|
3
|
-
export default function
|
3
|
+
export default function module(_module) {
|
4
4
|
const logger = this.clone();
|
5
|
-
logger.methodName = this.methodName;
|
6
5
|
logger.moduleName = _module;
|
7
|
-
logger.contextName = this.contextName;
|
8
|
-
logger.listenerName = this.listenerName;
|
9
|
-
|
10
6
|
logger._log = this._log.bind(logger);
|
11
7
|
return logger;
|
12
8
|
}
|
package/src/methods/trace.js
CHANGED
@@ -1,64 +1,8 @@
|
|
1
1
|
import LOG_LEVELS from "../LOG_LEVELS.js";
|
2
|
-
import
|
3
|
-
|
2
|
+
import log from "../utils/log.js";
|
4
3
|
export default function trace(...args) {
|
5
4
|
if(LOG_LEVELS[this.level] > LOG_LEVELS['trace']){
|
6
5
|
return;
|
7
6
|
}
|
8
|
-
|
9
|
-
if(args.length > 1){
|
10
|
-
_message = args.slice(0).join(' | ');
|
11
|
-
}
|
12
|
-
const date = this.date || new Date();
|
13
|
-
let message = `[${chalk.gray(date.toISOString())}][${chalk.grey('T')}]`;
|
14
|
-
|
15
|
-
|
16
|
-
if(this.contextName){
|
17
|
-
message += ` context: ${chalk.blueBright(this.contextName)} |`;
|
18
|
-
}
|
19
|
-
if(this.moduleName){
|
20
|
-
message += ` module:${chalk.gray(this.moduleName)} |`;
|
21
|
-
}
|
22
|
-
if(this.listenerName){
|
23
|
-
message += ` listener: ${chalk.cyan(this.listenerName)} |`;
|
24
|
-
}
|
25
|
-
if(this.methodName){
|
26
|
-
message += ` method: ${chalk.yellow(this.methodName)} |`;
|
27
|
-
}
|
28
|
-
if(message.endsWith(' |')){
|
29
|
-
message = message.slice(0, -1);
|
30
|
-
}
|
31
|
-
|
32
|
-
for(let i = 0; i < args.length; i++){
|
33
|
-
const typeOfArg = typeof args[i];
|
34
|
-
|
35
|
-
switch (typeOfArg){
|
36
|
-
case 'object':
|
37
|
-
const constructorName = args[i].constructor.name;
|
38
|
-
message += ` ${chalk.grey(`${constructorName}(`)}`
|
39
|
-
if(constructorName === 'Error'){
|
40
|
-
message += chalk.grey(JSON.stringify(args[i].message, null, 2))
|
41
|
-
}
|
42
|
-
message += chalk.grey(JSON.stringify(args[i], null, 2))
|
43
|
-
message += ` ${chalk.grey(")")}`
|
44
|
-
break;
|
45
|
-
case 'string':
|
46
|
-
message += chalk.grey(args[i])
|
47
|
-
break;
|
48
|
-
case "function":
|
49
|
-
message += chalk.grey(args[i].toString())
|
50
|
-
break;
|
51
|
-
case "number":
|
52
|
-
default:
|
53
|
-
message += chalk.grey(args[i])
|
54
|
-
break;
|
55
|
-
}
|
56
|
-
}
|
57
|
-
|
58
|
-
this.history.push(message);
|
59
|
-
if(this.history.length > 100){
|
60
|
-
this.history.shift();
|
61
|
-
}
|
62
|
-
|
63
|
-
this._log(message);
|
7
|
+
log('trace', args, this);
|
64
8
|
}
|
package/src/methods/warn.js
CHANGED
@@ -1,65 +1,8 @@
|
|
1
1
|
import LOG_LEVELS from "../LOG_LEVELS.js";
|
2
|
-
import
|
3
|
-
|
2
|
+
import log from "../utils/log.js";
|
4
3
|
export default function warn(...args) {
|
5
4
|
if(LOG_LEVELS[this.level] > LOG_LEVELS['warn']){
|
6
5
|
return;
|
7
6
|
}
|
8
|
-
|
9
|
-
if(args.length > 1){
|
10
|
-
_message = args.slice(0).join(' | ');
|
11
|
-
}
|
12
|
-
const date = this.date || new Date();
|
13
|
-
let message = `[${chalk.gray(date.toISOString())}][${chalk.yellow('W')}]`;
|
14
|
-
|
15
|
-
|
16
|
-
if(this.contextName){
|
17
|
-
message += ` context: ${chalk.blueBright(this.contextName)} |`;
|
18
|
-
}
|
19
|
-
if(this.moduleName){
|
20
|
-
message += ` module:${chalk.gray(this.moduleName)} |`;
|
21
|
-
}
|
22
|
-
if(this.listenerName){
|
23
|
-
message += ` listener: ${chalk.cyan(this.listenerName)} |`;
|
24
|
-
}
|
25
|
-
if(this.methodName){
|
26
|
-
message += ` method: ${chalk.yellow(this.methodName)} |`;
|
27
|
-
}
|
28
|
-
if(message.endsWith(' |')){
|
29
|
-
message = message.slice(0, -1);
|
30
|
-
}
|
31
|
-
|
32
|
-
for(let i = 0; i < args.length; i++){
|
33
|
-
|
34
|
-
const typeOfArg = typeof args[i];
|
35
|
-
|
36
|
-
switch (typeOfArg){
|
37
|
-
case 'object':
|
38
|
-
const constructorName = args[i].constructor.name;
|
39
|
-
message += ` ${chalk.yellow(`${constructorName}(`)}`
|
40
|
-
if(constructorName === 'Error'){
|
41
|
-
message += chalk.yellow(JSON.stringify(args[i].message, null, 2))
|
42
|
-
}
|
43
|
-
message += chalk.yellow(JSON.stringify(args[i], null, 2))
|
44
|
-
message += ` ${chalk.yellow(")")}`
|
45
|
-
break;
|
46
|
-
case 'string':
|
47
|
-
message += chalk.yellow(args[i])
|
48
|
-
break;
|
49
|
-
case "function":
|
50
|
-
message += chalk.yellow(args[i].toString())
|
51
|
-
break;
|
52
|
-
case "number":
|
53
|
-
default:
|
54
|
-
message += chalk.yellow(args[i])
|
55
|
-
break;
|
56
|
-
}
|
57
|
-
}
|
58
|
-
|
59
|
-
this.history.push(message);
|
60
|
-
if(this.history.length > 100){
|
61
|
-
this.history.shift();
|
62
|
-
}
|
63
|
-
|
64
|
-
this._log(message);
|
7
|
+
log('warn', args, this);
|
65
8
|
}
|
package/src/utils/log.js
ADDED
@@ -0,0 +1,72 @@
|
|
1
|
+
// src/utils/log.js
|
2
|
+
export default function log(level, args, context) {
|
3
|
+
const LOG_COLORS = context.LOG_COLORS;
|
4
|
+
const colors = LOG_COLORS[level] || LOG_COLORS['info'];
|
5
|
+
const date = context.date || new Date();
|
6
|
+
let type = level.toUpperCase()[0];
|
7
|
+
let message = `[${colors[0](date.toISOString())}][${colors[1](type)}]`;
|
8
|
+
|
9
|
+
if(level === 'log'){
|
10
|
+
message = `[${colors[0](date.toISOString())}]`;
|
11
|
+
}
|
12
|
+
|
13
|
+
|
14
|
+
// TODO: It would be neat to just have a single parents array that would be used
|
15
|
+
// to generate the context string.
|
16
|
+
if(context.contextName){
|
17
|
+
message += ` context: ${colors[2](context.contextName)} |`;
|
18
|
+
}
|
19
|
+
if(context.moduleName){
|
20
|
+
message += ` module:${colors[3](context.moduleName)} |`;
|
21
|
+
}
|
22
|
+
if(context.listenerName){
|
23
|
+
message += ` listener: ${colors[4](context.listenerName)} |`;
|
24
|
+
}
|
25
|
+
if(context.methodName){
|
26
|
+
message += ` method: ${colors[5](context.methodName)} |`;
|
27
|
+
}
|
28
|
+
if(context.objectName){
|
29
|
+
message += ` Object[${colors[6](context.objectName)}] |`;
|
30
|
+
}
|
31
|
+
|
32
|
+
if(message.endsWith(' |')){
|
33
|
+
message = message.slice(0, -1);
|
34
|
+
}
|
35
|
+
|
36
|
+
for(let i = 0; i < args.length; i++){
|
37
|
+
const typeOfArg = typeof args[i];
|
38
|
+
let color;
|
39
|
+
switch (typeOfArg){
|
40
|
+
case 'object':
|
41
|
+
color = colors[6]; // object color
|
42
|
+
const constructorName = args[i].constructor.name;
|
43
|
+
message += ` ${color(`${constructorName}(`)}`
|
44
|
+
if(constructorName === 'Error'){
|
45
|
+
message += color(JSON.stringify(args[i].message, null, 2))
|
46
|
+
}
|
47
|
+
message += color(JSON.stringify(args[i], null, 2))
|
48
|
+
message += ` ${color(")")}`
|
49
|
+
break;
|
50
|
+
case 'string':
|
51
|
+
color = colors[7]; // string color
|
52
|
+
message += color(args[i])
|
53
|
+
break;
|
54
|
+
case "function":
|
55
|
+
color = colors[8]; // function color
|
56
|
+
message += color(args[i].toString())
|
57
|
+
break;
|
58
|
+
case "number":
|
59
|
+
default:
|
60
|
+
color = colors[9]; // number/default color
|
61
|
+
message += color(args[i])
|
62
|
+
break;
|
63
|
+
}
|
64
|
+
}
|
65
|
+
|
66
|
+
context.history.push(message);
|
67
|
+
if(context.history.length > 100){
|
68
|
+
context.history.shift();
|
69
|
+
}
|
70
|
+
|
71
|
+
context._log(message);
|
72
|
+
}
|
package/usage.example.js
CHANGED
@@ -1,9 +1,12 @@
|
|
1
1
|
import Logger from './src/Logger.js';
|
2
2
|
|
3
|
-
|
4
3
|
const logger = new Logger({
|
5
4
|
level: 'trace',
|
6
5
|
});
|
6
|
+
|
7
|
+
|
8
|
+
logger.context('APIContext').module('Websocket Server').listener('onMessage').method('processIncomingMessage').log('Hello World');
|
9
|
+
|
7
10
|
const contextLogger = logger.context('APIContext')
|
8
11
|
contextLogger.log('Started API');
|
9
12
|
const moduleLogger = contextLogger.module('Websocket Server')
|
@@ -42,6 +45,7 @@ listenerLogger.level = 'trace'
|
|
42
45
|
listenerLogger.error('Error',object)
|
43
46
|
listenerLogger.warn('Warn',object)
|
44
47
|
listenerLogger.info('Info',object)
|
48
|
+
listenerLogger.fatal('Fatal',object)
|
45
49
|
listenerLogger.debug('debug',object)
|
46
50
|
listenerLogger.trace('trace',object)
|
47
51
|
|
@@ -49,6 +53,7 @@ const error = new Error('Something happened');
|
|
49
53
|
listenerLogger.error('Error',error)
|
50
54
|
listenerLogger.warn('Warn',error)
|
51
55
|
listenerLogger.info('Info',error)
|
56
|
+
listenerLogger.fatal('Fatal',error)
|
52
57
|
listenerLogger.debug('debug',error)
|
53
58
|
listenerLogger.trace('trace',error)
|
54
59
|
|