@unchainedshop/logger 5.0.0-alpha.2 → 5.0.0-alpha.3

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.
@@ -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;
@@ -1,5 +1,5 @@
1
1
  import { inspect } from 'node:util';
2
- import { stringify } from 'safe-stable-stringify';
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
- const bigintReplacer = (_key, value) => {
91
- if (typeof value === 'bigint') {
92
- return value.toString();
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
- console.log(stringify(logObject, bigintReplacer));
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();
@@ -1,3 +1,4 @@
1
1
  export * from './createLogger.ts';
2
+ export * from './safe-stringify.ts';
2
3
  export * from './logger.types.ts';
3
4
  export * from './log.ts';
@@ -1,3 +1,4 @@
1
1
  export * from "./createLogger.js";
2
+ export * from "./safe-stringify.js";
2
3
  export * from "./logger.types.js";
3
4
  export * from "./log.js";
@@ -0,0 +1,2 @@
1
+ export declare function safeStringify(value: any, replacer?: ((this: any, key: string, value: any) => any) | null, space?: string | number): string | undefined;
2
+ export default safeStringify;
@@ -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.2",
4
+ "version": "5.0.0-alpha.3",
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": "^25.0.0",
40
+ "@types/node": "^26.2.0",
44
41
  "typescript": "^5.8.3"
45
42
  }
46
43
  }