@vvlad1973/simple-logger 2.2.1 → 2.3.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.
@@ -30,8 +30,40 @@ export declare function extractContext(args: unknown[]): Record<string, unknown>
30
30
  export declare function colorizeLevel(level: LoggerLevel, label: string): string;
31
31
  /**
32
32
  * Prepares the log call by rearranging the provided arguments into a standardized format.
33
+ * For external loggers (like pino), ensures context object comes BEFORE message string.
33
34
  *
34
35
  * @param {unknown[]} args - The array of arguments to prepare for the log call.
35
36
  * @return {PreparedLogCall} The prepared log call arguments, or null if the input is invalid.
36
37
  */
37
38
  export declare function prepareLogCall(args: unknown[]): PreparedLogCall;
39
+ /**
40
+ * Safely wraps a value for logging to ensure it's a proper context object
41
+ *
42
+ * Use this helper ONLY for single values of unknown type that may be:
43
+ * - undefined/null
44
+ * - primitives (string, number, boolean)
45
+ * - Error objects (will be wrapped as an object with err property)
46
+ *
47
+ * DO NOT use for:
48
+ * - Composite objects with multiple fields (they're already safe)
49
+ * - Objects you construct yourself (use them directly)
50
+ *
51
+ * @param value - Value to wrap (can be Error, primitive, undefined, or object)
52
+ * @returns Safe object for logging or undefined if value is nullish
53
+ *
54
+ * @example
55
+ * ```typescript
56
+ * // Use for unknown single values
57
+ * catch (error) {
58
+ * logger.error(safeLogContext(error), 'Failed');
59
+ * }
60
+ *
61
+ * catch (reason) {
62
+ * logger.error(safeLogContext(reason), 'Rejected');
63
+ * }
64
+ *
65
+ * // DO NOT use for composite objects
66
+ * logger.error({ err, userId }, 'Failed'); // Direct, no helper needed
67
+ * ```
68
+ */
69
+ export declare function safeLogContext(value: unknown): Record<string, unknown>;
@@ -76,19 +76,81 @@ export function colorizeLevel(level, label) {
76
76
  }
77
77
  /**
78
78
  * Prepares the log call by rearranging the provided arguments into a standardized format.
79
+ * For external loggers (like pino), ensures context object comes BEFORE message string.
79
80
  *
80
81
  * @param {unknown[]} args - The array of arguments to prepare for the log call.
81
82
  * @return {PreparedLogCall} The prepared log call arguments, or null if the input is invalid.
82
83
  */
83
84
  export function prepareLogCall(args) {
84
- if (typeof args[0] === 'string') {
85
- return [args[0], ...args.slice(1)];
86
- }
85
+ // Case 1: First arg is object (context) - standard pino format
87
86
  if (typeof args[0] === 'object' && args[0] !== null) {
88
87
  const msg = typeof args[1] === 'string' ? args[1] : undefined;
89
88
  const rest = args.slice(msg ? 2 : 1);
90
89
  return [args[0], msg, ...rest];
91
90
  }
91
+ // Case 2: First arg is string (message)
92
+ if (typeof args[0] === 'string') {
93
+ const msg = args[0];
94
+ // Check if second arg is context object - if so, swap them for pino
95
+ if (typeof args[1] === 'object' && args[1] !== null && !Array.isArray(args[1])) {
96
+ const context = args[1];
97
+ const rest = args.slice(2);
98
+ return [context, msg, ...rest];
99
+ }
100
+ // Otherwise keep as is (message only or message with non-object args)
101
+ return [msg, ...args.slice(1)];
102
+ }
92
103
  return null;
93
104
  }
