@naturalcycles/js-lib 15.91.0 → 15.92.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.
@@ -34,7 +34,12 @@ export interface LogEntry extends LogData {
34
34
  level: CommonLogLevel;
35
35
  }
36
36
  export type LogSink = (entry: LogEntry) => void;
37
- export type CommonLogFunction = (input: LogInput) => void;
37
+ /**
38
+ * A log call is either one `LogInput`, or a message with the other fields:
39
+ * `logger.log('hey')`, `logger.error(err)`, `logger.warn({ msg, err })`, `logger.warn('failed', { err })`.
40
+ */
41
+ export type CommonLogArgs = [input: LogInput] | [msg: string, data: LogData];
42
+ export type CommonLogFunction = (...args: CommonLogArgs) => void;
38
43
  /**
39
44
  * @experimental
40
45
  */
@@ -12,18 +12,18 @@ export const commonLogLevelNumber = {
12
12
  * after the logger is created (e.g a request context whose user is resolved later).
13
13
  */
14
14
  export function commonLoggerCreate(sink, context) {
15
- const emit = (level, input) => sink({ ...context?.(), ...toLogData(input), level });
15
+ const emit = (level, [input, data]) => sink({ ...context?.(), ...toLogData(input, data), level });
16
16
  return {
17
- debug: input => emit('debug', input),
18
- log: input => emit('log', input),
19
- warn: input => emit('warn', input),
20
- error: input => emit('error', input),
17
+ debug: (...args) => emit('debug', args),
18
+ log: (...args) => emit('log', args),
19
+ warn: (...args) => emit('warn', args),
20
+ error: (...args) => emit('error', args),
21
21
  child: childContext => commonLoggerCreate(sink, () => ({ ...context?.(), ...childContext })),
22
22
  };
23
23
  }
24
- function toLogData(input) {
24
+ function toLogData(input, data) {
25
25
  if (typeof input === 'string')
26
- return { msg: input };
26
+ return { ...data, msg: input };
27
27
  if (input instanceof Error)
28
28
  return { err: input };
29
29
  return input;
@@ -62,9 +62,9 @@ export function createCommonLoggerAtLevel(logger = consoleLogger, minLevel = 'lo
62
62
  }
63
63
  return {
64
64
  debug: _noop, // otherwise it is "log everything" logger (same logger as input)
65
- log: level <= commonLogLevelNumber['log'] ? input => logger.log(input) : _noop,
66
- warn: level <= commonLogLevelNumber['warn'] ? input => logger.warn(input) : _noop,
67
- error: input => logger.error(input),
65
+ log: level <= commonLogLevelNumber['log'] ? (...args) => logger.log(...args) : _noop,
66
+ warn: level <= commonLogLevelNumber['warn'] ? (...args) => logger.warn(...args) : _noop,
67
+ error: (...args) => logger.error(...args),
68
68
  child: context => createCommonLoggerAtLevel(logger.child(context), minLevel),
69
69
  };
70
70
  }
@@ -73,10 +73,10 @@ export function createCommonLoggerAtLevel(logger = consoleLogger, minLevel = 'lo
73
73
  */
74
74
  export function commonLoggerPipe(loggers) {
75
75
  return {
76
- debug: input => loggers.forEach(logger => logger.debug(input)),
77
- log: input => loggers.forEach(logger => logger.log(input)),
78
- warn: input => loggers.forEach(logger => logger.warn(input)),
79
- error: input => loggers.forEach(logger => logger.error(input)),
76
+ debug: (...args) => loggers.forEach(logger => logger.debug(...args)),
77
+ log: (...args) => loggers.forEach(logger => logger.log(...args)),
78
+ warn: (...args) => loggers.forEach(logger => logger.warn(...args)),
79
+ error: (...args) => loggers.forEach(logger => logger.error(...args)),
80
80
  child: context => commonLoggerPipe(loggers.map(logger => logger.child(context))),
81
81
  };
82
82
  }
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/js-lib",
3
3
  "type": "module",
4
- "version": "15.91.0",
4
+ "version": "15.92.0",
5
5
  "dependencies": {
6
6
  "tslib": "^2"
7
7
  },
@@ -14,7 +14,7 @@
14
14
  }
15
15
  },
16
16
  "devDependencies": {
17
- "@naturalcycles/dev-lib": "20.64.0",
17
+ "@naturalcycles/dev-lib": "0.0.0",
18
18
  "@types/node": "^26",
19
19
  "@types/semver": "^7",
20
20
  "typescript": "^7",
@@ -48,7 +48,13 @@ export interface LogEntry extends LogData {
48
48
 
49
49
  export type LogSink = (entry: LogEntry) => void
50
50
 
51
- export type CommonLogFunction = (input: LogInput) => void
51
+ /**
52
+ * A log call is either one `LogInput`, or a message with the other fields:
53
+ * `logger.log('hey')`, `logger.error(err)`, `logger.warn({ msg, err })`, `logger.warn('failed', { err })`.
54
+ */
55
+ export type CommonLogArgs = [input: LogInput] | [msg: string, data: LogData]
56
+
57
+ export type CommonLogFunction = (...args: CommonLogArgs) => void
52
58
 
53
59
  /**
54
60
  * @experimental
@@ -70,20 +76,20 @@ export interface CommonLogger {
70
76
  * after the logger is created (e.g a request context whose user is resolved later).
71
77
  */
72
78
  export function commonLoggerCreate(sink: LogSink, context?: () => LogData): CommonLogger {
73
- const emit = (level: CommonLogLevel, input: LogInput): void =>
74
- sink({ ...context?.(), ...toLogData(input), level })
79
+ const emit = (level: CommonLogLevel, [input, data]: CommonLogArgs): void =>
80
+ sink({ ...context?.(), ...toLogData(input, data), level })
75
81
 
76
82
  return {
77
- debug: input => emit('debug', input),
78
- log: input => emit('log', input),
79
- warn: input => emit('warn', input),
80
- error: input => emit('error', input),
83
+ debug: (...args) => emit('debug', args),
84
+ log: (...args) => emit('log', args),
85
+ warn: (...args) => emit('warn', args),
86
+ error: (...args) => emit('error', args),
81
87
  child: childContext => commonLoggerCreate(sink, () => ({ ...context?.(), ...childContext })),
82
88
  }
83
89
  }
84
90
 
85
- function toLogData(input: LogInput): LogData {
86
- if (typeof input === 'string') return { msg: input }
91
+ function toLogData(input: LogInput, data?: LogData): LogData {
92
+ if (typeof input === 'string') return { ...data, msg: input }
87
93
  if (input instanceof Error) return { err: input }
88
94
  return input
89
95
  }
@@ -126,9 +132,9 @@ export function createCommonLoggerAtLevel(
126
132
 
127
133
  return {
128
134
  debug: _noop, // otherwise it is "log everything" logger (same logger as input)
129
- log: level <= commonLogLevelNumber['log'] ? input => logger.log(input) : _noop,
130
- warn: level <= commonLogLevelNumber['warn'] ? input => logger.warn(input) : _noop,
131
- error: input => logger.error(input),
135
+ log: level <= commonLogLevelNumber['log'] ? (...args) => logger.log(...args) : _noop,
136
+ warn: level <= commonLogLevelNumber['warn'] ? (...args) => logger.warn(...args) : _noop,
137
+ error: (...args) => logger.error(...args),
132
138
  child: context => createCommonLoggerAtLevel(logger.child(context), minLevel),
133
139
  }
134
140
  }
@@ -138,10 +144,10 @@ export function createCommonLoggerAtLevel(
138
144
  */
139
145
  export function commonLoggerPipe(loggers: CommonLogger[]): CommonLogger {
140
146
  return {
141
- debug: input => loggers.forEach(logger => logger.debug(input)),
142
- log: input => loggers.forEach(logger => logger.log(input)),
143
- warn: input => loggers.forEach(logger => logger.warn(input)),
144
- error: input => loggers.forEach(logger => logger.error(input)),
147
+ debug: (...args) => loggers.forEach(logger => logger.debug(...args)),
148
+ log: (...args) => loggers.forEach(logger => logger.log(...args)),
149
+ warn: (...args) => loggers.forEach(logger => logger.warn(...args)),
150
+ error: (...args) => loggers.forEach(logger => logger.error(...args)),
145
151
  child: context => commonLoggerPipe(loggers.map(logger => logger.child(context))),
146
152
  }
147
153
  }