hermodlog 1.2.0 → 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 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="1365" alt="image" src="https://github.com/Alex-Werner/hermodlog/assets/5849920/347d8d00-8cb1-4838-bc5d-e7434fdd019b">
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
- logger.log("Stuff");
25
-
26
-
27
- contextLogger = logger.context('Node#4');
28
-
29
- moduleLogger = contextLogger.module('myModule');
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
@@ -37,10 +38,18 @@ moduleLogger = contextLogger.module('myModule');
37
38
  Create a new logger instance.
38
39
 
39
40
  ##### `context(name)`
40
- Create a new context logger with the given name.
41
+ Create a new context logger with the given context name.
41
42
 
42
43
  ##### `module(name)`
43
- 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
44
53
 
45
54
  ##### `log(message, level)`
46
55
  Log a message with the given level.
@@ -54,6 +63,9 @@ Log a message with the info level.
54
63
  ##### `warn(message)`
55
64
  Log a message with the warn level.
56
65
 
66
+ ##### `fatal(message)`
67
+ Log a message with the fatal level.
68
+
57
69
  ##### `error(message)`
58
70
  Log a message with the error level.
59
71
 
@@ -61,6 +73,33 @@ Log a message with the error level.
61
73
  Log a message with the fatal level.
62
74
 
63
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
+
64
103
  ### License
65
104
 
66
105
  MIT
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hermodlog",
3
- "version": "1.2.0",
3
+ "version": "1.3.0",
4
4
  "description": "Simple contextual logger to simplify log display in heavy load context and provide easy parsing",
5
5
  "main": "src/Logger.js",
6
6
  "type": "module",
@@ -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
@@ -4,6 +4,7 @@ const LOG_LEVELS = {
4
4
  info: 2,
5
5
  warn: 3,
6
6
  error: 4,
7
+ fatal: 5
7
8
  }
8
9
 
9
10
  export default LOG_LEVELS
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;
@@ -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[31monTest\u001b[39m \u001b[32mHello\u001b[39m")
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')
@@ -48,4 +48,28 @@ describe('Logger', () => {
48
48
  levelLogger.trace('Hello');
49
49
  assert.equal(levelLogger.history.length, 1)
50
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
+ });
51
75
  })
@@ -0,0 +1,6 @@
1
+ export default function child(_child) {
2
+ const logger = this.clone();
3
+ logger.childName = _child;
4
+ logger._log = this._log.bind(logger);
5
+ return logger;
6
+ }
@@ -1,66 +1,8 @@
1
1
  import LOG_LEVELS from "../LOG_LEVELS.js";
2
- import chalk from "chalk";
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
- let _message = args[0];
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
  }
@@ -1,64 +1,8 @@
1
1
  import LOG_LEVELS from "../LOG_LEVELS.js";
2
- import chalk from "chalk";
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
- let _message = args[0];
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
  }
@@ -0,0 +1,8 @@
1
+ import LOG_LEVELS from "../LOG_LEVELS.js";
2
+ import log from "../utils/log.js";
3
+ export default function fatal(...args) {
4
+ if(LOG_LEVELS[this.level] > LOG_LEVELS['fatal']){
5
+ return;
6
+ }
7
+ log('fatal', args, this);
8
+ }
@@ -1,66 +1,8 @@
1
1
  import LOG_LEVELS from "../LOG_LEVELS.js";
2
- import chalk from "chalk";
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
- let _message = args[0];
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
  }
@@ -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
- return this.info(...args);
4
+ if(LOG_LEVELS[this.level] > LOG_LEVELS['info']){
5
+ return;
6
+ }
7
+ _log('log', args, this);
6
8
  }
@@ -1,64 +1,8 @@
1
1
  import LOG_LEVELS from "../LOG_LEVELS.js";
2
- import chalk from "chalk";
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
- let _message = args[0];
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
  }
@@ -1,65 +1,8 @@
1
1
  import LOG_LEVELS from "../LOG_LEVELS.js";
2
- import chalk from "chalk";
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
- let _message = args[0];
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
  }
@@ -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')
@@ -42,6 +65,7 @@ listenerLogger.level = 'trace'
42
65
  listenerLogger.error('Error',object)
43
66
  listenerLogger.warn('Warn',object)
44
67
  listenerLogger.info('Info',object)
68
+ listenerLogger.fatal('Fatal',object)
45
69
  listenerLogger.debug('debug',object)
46
70
  listenerLogger.trace('trace',object)
47
71
 
@@ -49,6 +73,7 @@ const error = new Error('Something happened');
49
73
  listenerLogger.error('Error',error)
50
74
  listenerLogger.warn('Warn',error)
51
75
  listenerLogger.info('Info',error)
76
+ listenerLogger.fatal('Fatal',error)
52
77
  listenerLogger.debug('debug',error)
53
78
  listenerLogger.trace('trace',error)
54
79