hermodlog 1.4.0 → 1.5.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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hermodlog",
3
- "version": "1.4.0",
3
+ "version": "1.5.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",
@@ -18,10 +18,7 @@
18
18
  "url": "https://github.com/Alex-Werner/hermodlog/issues"
19
19
  },
20
20
  "homepage": "https://github.com/Alex-Werner/hermodlog#readme",
21
- "dependencies": {
22
- "chalk": "^5.3.0"
23
- },
24
21
  "devDependencies": {
25
- "vitest": "^0.34.5"
22
+ "vitest": "^2.1.8"
26
23
  }
27
24
  }
package/src/LOG_COLORS.js CHANGED
@@ -1,13 +1,13 @@
1
- import chalk from "chalk";
1
+ import colors from './utils/colors.js';
2
2
 
3
3
  const LOG_COLORS = {
4
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],
5
+ debug: [colors.gray, colors.blue, colors.blueBright, colors.gray, colors.cyan, colors.yellow, colors.blue, colors.blue, colors.blue, colors.blue],
6
+ error: [colors.gray, colors.red, colors.blueBright, colors.gray, colors.cyan, colors.yellow, colors.red, colors.red, colors.red, colors.red],
7
+ fatal: [colors.gray, colors.magenta, colors.blueBright, colors.gray, colors.cyan, colors.yellow, colors.magenta, colors.magenta, colors.magenta, colors.magenta],
8
+ info: [colors.gray, colors.green, colors.blueBright, colors.gray, colors.cyan, colors.yellow, colors.green, colors.green, colors.green, colors.green],
9
+ trace: [colors.gray, colors.gray, colors.blueBright, colors.gray, colors.cyan, colors.yellow, colors.gray, colors.gray, colors.gray, colors.gray],
10
+ warn: [colors.gray, colors.yellow, colors.blueBright, colors.gray, colors.cyan, colors.yellow, colors.yellow, colors.yellow, colors.yellow, colors.yellow],
11
11
  }
12
12
 
13
13
  export default LOG_COLORS;
package/src/Logger.js CHANGED
@@ -2,6 +2,7 @@ import LOG_LEVELS from "./LOG_LEVELS.js";
2
2
 
3
3
  import context from "./methods/context.js";
4
4
  import debug from "./methods/debug.js";
5
+ import dir from "./methods/dir.js";
5
6
  import error from "./methods/error.js";
6
7
  import info from "./methods/info.js";
7
8
  import listener from "./methods/listener.js";
@@ -17,24 +18,61 @@ import LOG_COLORS from "./LOG_COLORS.js";
17
18
  import object from "./methods/object.js";
18
19
  class Logger {
19
20
  constructor(props = {}) {
20
- this.level = props.level ?? 'info';
21
- if (!LOG_LEVELS.hasOwnProperty(this.level)) {
22
- throw new Error(`Unknown log level ${this.level}`)
23
- }
24
-
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;
21
+ // Define non-enumerable properties
22
+ Object.defineProperties(this, {
23
+ LOG_COLORS: {
24
+ value: props.colors ?? LOG_COLORS,
25
+ writable: true,
26
+ enumerable: false
27
+ },
28
+ history: {
29
+ value: props.history ?? [],
30
+ writable: true,
31
+ enumerable: false
32
+ },
33
+ level: {
34
+ value: props.level ?? 'info',
35
+ writable: true,
36
+ enumerable: true
37
+ },
38
+ contextName: {
39
+ value: props.contextName ?? null,
40
+ writable: true,
41
+ enumerable: true
42
+ },
43
+ methodName: {
44
+ value: props.methodName ?? null,
45
+ writable: true,
46
+ enumerable: true
47
+ },
48
+ moduleName: {
49
+ value: props.moduleName ?? null,
50
+ writable: true,
51
+ enumerable: true
52
+ },
53
+ listenerName: {
54
+ value: props.listenerName ?? null,
55
+ writable: true,
56
+ enumerable: true
57
+ },
58
+ objectName: {
59
+ value: props.objectName ?? null,
60
+ writable: true,
61
+ enumerable: true
62
+ }
63
+ });
32
64
 
33
65
  if (props.date) {
34
- this.date = props.date;
66
+ Object.defineProperty(this, 'date', {
67
+ value: props.date,
68
+ writable: true,
69
+ enumerable: false
70
+ });
35
71
  }
36
72
 
37
- this.LOG_COLORS = (props.colors) ?? LOG_COLORS;
73
+ if (!LOG_LEVELS.hasOwnProperty(this.level)) {
74
+ throw new Error(`Unknown log level ${this.level}`)
75
+ }
38
76
  }
39
77
 
40
78
  _log(message) {
@@ -64,11 +102,11 @@ class Logger {
64
102
  objectName: (keepObject) ? this.objectName : opts.objectName ?? null,
65
103
  })
66
104
  }
