@unchainedshop/logger 5.0.0-alpha.2 → 5.0.0-alpha.4
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/lib/createLogger.d.ts +2 -0
- package/lib/createLogger.js +20 -7
- package/lib/logger-index.d.ts +1 -0
- package/lib/logger-index.js +1 -0
- package/lib/safe-stringify.d.ts +2 -0
- package/lib/safe-stringify.js +19 -0
- package/package.json +3 -6
package/lib/createLogger.d.ts
CHANGED
|
@@ -5,5 +5,7 @@ export interface Logger {
|
|
|
5
5
|
warn: (message: any, ...args: any[]) => void;
|
|
6
6
|
error: (message: any, ...args: any[]) => void;
|
|
7
7
|
}
|
|
8
|
+
export type LogContextProvider = () => Record<string, unknown> | undefined;
|
|
9
|
+
export declare const setLogContextProvider: (provider: LogContextProvider | undefined) => void;
|
|
8
10
|
export declare const resetLoggerInitialization: () => void;
|
|
9
11
|
export declare const createLogger: (moduleName: string) => Logger;
|
package/lib/createLogger.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { inspect } from 'node:util';
|
|
2
|
-
import {
|
|
2
|
+
import { safeStringify } from "./safe-stringify.js";
|
|
3
3
|
import { LogLevel } from "./logger.types.js";
|
|
4
4
|
const regexCache = new Map();
|
|
5
5
|
const debugPatternCache = new Map();
|
|
@@ -87,15 +87,14 @@ const formatTimestamp = () => {
|
|
|
87
87
|
const seconds = now.getSeconds().toString().padStart(2, '0');
|
|
88
88
|
return `${hours}:${minutes}:${seconds}`;
|
|
89
89
|
};
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
}
|
|
94
|
-
return value;
|
|
90
|
+
let logContextProvider;
|
|
91
|
+
export const setLogContextProvider = (provider) => {
|
|
92
|
+
logContextProvider = provider;
|
|
95
93
|
};
|
|
96
94
|
export const resetLoggerInitialization = () => {
|
|
97
95
|
regexCache.clear();
|
|
98
96
|
debugPatternCache.clear();
|
|
97
|
+
logContextProvider = undefined;
|
|
99
98
|
};
|
|
100
99
|
export const createLogger = (moduleName) => {
|
|
101
100
|
const { DEBUG = '', LOG_LEVEL = LogLevel.Info, UNCHAINED_LOG_FORMAT = 'unchained' } = process.env;
|
|
@@ -130,7 +129,21 @@ export const createLogger = (moduleName) => {
|
|
|
130
129
|
logObject[key] = args[0][key];
|
|
131
130
|
}
|
|
132
131
|
}
|
|
133
|
-
|
|
132
|
+
if (logContextProvider) {
|
|
133
|
+
try {
|
|
134
|
+
const extra = logContextProvider();
|
|
135
|
+
if (extra) {
|
|
136
|
+
for (const key in extra) {
|
|
137
|
+
if (key === '__proto__' || key === 'constructor' || key === 'prototype')
|
|
138
|
+
continue;
|
|
139
|
+
logObject[key] = extra[key];
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
catch {
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
console.log(safeStringify(logObject));
|
|
134
147
|
}
|
|
135
148
|
else {
|
|
136
149
|
const timestamp = formatTimestamp();
|
package/lib/logger-index.d.ts
CHANGED
package/lib/logger-index.js
CHANGED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
export function safeStringify(value, replacer, space) {
|
|
2
|
+
const ancestors = [];
|
|
3
|
+
return JSON.stringify(value, function (key, val) {
|
|
4
|
+
const replaced = replacer ? replacer.call(this, key, val) : val;
|
|
5
|
+
if (typeof replaced === 'bigint')
|
|
6
|
+
return replaced.toString();
|
|
7
|
+
if (typeof replaced !== 'object' || replaced === null)
|
|
8
|
+
return replaced;
|
|
9
|
+
if (replaced instanceof BigInt)
|
|
10
|
+
return String(replaced);
|
|
11
|
+
while (ancestors.length > 0 && ancestors[ancestors.length - 1] !== this)
|
|
12
|
+
ancestors.pop();
|
|
13
|
+
if (ancestors.includes(replaced))
|
|
14
|
+
return '[Circular]';
|
|
15
|
+
ancestors.push(replaced);
|
|
16
|
+
return replaced;
|
|
17
|
+
}, space);
|
|
18
|
+
}
|
|
19
|
+
export default safeStringify;
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@unchainedshop/logger",
|
|
3
3
|
"description": "High-performance logging package for the Unchained Engine",
|
|
4
|
-
"version": "5.0.0-alpha.
|
|
4
|
+
"version": "5.0.0-alpha.4",
|
|
5
5
|
"main": "lib/logger-index.js",
|
|
6
6
|
"types": "lib/logger-index.d.ts",
|
|
7
7
|
"type": "module",
|
|
@@ -36,11 +36,8 @@
|
|
|
36
36
|
"url": "https://github.com/unchainedshop/unchained/issues"
|
|
37
37
|
},
|
|
38
38
|
"homepage": "https://github.com/unchainedshop/unchained#readme",
|
|
39
|
-
"dependencies": {
|
|
40
|
-
"safe-stable-stringify": "^2.5.0"
|
|
41
|
-
},
|
|
42
39
|
"devDependencies": {
|
|
43
|
-
"@types/node": "^
|
|
44
|
-
"typescript": "^
|
|
40
|
+
"@types/node": "^26.2.0",
|
|
41
|
+
"typescript": "^6.0.3"
|
|
45
42
|
}
|
|
46
43
|
}
|