@unchainedshop/logger 4.8.27 → 4.8.28
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 +26 -1
- package/package.json +1 -1
package/lib/createLogger.d.ts
CHANGED
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
export type LogContextProvider = () => Record<string, unknown> | undefined | null;
|
|
2
|
+
export declare const setLogContextProvider: (provider: LogContextProvider | null) => void;
|
|
1
3
|
export interface Logger {
|
|
2
4
|
trace: (message: any, ...args: any[]) => void;
|
|
3
5
|
debug: (message: any, ...args: any[]) => void;
|
package/lib/createLogger.js
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
1
|
import { inspect } from 'node:util';
|
|
2
2
|
import { safeStringify } from "./safe-stringify.js";
|
|
3
3
|
import { LogLevel } from "./logger.types.js";
|
|
4
|
+
let currentContextProvider = null;
|
|
5
|
+
export const setLogContextProvider = (provider) => {
|
|
6
|
+
currentContextProvider = provider;
|
|
7
|
+
};
|
|
8
|
+
const RESERVED_KEYS = new Set(['timestamp', 'level', 'name', 'message']);
|
|
9
|
+
const UNSAFE_KEYS = new Set(['__proto__', 'constructor', 'prototype']);
|
|
4
10
|
const regexCache = new Map();
|
|
5
11
|
const debugPatternCache = new Map();
|
|
6
12
|
const escapeRegexForDebug = (pattern) => {
|
|
@@ -90,6 +96,7 @@ const formatTimestamp = () => {
|
|
|
90
96
|
export const resetLoggerInitialization = () => {
|
|
91
97
|
regexCache.clear();
|
|
92
98
|
debugPatternCache.clear();
|
|
99
|
+
currentContextProvider = null;
|
|
93
100
|
};
|
|
94
101
|
export const createLogger = (moduleName) => {
|
|
95
102
|
const { DEBUG = '', LOG_LEVEL = LogLevel.Info, UNCHAINED_LOG_FORMAT = 'unchained' } = process.env;
|
|
@@ -117,9 +124,27 @@ export const createLogger = (moduleName) => {
|
|
|
117
124
|
name: moduleName,
|
|
118
125
|
message: typeof message === 'string' ? message : message,
|
|
119
126
|
};
|
|
127
|
+
if (currentContextProvider) {
|
|
128
|
+
let context;
|
|
129
|
+
try {
|
|
130
|
+
context = currentContextProvider();
|
|
131
|
+
}
|
|
132
|
+
catch {
|
|
133
|
+
context = undefined;
|
|
134
|
+
}
|
|
135
|
+
if (context && typeof context === 'object') {
|
|
136
|
+
for (const key in context) {
|
|
137
|
+
if (UNSAFE_KEYS.has(key) || RESERVED_KEYS.has(key))
|
|
138
|
+
continue;
|
|
139
|
+
if (key in logObject)
|
|
140
|
+
continue;
|
|
141
|
+
logObject[key] = context[key];
|
|
142
|
+
}
|
|
143
|
+
}
|
|
144
|
+
}
|
|
120
145
|
if (args.length > 0 && typeof args[0] === 'object' && args[0] !== null) {
|
|
121
146
|
for (const key in args[0]) {
|
|
122
|
-
if (key
|
|
147
|
+
if (UNSAFE_KEYS.has(key))
|
|
123
148
|
continue;
|
|
124
149
|
logObject[key] = args[0][key];
|
|
125
150
|
}
|
package/package.json
CHANGED