67
-
68
105
  };
69
106
 
70
107
  Logger.prototype.context = context;
71
108
  Logger.prototype.debug = debug;
109
+ Logger.prototype.dir = dir;
72
110
  Logger.prototype.error = error;
73
111
  Logger.prototype.fatal = fatal;
74
112
  Logger.prototype.info = info;
@@ -10,32 +10,36 @@ describe('Logger', () => {
10
10
  assert.equal(silentLogger.history.length, 0)
11
11
  silentLogger.log('Hello');
12
12
  assert.equal(silentLogger.history.length, 1)
13
- assert.equal(silentLogger.history[0], '[\u001b[90m2023-07-29T01:38:00.482Z\u001b[39m]\u001b[32mHello\u001b[39m')
13
+ const expected = '[\u001b[90m2023-07-29T01:38:00.482Z\u001b[0m] \u001b[32mHello\u001b[0m'
14
+ assert.equal(silentLogger.history[0], expected)
14
15
  })
15
16
  it('should create an logger with module', () => {
16
17
  silentModuleLogger = silentLogger.module('test-module')
17
18
  silentModuleLogger.log('Hello');
18
19
  assert.equal(silentModuleLogger.history.length, 1)
19
- assert.equal(silentModuleLogger.history[0], "[\u001b[90m2023-07-29T01:38:00.482Z\u001b[39m] module:\u001b[90mtest-module\u001b[39m \u001b[32mHello\u001b[39m")
20
+ const expected = '[\u001b[90m2023-07-29T01:38:00.482Z\u001b[0m] module:\u001b[90mtest-module\u001b[0m \u001b[32mHello\u001b[0m'
21
+ assert.equal(silentModuleLogger.history[0], expected)
20
22
  })
21
23
  it('should create an logger with context', () => {
22
24
  const silentContextLogger = silentModuleLogger.context('test-context')
23
25
  silentContextLogger.log('Hello');
24
26
  assert.equal(silentContextLogger.history.length, 1)
25
- assert.equal(silentContextLogger.history[0], "[\u001b[90m2023-07-29T01:38:00.482Z\u001b[39m] context: \u001b[94mtest-context\u001b[39m | module:\u001b[90mtest-module\u001b[39m \u001b[32mHello\u001b[39m")
27
+ const expected = '[\u001b[90m2023-07-29T01:38:00.482Z\u001b[0m] context: \u001b[94mtest-context\u001b[0m | module:\u001b[90mtest-module\u001b[0m \u001b[32mHello\u001b[0m'
28
+ assert.equal(silentContextLogger.history[0], expected)
26
29
  })