105
+ /**
106
+ * Safely wraps a value for logging to ensure it's a proper context object
107
+ *
108
+ * Use this helper ONLY for single values of unknown type that may be:
109
+ * - undefined/null
110
+ * - primitives (string, number, boolean)
111
+ * - Error objects (will be wrapped as an object with err property)
112
+ *
113
+ * DO NOT use for:
114
+ * - Composite objects with multiple fields (they're already safe)
115
+ * - Objects you construct yourself (use them directly)
116
+ *
117
+ * @param value - Value to wrap (can be Error, primitive, undefined, or object)
118
+ * @returns Safe object for logging or undefined if value is nullish
119
+ *
120
+ * @example
121
+ * ```typescript
122
+ * // Use for unknown single values
123
+ * catch (error) {
124
+ * logger.error(safeLogContext(error), 'Failed');
125
+ * }
126
+ *
127
+ * catch (reason) {
128
+ * logger.error(safeLogContext(reason), 'Rejected');
129
+ * }
130
+ *
131
+ * // DO NOT use for composite objects
132
+ * logger.error({ err, userId }, 'Failed'); // Direct, no helper needed
133
+ * ```
134
+ */
135
+ export function safeLogContext(value) {
136
+ // Nullish values - return empty record
137
+ if (value === null || value === undefined) {
138
+ return {};
139
+ }
140
+ // Error objects - wrap in standard "err" field
141
+ if (value instanceof Error) {
142
+ return { err: value };
143
+ }
144
+ // Already an object (not array) - validate it's a plain object
145
+ if (typeof value === 'object' && !Array.isArray(value)) {
146
+ // Use type guard to safely return the object
147
+ if (isPlainObject(value)) {
148
+ return value;
149
+ }
150
+ // For non-plain objects (class instances, etc), wrap them
151
+ return { value };
152
+ }
153
+ // Primitives or arrays - wrap in "value" field
154
+ return { value };
155
+ }
94
156
  //# sourceMappingURL=helpers.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"helpers.js","sourceRoot":"","sources":["../../src/helpers/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,MAAM,OAAO,CAAC;AAE1B,OAAO,EAAE,YAAY,EAAE,MAAM,2BAA2B,CAAC;AAEzD;;;;;;;;;GASG;AACH,MAAM,UAAU,cAAc,CAAC,IAAe;IAI5C,MAAM,CAAC,KAAK,EAAE,MAAM,EAAE,GAAG,IAAI,CAAC,GAAG,IAAI,CAAC;IAEtC,IACE,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,OAAO,MAAM,KAAK,QAAQ,EAC1B,CAAC;QACD,OAAO,EAAE,GAAG,EAAE,MAAM,EAAE,IAAI,EAAE,CAAC;IAC/B,CAAC;IAED,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO;YACL,GAAG,EAAE,KAAK;YACV,IAAI,EAAE,CAAC,MAAM,EAAE,GAAG,IAAI,CAAC,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,KAAK,KAAK,SAAS,CAAC;SAC/D,CAAC;IACJ,CAAC;IAED,OAAO,EAAE,GAAG,EAAE,EAAE,EAAE,IAAI,EAAE,EAAE,EAAE,CAAC;AAC/B,CAAC;AAED;;;;;GAKG;AACH,SAAS,aAAa,CAAC,KAAc;IACnC,OAAO,CACL,OAAO,KAAK,KAAK,QAAQ;QACzB,KAAK,KAAK,IAAI;QACd,CAAC,KAAK,CAA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AE,CAAC;QAC1C,OAAO,EAA6B,CAAC;IACvC,CAAC;IAED,+CAA+C;IAC/C,IAAI,KAAK,YAAY,KAAK,EAAE,CAAC;QAC3B,OAAO,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC;IACxB,CAAC;IAED,+DAA+D;IAC/D,IAAI,OAAO,KAAK,KAAK,QAAQ,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,KAAK,CAAC,EAAE,CAAC;QACvD,6CAA6C;QAC7C,IAAI,aAAa,CAAC,KAAK,CAAC,EAAE,CAAC;YACzB,OAAO,KAAK,CAAC;QACf,CAAC;QACD,0DAA0D;QAC1D,OAAO,EAAE,KAAK,EAAE,CAAC;IACnB,CAAC;IAED,+CAA+C;IAC/C,OAAO,EAAE,KAAK,EAAE,CAAC;AACnB,CAAC"}
package/dist/index.d.ts CHANGED
@@ -3,3 +3,4 @@ export default SimpleLogger;
3
3
  export { SimpleLogger };
4
4
  export type * from './types/simple-logger.types.js';
5
5
  export * from './constants/constants.js';
6
+ export * from './helpers/helpers.js';
package/dist/index.js CHANGED
@@ -2,4 +2,5 @@ import SimpleLogger from './classes/simple-logger.js';
2
2
  export default SimpleLogger;
3
3
  export { SimpleLogger };
4
4
  export * from './constants/constants.js';
5
+ export * from './helpers/helpers.js';
5
6
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,4BAA4B,CAAC;AAEtD,eAAe,YAAY,CAAC;AAC5B,OAAO,EAAE,YAAY,EAAE,CAAC;AAGxB,cAAc,0BAA0B,CAAC"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,YAAY,MAAM,4BAA4B,CAAC;AAEtD,eAAe,YAAY,CAAC;AAC5B,OAAO,EAAE,YAAY,EAAE,CAAC;AAGxB,cAAc,0BAA0B,CAAC;AACzC,cAAc,sBAAsB,CAAA"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@vvlad1973/simple-logger",
3
- "version": "2.2.1",
3
+ "version": "2.3.0",
4
4
  "main": "dist/index.js",
