hermodlog 1.1.1 → 1.3.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 +51 -10
- package/package.json +1 -1
- package/src/LOG_COLORS.js +13 -0
- package/src/LOG_LEVELS.js +1 -0
- package/src/Logger.js +7 -0
- package/src/Logger.spec.js +37 -2
- package/src/methods/child.js +6 -0
- package/src/methods/debug.js +2 -44
- package/src/methods/error.js +3 -44
- package/src/methods/fatal.js +8 -0
- package/src/methods/info.js +2 -44
- package/src/methods/log.js +5 -3
- package/src/methods/trace.js +2 -44
- package/src/methods/warn.js +2 -44
- package/src/utils/log.js +65 -0
- package/usage.example.js +41 -5
package/README.md
CHANGED
@@ -1,10 +1,12 @@
|
|
1
1
|
# hermodlog
|
2
2
|
|
3
|
+
[](https://badge.fury.io/js/hermodlog)
|
4
|
+
|
3
5
|
Stupid simple logging for JS but with context for heavy log environments.
|
4
6
|
|
5
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.
|
6
8
|
|
7
|
-
<img width="
|
9
|
+
<img width="1065" alt="image" src="https://github.com/Alex-Werner/hermodlog/assets/5849920/1df7e7dd-cf25-4ce3-872b-dab1288966e4">
|
8
10
|
|
9
11
|
|
10
12
|
## Install
|
@@ -18,13 +20,14 @@ npm i hermodlog
|
|
18
20
|
```js
|
19
21
|
import Logger from 'hermodlog';
|
20
22
|
|
21
|
-
logger = new Logger(
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
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!");
|
28
31
|
```
|
29
32
|
|
30
33
|
## API
|
@@ -35,10 +38,18 @@ moduleLogger = contextLogger.module('myModule');
|
|
35
38
|
Create a new logger instance.
|
36
39
|
|
37
40
|
##### `context(name)`
|
38
|
-
Create a new context logger with the given name.
|
41
|
+
Create a new context logger with the given context name.
|
39
42
|
|
40
43
|
##### `module(name)`
|
41
|
-
Create a new module logger with the given name.
|
44
|
+
Create a new module logger with the given module name.
|
45
|
+
|
46
|
+
##### `listener(name)`
|
47
|
+
Create a new listener sublogger with the given listener name.
|
48
|
+
|
49
|
+
##### `method(name)`
|
50
|
+
Create a new method sublogger with the given method name.
|
51
|
+
|
52
|
+
#### Level methods
|
42
53
|
|
43
54
|
##### `log(message, level)`
|
44
55
|
Log a message with the given level.
|
@@ -52,6 +63,9 @@ Log a message with the info level.
|
|
52
63
|
##### `warn(message)`
|
53
64
|
Log a message with the warn level.
|
54
65
|
|
66
|
+
##### `fatal(message)`
|
67
|
+
Log a message with the fatal level.
|
68
|
+
|
55
69
|
##### `error(message)`
|
56
70
|
Log a message with the error level.
|
57
71
|
|
@@ -59,6 +73,33 @@ Log a message with the error level.
|
|
59
73
|
Log a message with the fatal level.
|
60
74
|
|
61
75
|
|
76
|
+
#### constructor options
|
77
|
+
The `options` object can have the following properties:
|
78
|
+
|
79
|
+
level: The default log level. Can be one of 'fatal', 'error', 'warn', 'info', 'debug', 'trace'.
|
80
|
+
colors: 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].
|
81
|
+
date: A date-compatible format
|
82
|
+
|
83
|
+
```js
|
84
|
+
{
|
85
|
+
const options = {
|
86
|
+
date: new Date('2024-01-01T00:01:01.001Z')},
|
87
|
+
level: 'info', // Set the default log level
|
88
|
+
colors: { // Set the colors for different parts of the log message
|
89
|
+
fatal: [chalk.gray, chalk.red, chalk.blueBright, chalk.gray, chalk.cyan, chalk.yellow, chalk.magenta, chalk.white, chalk.blue, chalk.cyan],
|
90
|
+
error: [chalk.gray, chalk.red, chalk.blueBright, chalk.gray, chalk.cyan, chalk.yellow, chalk.red, chalk.yellow, chalk.green, chalk.blue],
|
91
|
+
warn: [chalk.gray, chalk.red, chalk.blueBright, chalk.gray, chalk.cyan, chalk.yellow, chalk.yellow, chalk.blue, chalk.magenta, chalk.red],
|
92
|
+
info: [chalk.gray, chalk.red, chalk.blueBright, chalk.gray, chalk.cyan, chalk.yellow, chalk.blue, chalk.green, chalk.red, chalk.yellow],
|
93
|
+
debug: [chalk.gray, chalk.red, chalk.blueBright, chalk.gray, chalk.cyan, chalk.yellow, chalk.green, chalk.red, chalk.yellow, chalk.blue],
|
94
|
+
trace: [chalk.gray, chalk.red, chalk.blueBright, chalk.gray, chalk.cyan, chalk.yellow, chalk.magenta, chalk.yellow, chalk.blue, chalk.green],
|
95
|
+
}
|
96
|
+
};
|
97
|
+
const logger = new Logger(options);
|
98
|
+
|
99
|
+
logger.log('Hello World');
|
100
|
+
}
|
101
|
+
```
|
102
|
+
|
62
103
|
### License
|
63
104
|
|
64
105
|
MIT
|
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,10 @@ 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";
|
14
17
|
class Logger {
|
15
18
|
constructor(props = {}) {
|
16
19
|
this.level = props.level ?? 'info';
|
@@ -27,6 +30,8 @@ class Logger {
|
|
27
30
|
if (props.date) {
|
28
31
|
this.date = props.date;
|
29
32
|
}
|
33
|
+
|
34
|
+
this.LOG_COLORS = (props.colors) ?? LOG_COLORS;
|
30
35
|
}
|
31
36
|
|
32
37
|
_log(message) {
|
@@ -45,6 +50,7 @@ class Logger {
|
|
45
50
|
Logger.prototype.context = context;
|
46
51
|
Logger.prototype.debug = debug;
|
47
52
|
Logger.prototype.error = error;
|
53
|
+
Logger.prototype.fatal = fatal;
|
48
54
|
Logger.prototype.info = info;
|
49
55
|
Logger.prototype.listener = listener;
|
50
56
|
Logger.prototype.log = log;
|
@@ -52,5 +58,6 @@ Logger.prototype.method = method;
|
|
52
58
|
Logger.prototype.module = module;
|
53
59
|
Logger.prototype.trace = trace;
|
54
60
|
Logger.prototype.warn = warn;
|
61
|
+
Logger.prototype.child = child;
|
55
62
|
|
56
63
|
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')
|
@@ -37,4 +37,39 @@ describe('Logger', () => {
|
|
37
37
|
|
38
38
|
assert.equal(silentMethodLogger.history[0], '[\u001b[90m2023-07-29T01:38:00.482Z\u001b[39m] module:\u001b[90mtest-module\u001b[39m | method: \u001b[33mtest-method\u001b[39m \u001b[32mHello\u001b[39m')
|
39
39
|
})
|
40
|
+
it('should handle level', function () {
|
41
|
+
const levelLogger = new Logger({level: 'error'})
|
42
|
+
levelLogger.error('Hello');
|
43
|
+
assert.equal(levelLogger.history.length, 1)
|
44
|
+
levelLogger.warn('Hello');
|
45
|
+
assert.equal(levelLogger.history.length, 1)
|
46
|
+
|
47
|
+
levelLogger.level = 'info'
|
48
|
+
levelLogger.trace('Hello');
|
49
|
+
assert.equal(levelLogger.history.length, 1)
|
50
|
+
});
|
51
|
+
|
52
|
+
it('should automatically beautify instead of [Object Object]', () => {
|
53
|
+
const object = {
|
54
|
+
x: 42,
|
55
|
+
y: {
|
56
|
+
b: [1, 2, 3]
|
57
|
+
},
|
58
|
+
req: 'getCar'
|
59
|
+
}
|
60
|
+
silentModuleLogger.log('Received from ws client', object)
|
61
|
+
assert.equal(silentModuleLogger.history.length, 2)
|
62
|
+
// If you change the styling of below it will assume spaces are part of the string
|
63
|
+
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
|
64
|
+
\u001b[32m "x": 42,\u001b[39m
|
65
|
+
\u001b[32m "y": {\u001b[39m
|
66
|
+
\u001b[32m "b": [\u001b[39m
|
67
|
+
\u001b[32m 1,\u001b[39m
|
68
|
+
\u001b[32m 2,\u001b[39m
|
69
|
+
\u001b[32m 3\u001b[39m
|
70
|
+
\u001b[32m ]\u001b[39m
|
71
|
+
\u001b[32m },\u001b[39m
|
72
|
+
\u001b[32m "req": "getCar"\u001b[39m
|
73
|
+
\u001b[32m}\u001b[39m \u001b[32m)\u001b[39m`)
|
74
|
+
});
|
40
75
|
})
|
package/src/methods/debug.js
CHANGED
@@ -1,50 +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
|
-
if(typeof args[i] === 'object'){
|
34
|
-
const constructorName = args[i].constructor.name;
|
35
|
-
message += ` ${chalk.blue(`${constructorName}(`)}`
|
36
|
-
message += chalk.blue(JSON.stringify(args[i], null, 2))
|
37
|
-
message += ` ${chalk.blue(")")}`
|
38
|
-
}
|
39
|
-
if(typeof args[i] === 'string'){
|
40
|
-
message += chalk.blue(args[i])
|
41
|
-
}
|
42
|
-
}
|
43
|
-
|
44
|
-
this.history.push(message);
|
45
|
-
if(this.history.length > 100){
|
46
|
-
this.history.shift();
|
47
|
-
}
|
48
|
-
|
49
|
-
this._log(message);
|
7
|
+
log('debug', args, this);
|
50
8
|
}
|
package/src/methods/error.js
CHANGED
@@ -1,49 +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
|
-
if(typeof args[i] === 'object'){
|
33
|
-
const constructorName = args[i].constructor.name;
|
34
|
-
message += ` ${chalk.red(`${constructorName}(`)}`
|
35
|
-
message += chalk.red(JSON.stringify(args[i], null, 2))
|
36
|
-
message += ` ${chalk.red(")")}`
|
37
|
-
}
|
38
|
-
if(typeof args[i] === 'string'){
|
39
|
-
message += chalk.red(args[i])
|
40
|
-
}
|
41
|
-
}
|
42
|
-
|
43
|
-
this.history.push(message);
|
44
|
-
if(this.history.length > 100){
|
45
|
-
this.history.shift();
|
46
|
-
}
|
47
|
-
|
48
|
-
this._log(message);
|
7
|
+
log('error', args, this);
|
49
8
|
}
|
package/src/methods/info.js
CHANGED
@@ -1,50 +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
|
-
if(typeof args[i] === 'object'){
|
34
|
-
const constructorName = args[i].constructor.name;
|
35
|
-
message += ` ${chalk.yellow(`${constructorName}(`)}`
|
36
|
-
message += chalk.yellow(JSON.stringify(args[i], null, 2))
|
37
|
-
message += ` ${chalk.yellow(")")}`
|
38
|
-
}
|
39
|
-
if(typeof args[i] === 'string'){
|
40
|
-
message += chalk.green(args[i])
|
41
|
-
}
|
42
|
-
}
|
43
|
-
|
44
|
-
this.history.push(message);
|
45
|
-
if(this.history.length > 100){
|
46
|
-
this.history.shift();
|
47
|
-
}
|
48
|
-
|
49
|
-
this._log(message);
|
7
|
+
log('info', args, this);
|
50
8
|
}
|
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/trace.js
CHANGED
@@ -1,50 +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
|
-
if(typeof args[i] === 'object'){
|
34
|
-
const constructorName = args[i].constructor.name;
|
35
|
-
message += ` ${chalk.grey(`${constructorName}(`)}`
|
36
|
-
message += chalk.grey(JSON.stringify(args[i], null, 2))
|
37
|
-
message += ` ${chalk.grey(")")}`
|
38
|
-
}
|
39
|
-
if(typeof args[i] === 'string'){
|
40
|
-
message += chalk.grey(args[i])
|
41
|
-
}
|
42
|
-
}
|
43
|
-
|
44
|
-
this.history.push(message);
|
45
|
-
if(this.history.length > 100){
|
46
|
-
this.history.shift();
|
47
|
-
}
|
48
|
-
|
49
|
-
this._log(message);
|
7
|
+
log('trace', args, this);
|
50
8
|
}
|
package/src/methods/warn.js
CHANGED
@@ -1,50 +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
|
-
if(typeof args[i] === 'object'){
|
34
|
-
const constructorName = args[i].constructor.name;
|
35
|
-
message += ` ${chalk.yellow(`${constructorName}(`)}`
|
36
|
-
message += chalk.yellow(JSON.stringify(args[i], null, 2))
|
37
|
-
message += ` ${chalk.yellow(")")}`
|
38
|
-
}
|
39
|
-
if(typeof args[i] === 'string'){
|
40
|
-
message += chalk.yellow(args[i])
|
41
|
-
}
|
42
|
-
}
|
43
|
-
|
44
|
-
this.history.push(message);
|
45
|
-
if(this.history.length > 100){
|
46
|
-
this.history.shift();
|
47
|
-
}
|
48
|
-
|
49
|
-
this._log(message);
|
7
|
+
log('warn', args, this);
|
50
8
|
}
|
package/src/utils/log.js
ADDED
@@ -0,0 +1,65 @@
|
|
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
|
+
if(context.contextName){
|
14
|
+
message += ` context: ${colors[2](context.contextName)} |`;
|
15
|
+
}
|
16
|
+
if(context.moduleName){
|
17
|
+
message += ` module:${colors[3](context.moduleName)} |`;
|
18
|
+
}
|
19
|
+
if(context.listenerName){
|
20
|
+
message += ` listener: ${colors[4](context.listenerName)} |`;
|
21
|
+
}
|
22
|
+
if(context.methodName){
|
23
|
+
message += ` method: ${colors[5](context.methodName)} |`;
|
24
|
+
}
|
25
|
+
if(message.endsWith(' |')){
|
26
|
+
message = message.slice(0, -1);
|
27
|
+
}
|
28
|
+
|
29
|
+
for(let i = 0; i < args.length; i++){
|
30
|
+
const typeOfArg = typeof args[i];
|
31
|
+
let color;
|
32
|
+
switch (typeOfArg){
|
33
|
+
case 'object':
|
34
|
+
color = colors[6]; // object color
|
35
|
+
const constructorName = args[i].constructor.name;
|
36
|
+
message += ` ${color(`${constructorName}(`)}`
|
37
|
+
if(constructorName === 'Error'){
|
38
|
+
message += color(JSON.stringify(args[i].message, null, 2))
|
39
|
+
}
|
40
|
+
message += color(JSON.stringify(args[i], null, 2))
|
41
|
+
message += ` ${color(")")}`
|
42
|
+
break;
|
43
|
+
case 'string':
|
44
|
+
color = colors[7]; // string color
|
45
|
+
message += color(args[i])
|
46
|
+
break;
|
47
|
+
case "function":
|
48
|
+
color = colors[8]; // function color
|
49
|
+
message += color(args[i].toString())
|
50
|
+
break;
|
51
|
+
case "number":
|
52
|
+
default:
|
53
|
+
color = colors[9]; // number/default color
|
54
|
+
message += color(args[i])
|
55
|
+
break;
|
56
|
+
}
|
57
|
+
}
|
58
|
+
|
59
|
+
context.history.push(message);
|
60
|
+
if(context.history.length > 100){
|
61
|
+
context.history.shift();
|
62
|
+
}
|
63
|
+
|
64
|
+
context._log(message);
|
65
|
+
}
|
package/usage.example.js
CHANGED
@@ -4,6 +4,29 @@ import Logger from './src/Logger.js';
|
|
4
4
|
const logger = new Logger({
|
5
5
|
level: 'trace',
|
6
6
|
});
|
7
|
+
|
8
|
+
// {
|
9
|
+
// room: 'CANDLE',
|
10
|
+
// message: {
|
11
|
+
// payload: Candle {
|
12
|
+
// market: [Market],
|
13
|
+
// interval: '1m',
|
14
|
+
// open: '41303.70',
|
15
|
+
// close: '41305.60',
|
16
|
+
// low: '41301.00',
|
17
|
+
// high: '41308.50',
|
18
|
+
// volume: [Object],
|
19
|
+
// openTime: [Epoch],
|
20
|
+
// closeTime: [Epoch],
|
21
|
+
// trades: '223',
|
22
|
+
// id: '8e884b29'
|
23
|
+
// },
|
24
|
+
// topic: 'CANDLE'
|
25
|
+
// }
|
26
|
+
// }
|
27
|
+
|
28
|
+
|
29
|
+
|
7
30
|
const contextLogger = logger.context('APIContext')
|
8
31
|
contextLogger.log('Started API');
|
9
32
|
const moduleLogger = contextLogger.module('Websocket Server')
|
@@ -19,6 +42,9 @@ const object = {
|
|
19
42
|
}
|
20
43
|
listenerLogger.method('processIncomingMessage').log('Received from ws client',object)
|
21
44
|
|
45
|
+
listenerLogger.log(()=>'This is a function that returns a string')
|
46
|
+
listenerLogger.log(1,2,3,4,5,6,7,8,9,10)
|
47
|
+
|
22
48
|
class Car {
|
23
49
|
constructor(props) {
|
24
50
|
this.name = props.name;
|
@@ -36,8 +62,18 @@ listenerLogger.trace('Do not display me trace on error');
|
|
36
62
|
listenerLogger.log('Do not display me log on error')
|
37
63
|
listenerLogger.error('Do display me')
|
38
64
|
listenerLogger.level = 'trace'
|
39
|
-
listenerLogger.error('Error')
|
40
|
-
listenerLogger.warn('Warn')
|
41
|
-
listenerLogger.info('Info')
|
42
|
-
listenerLogger.
|
43
|
-
listenerLogger.
|
65
|
+
listenerLogger.error('Error',object)
|
66
|
+
listenerLogger.warn('Warn',object)
|
67
|
+
listenerLogger.info('Info',object)
|
68
|
+
listenerLogger.fatal('Fatal',object)
|
69
|
+
listenerLogger.debug('debug',object)
|
70
|
+
listenerLogger.trace('trace',object)
|
71
|
+
|
72
|
+
const error = new Error('Something happened');
|
73
|
+
listenerLogger.error('Error',error)
|
74
|
+
listenerLogger.warn('Warn',error)
|
75
|
+
listenerLogger.info('Info',error)
|
76
|
+
listenerLogger.fatal('Fatal',error)
|
77
|
+
listenerLogger.debug('debug',error)
|
78
|
+
listenerLogger.trace('trace',error)
|
79
|
+
|