27
30
  it('should create an logger with listener', () => {
28
31
  const silentListenerLogger = silentModuleLogger.listener('onTest')
29
32
  silentListenerLogger.log('Hello');
30
33
  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[36monTest\u001b[39m \u001b[32mHello\u001b[39m")
34
+ const expected = '[\u001b[90m2023-07-29T01:38:00.482Z\u001b[0m] module:\u001b[90mtest-module\u001b[0m | listener: \u001b[36monTest\u001b[0m \u001b[32mHello\u001b[0m'
35
+ assert.equal(silentListenerLogger.history[0], expected)
32
36
  })
33
37
  it('should create an logger with method', () => {
34
38
  const silentMethodLogger = silentModuleLogger.method('test-method')
35
39
  silentMethodLogger.log('Hello');
36
40
  assert.equal(silentMethodLogger.history.length, 1)
37
-
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')
41
+ const expected = '[\u001b[90m2023-07-29T01:38:00.482Z\u001b[0m] module:\u001b[90mtest-module\u001b[0m | method: \u001b[33mtest-method\u001b[0m \u001b[32mHello\u001b[0m'
42
+ assert.equal(silentMethodLogger.history[0], expected)
39
43
  })
40
44
  it('should handle level', function () {
41
45
  const levelLogger = new Logger({level: 'error'})
@@ -60,17 +64,7 @@ describe('Logger', () => {
60
64
  }
61
65
  silentModuleLogger.log('Received from ws client', object)
62
66
  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`)
67
+ const expected = `[\u001b[90m2023-07-29T01:38:00.482Z\u001b[0m] module:\u001b[90mtest-module\u001b[0m \u001b[32mReceived from ws client\u001b[0m \u001b[32mObject(\u001b[0m\u001b[32m{\n \"x\": 42,\n \"y\": {\n \"b\": [\n 1,\n 2,\n 3\n ]\n },\n \"req\": \"getCar\"\n}\u001b[0m\u001b[32m)\u001b[0m`;
68
+ assert.equal(silentModuleLogger.history[1], expected)
75
69
  });
76
70
  })
@@ -0,0 +1,10 @@
1
+ import LOG_LEVELS from "../LOG_LEVELS.js";
2
+ import log from "../utils/log.js";
3
+
4
+ export default function dir(label, object) {
5
+ if(LOG_LEVELS[this.level] > LOG_LEVELS['info']){
6
+ return;
7
+ }
8
+ log('log', [label], this);
9
+ console.dir(object, { depth: 1, colors: true });
10
+ }
@@ -0,0 +1,17 @@
1
+ const ESC = '\x1b';
2
+
3
+ const createColorFn = (code) => (text) => `${ESC}[${code}m${text}${ESC}[0m`;
4
+
5
+ const colors = {
6
+ gray: createColorFn('90'),
7
+ red: createColorFn('31'),
8
+ green: createColorFn('32'),
9
+ yellow: createColorFn('33'),
10
+ blue: createColorFn('34'),
11
+ magenta: createColorFn('35'),
12
+ cyan: createColorFn('36'),
13
+ // Bright variants
14
+ blueBright: createColorFn('94'),
15
+ };
16
+
17
+ export default colors;
package/src/utils/log.js CHANGED
@@ -1,70 +1,113 @@
1
- // src/utils/log.js
1
+ function safeStringify(obj, indent = 2, depth = 5, level = 0) {
2
+ let cache = [];
3
+ const retVal = JSON.stringify(
4
+ obj,
5
+ (key, value) => {
6
+ if (level > depth) return '...';
7
+ if (typeof value === "object" && value !== null) {
8
+ if (cache.includes(value)) return undefined;
9
+ cache.push(value);
10
+ level++;
11
+ }
12
+ return value;
13
+ },
14
+ indent
15
+ );
16
+ cache = null;
17
+ return retVal;
18
+ }
19
+
2
20
  export default function log(level, args, context) {
3
21
  const LOG_COLORS = context.LOG_COLORS;
4
22
  const colors = LOG_COLORS[level] || LOG_COLORS['info'];
5
23
  const date = context.date || new Date();
6
24
  let type = level.toUpperCase()[0];
7
- let message = `[${colors[0](date.toISOString())}][${colors[1](type)}]`;
25
+ let message = `[${colors[0](date.toISOString())}]`;
8
26
 
9
- if(level === 'log'){
10
- message = `[${colors[0](date.toISOString())}]`;
27
+ if (level !== 'log') {
28
+ message += `[${colors[1](type)}]`;
11
29
  }
12
30
 
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)} |`;
31
+ if (context.contextName) {
32
+ message += ` context: ${colors[2](context.contextName)}`;
33
+ if (context.moduleName || context.listenerName || context.methodName) {
34
+ message += ' |';
35
+ }
21
36
  }
22
- if(context.listenerName){
23
- message += ` listener: ${colors[4](context.listenerName)} |`;
37
+
38
+ if (context.moduleName) {
39
+ message += ` module:${colors[3](context.moduleName)}`;
40
+ if (context.listenerName || context.methodName) {
41
+ message += ' |';
42
+ }
24
43
  }
25
- if(context.methodName){
26
- message += ` method: ${colors[5](context.methodName)} |`;
44
+
45
+ if (context.listenerName) {
46
+ message += ` listener: ${colors[4](context.listenerName)}`;
47
+ if (context.methodName) {
48
+ message += ' |';
49
+ }
27
50
  }
28
- if(context.objectName){
29
- message += ` Object[${colors[6](context.objectName)}] |`;
51
+
52
+ if (context.methodName) {
53
+ message += ` method: ${colors[5](context.methodName)}`;
30
54
  }
31
55
 
32
- if(message.endsWith(' |')){
33
- message = message.slice(0, -1);
56
+ if (context.objectName) {
57
+ message += ` Object[${colors[6](context.objectName)}]`;
34
58
  }
35
59
 
36
- for(let i = 0; i < args.length; i++){
37
- const typeOfArg = typeof args[i];
60
+ message += ' ';
61
+
62
+ for (let i = 0; i < args.length; i++) {
63
+ const arg = args[i];
64
+ const typeOfArg = typeof arg;
38
65
  let color;
39
- switch (typeOfArg){
66
+
67
+ switch (typeOfArg) {
40
68
  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))
69
+ color = colors[6];
70
+ if (!arg) {
71
+ message += `${color('null')}`;
72
+ break;
73
+ }
74
+ const constructorName = arg?.constructor?.name;
75
+ if (constructorName === 'Object') {
76
+ message += `${color('Object(')}${color(safeStringify(arg, 2))}${color(')')}`;
77
+ } else if (constructorName === 'Error') {
78
+ message += `${color(constructorName)}(${color(arg.message)})\n`;
79
+ if (arg.stack) {
80
+ const stackLines = arg.stack.split('\n');
81
+ // Skip first line as it contains the error message we already logged
82
+ for (let j = 1; j < stackLines.length; j++) {
83
+ message += `${color(stackLines[j].trim())}\n`;
84
+ }
85
+ }
86
+ } else {
87
+ message += `${color(constructorName)}(${color(safeStringify(arg, 2))})`;
46
88
  }
47
- message += color(JSON.stringify(args[i], null, 2))
48
- message += ` ${color(")")}`
49
89
  break;
50
90
  case 'string':
51
- color = colors[7]; // string color
52
- message += color(args[i])
91
+ color = colors[7];
92
+ message += color(arg);
53
93
  break;
54
94
  case "function":
55
- color = colors[8]; // function color
56
- message += color(args[i].toString())
95
+ color = colors[8];
96
+ message += color(arg.toString());
57
97
  break;
58
- case "number":
59
98
  default:
60
- color = colors[9]; // number/default color
61
- message += color(args[i])
99
+ color = colors[9];
100
+ message += color(String(arg));
62
101
  break;
63
102
  }
103
+
104
+ if (i < args.length - 1) {
105
+ message += ' ';
106
+ }
64
107
  }
65
108
 
66
109
  context.history.push(message);
67
- if(context.history.length > 100){
110
+ if (context.history.length > 100) {
68
111
  context.history.shift();
69
112
  }
70
113
 
package/usage.example.js CHANGED
@@ -57,3 +57,9 @@ listenerLogger.fatal('Fatal',error)
57
57
  listenerLogger.debug('debug',error)
58
58
  listenerLogger.trace('trace',error)
59
59
 
60
+
61
+ const errorWithStack = new Error('Something happened with stack');
62
+ errorWithStack.stack = '\nThis is a stack trace with a new line \n and a tab \t';
63
+ listenerLogger.error('Error',errorWithStack)
64
+
65
+ console.dir(listenerLogger, { depth: null });