5
5
  "types": "dist/index.d.ts",
6
6
  "type": "module",
@@ -92,19 +92,86 @@ export function colorizeLevel(level: LoggerLevel, label: string): string {
92
92
 
93
93
  /**
94
94
  * Prepares the log call by rearranging the provided arguments into a standardized format.
95
+ * For external loggers (like pino), ensures context object comes BEFORE message string.
95
96
  *
96
97
  * @param {unknown[]} args - The array of arguments to prepare for the log call.
97
98
  * @return {PreparedLogCall} The prepared log call arguments, or null if the input is invalid.
98
99
  */
99
100
  export function prepareLogCall(args: unknown[]): PreparedLogCall {
100
- if (typeof args[0] === 'string') {
101
- return [args[0], ...args.slice(1)];
102
- }
101
+ // Case 1: First arg is object (context) - standard pino format
103
102
  if (typeof args[0] === 'object' && args[0] !== null) {
104
103
  const msg = typeof args[1] === 'string' ? args[1] : undefined;
105
104
  const rest = args.slice(msg ? 2 : 1);
106
105
  return [args[0], msg, ...rest];
107
106
  }
107
+
108
+ // Case 2: First arg is string (message)
109
+ if (typeof args[0] === 'string') {
110
+ const msg = args[0];
111
+ // Check if second arg is context object - if so, swap them for pino
112
+ if (typeof args[1] === 'object' && args[1] !== null && !Array.isArray(args[1])) {
113
+ const context = args[1];
114
+ const rest = args.slice(2);
115
+ return [context, msg, ...rest];
116
+ }
117
+ // Otherwise keep as is (message only or message with non-object args)
118
+ return [msg, ...args.slice(1)];
119
+ }
120
+
108
121
  return null;
109
122
  }
110
123
 
124
+ /**
125
+ * Safely wraps a value for logging to ensure it's a proper context object
126
+ *
127
+ * Use this helper ONLY for single values of unknown type that may be:
128
+ * - undefined/null
129
+ * - primitives (string, number, boolean)
130
+ * - Error objects (will be wrapped as an object with err property)
131
+ *
132
+ * DO NOT use for:
133
+ * - Composite objects with multiple fields (they're already safe)
134
+ * - Objects you construct yourself (use them directly)
135
+ *
136
+ * @param value - Value to wrap (can be Error, primitive, undefined, or object)
137
+ * @returns Safe object for logging or undefined if value is nullish
138
+ *
139
+ * @example
140
+ * ```typescript
141
+ * // Use for unknown single values
142
+ * catch (error) {
143
+ * logger.error(safeLogContext(error), 'Failed');
144
+ * }
145
+ *
146
+ * catch (reason) {
147
+ * logger.error(safeLogContext(reason), 'Rejected');
148
+ * }
149
+ *
150
+ * // DO NOT use for composite objects
151
+ * logger.error({ err, userId }, 'Failed'); // Direct, no helper needed
152
+ * ```
153
+ */
154
+ export function safeLogContext(value: unknown): Record<string, unknown> {
155
+ // Nullish values - return empty record
156
+ if (value === null || value === undefined) {
157
+ return {} as Record<string, unknown>;
158
+ }
159
+
160
+ // Error objects - wrap in standard "err" field
161
+ if (value instanceof Error) {
162
+ return { err: value };
163
+ }
164
+
165
+ // Already an object (not array) - validate it's a plain object
166
+ if (typeof value === 'object' && !Array.isArray(value)) {
167
+ // Use type guard to safely return the object
168
+ if (isPlainObject(value)) {
169
+ return value;
170
+ }
171
+ // For non-plain objects (class instances, etc), wrap them
172
+ return { value };
173
+ }
174
+
175
+ // Primitives or arrays - wrap in "value" field
176
+ return { value };
177
+ }
package/src/index.ts CHANGED
@@ -4,4 +4,5 @@ export default SimpleLogger;
4
4
  export { SimpleLogger };
5
5
 
6
6
  export type * from './types/simple-logger.types.js';
7
- export * from './constants/constants.js';
7
+ export * from './constants/constants.js';
8
+ export * from './helpers/helpers.js'