hermodlog 1.4.0 → 1.4.1

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/src/utils/log.js +28 -4
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hermodlog",
3
- "version": "1.4.0",
3
+ "version": "1.4.1",
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/utils/log.js CHANGED
@@ -1,4 +1,22 @@
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; // Duplicate reference found, discard key
9
+ cache.push(value); // Store value in our collection
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'];
@@ -41,10 +59,16 @@ export default function log(level, args, context) {
41
59
  color = colors[6]; // object color
42
60
  const constructorName = args[i].constructor.name;
43
61
  message += ` ${color(`${constructorName}(`)}`
44
- if(constructorName === 'Error'){
45
- message += color(JSON.stringify(args[i].message, null, 2))
62
+ try {
63
+ if(constructorName === 'Error'){
64
+ message += color(safeStringify(args[i].message, 3, 4))
65
+ }
66
+ message += color(safeStringify(args[i], 3, 4))
67
+ } catch (err) {
68
+ // Probably a circular reference
69
+ message += color(args[i].toString())
46
70
  }
47
- message += color(JSON.stringify(args[i], null, 2))
71
+
48
72
  message += ` ${color(")")}`
49
73
  break;
50
74
  case 'string':