hermodlog 1.5.2 → 1.7.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.5.2",
3
+ "version": "1.7.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",
package/src/Logger.js CHANGED
@@ -13,6 +13,7 @@ import trace from "./methods/trace.js";
13
13
  import warn from "./methods/warn.js";
14
14
  import fatal from "./methods/fatal.js";
15
15
  import child from "./methods/child.js";
16
+ import submethod from "./methods/submethod.js";
16
17
 
17
18
  import LOG_COLORS from "./LOG_COLORS.js";
18
19
  import object from "./methods/object.js";
@@ -29,6 +30,11 @@ class Logger {
29
30
  writable: true,
30
31
  enumerable: false
31
32
  },
33
+ historyLimit: {
34
+ value: props.historyLimit ?? 100,
35
+ writable: true,
36
+ enumerable: true
37
+ },
32
38
  level: {
33
39
  value: props.level ?? 'info',
34
40
  writable: true,
@@ -54,11 +60,17 @@ class Logger {
54
60
  writable: true,
55
61
  enumerable: true
56
62
  },
63
+ submethodName: {
64
+ value: props.submethodName ?? null,
65
+ writable: true,
66
+ enumerable: true
67
+ },
57
68
  objectName: {
58
69
  value: props.objectName ?? null,
59
70
  writable: true,
60
71
  enumerable: true
61
72
  }
73
+
62
74
  });
63
75
 
64
76
  if (props.date) {
@@ -86,6 +98,7 @@ class Logger {
86
98
  const keepContext = opts?.keepContext ?? true;
87
99
  const keepMethod = opts?.keepMethod ?? true;
88
100
  const keepModule = opts?.keepModule ?? true;
101
+ const keepSubmethod = opts?.keepSubmethod ?? true;
89
102
  const keepListener = opts?.keepListener ?? true;
90
103
  const keepObject = opts?.keepObject ?? true;
91
104
 
@@ -98,9 +111,14 @@ class Logger {
98
111
  methodName: (keepMethod) ? this.methodName : opts.methodName ?? null,
99
112
  moduleName: (keepModule) ? this.moduleName : opts.moduleName ?? null,
100
113
  listenerName: (keepListener) ? this.listenerName : opts.listenerName ?? null,
114
+ submethodName: (keepSubmethod) ? this.submethodName : opts.submethodName ?? null,
101
115
  objectName: (keepObject) ? this.objectName : opts.objectName ?? null,
102
116
  })
103
117
  }
118
+
119
+ getHistory(limit = 100) {
120
+ return this.history.slice(-limit);
121
+ }
104
122
  };
105
123
 
106
124
  Logger.prototype.context = context;
@@ -113,6 +131,7 @@ Logger.prototype.listener = listener;
113
131
  Logger.prototype.log = log;
114
132
  Logger.prototype.method = method;
115
133
  Logger.prototype.object = object;
134
+ Logger.prototype.submethod = submethod;
116
135
  Logger.prototype.module = module;
117
136
  Logger.prototype.trace = trace;
118
137
  Logger.prototype.warn = warn;
@@ -0,0 +1,8 @@
1
+ import Logger from "../Logger.js";
2
+
3
+ export default function submethod(_submethod) {
4
+ const logger = this.clone();
5
+ logger.submethodName = _submethod;
6
+ logger._log = this._log.bind(logger);
7
+ return logger;
8
+ }
package/src/utils/log.js CHANGED
@@ -31,36 +31,59 @@ export default function log(level, args, context) {
31
31
  message += `[${colors[1](type)}]`;
32
32
  }
33
33
 
34
- if (context.contextName) {
34
+
35
+ const hasContext = !!context.contextName;
36
+ const hasModule = !!context.moduleName;
37
+ const hasListener = !!context.listenerName;
38
+ const hasMethod = !!context.methodName;
39
+ const hasSubmethod = !!context.submethodName;
40
+ const hasObject = !!context.objectName;
41
+
42
+ if (hasContext) {
35
43
  message += ` context: ${colors[2](context.contextName)}`;
36
- if (context.moduleName || context.listenerName || context.methodName) {
44
+ const hasNext = hasModule || hasListener || hasMethod || hasSubmethod;
45
+ if (hasNext) {
37
46
  message += ' |';
38
47
  }
39
48
  }
40
49
 
41
- if (context.moduleName) {
50
+ if (hasModule) {
42
51
  message += ` module:${colors[3](context.moduleName)}`;
43
- if (context.listenerName || context.methodName) {
52
+ const hasNext = hasListener || hasMethod || hasSubmethod;
53
+ if (hasNext) {
44
54
  message += ' |';
45
55
  }
46
56
  }
47
57
 
48
- if (context.listenerName) {
49
- message += ` listener: ${colors[4](context.listenerName)}`;
50
- if (context.methodName) {
58
+ if (hasMethod) {
59
+ message += ` method: ${colors[5](context.methodName)}`;
60
+ const hasNext = hasSubmethod;
61
+ if (hasNext) {
51
62
  message += ' |';
52
63
  }
53
64
  }
54
-
55
- if (context.methodName) {
56
- message += ` method: ${colors[5](context.methodName)}`;
65
+
66
+ if (hasSubmethod) {
67
+ message += ` submethod: ${colors[6](context.submethodName)}`;
68
+ const hasNext = hasListener || hasMethod;
69
+ if (hasNext) {
70
+ message += ' |';
71
+ }
72
+ }
73
+
74
+ if (hasListener) {
75
+ message += ` listener: ${colors[4](context.listenerName)}`;
76
+ const hasNext = hasObject;
77
+ if (hasNext) {
78
+ message += ' |';
79
+ }
57
80
  }
58
81
 
59
- if (context.objectName) {
82
+ if (hasObject) {
60
83
  message += ` Object[${colors[6](context.objectName)}]`;
61
84
  }
62
85
 
63
- message += ' ';
86
+ message += ' - ';
64
87
 
65
88
  for (let i = 0; i < args.length; i++) {
66
89
  const arg = args[i];
@@ -126,7 +149,8 @@ export default function log(level, args, context) {
126
149
  }
127
150
 
128
151
  context.history.push(message);
129
- if (context.history.length > 100) {
152
+ const historyLimit = context.historyLimit || 100;
153
+ if (context.history.length > historyLimit) {
130
154
  context.history.shift();
131
155
  }
132
156
 
package/usage.example.js CHANGED
@@ -6,6 +6,7 @@ const logger = new Logger({
6
6
 
7
7
 
8
8
 
9
+
9
10
  const listenerLogger = logger.listener('onConnection')
10
11
  const object = {
11
12
  x:42,
@@ -17,5 +18,18 @@ const object = {
17
18
 
18
19
  listenerLogger.log(`${object}`);
19
20
 
21
+ // listenerLogger.submethod('processIncomingMessage').log('Received from ws client',object)
22
+
23
+
24
+
25
+ // console.log({logger});
26
+
27
+ const el = logger.context('APIContext').module('Websocket Server').method('setListeners').listener('onMessage').submethod('processIncomingMessage')
28
+ el.log('Received from ws client');
20
29
 
21
- console.log({logger});
30
+ el.log(1);
31
+ el.log(2);
32
+ el.log(3);
33
+ el.log(4);
34
+ el.log(5);
35
+ console.log(el.getHistory(2));