@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.
- package/dist/log/commonLogger.d.ts +6 -1
- package/dist/log/commonLogger.js +14 -14
- package/package.json +2 -2
- package/src/log/commonLogger.ts +22 -16
|
@@ -34,7 +34,12 @@ export interface LogEntry extends LogData {
|
|
|
34
34
|
level: CommonLogLevel;
|
|
35
35
|
}
|
|
36
36
|
export type LogSink = (entry: LogEntry) => void;
|
|
37
|
-
|
|
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
|
*/
|
package/dist/log/commonLogger.js
CHANGED
|
@@ -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:
|
|
18
|
-
log:
|
|
19
|
-
warn:
|
|
20
|
-
error:
|
|
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'] ?
|
|
66
|
-
warn: level <= commonLogLevelNumber['warn'] ?
|
|
67
|
-
error:
|
|
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:
|
|
77
|
-
log:
|
|
78
|
-
warn:
|
|
79
|
-
error:
|
|
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.
|
|
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": "
|
|
17
|
+
"@naturalcycles/dev-lib": "0.0.0",
|
|
18
18
|
"@types/node": "^26",
|
|
19
19
|
"@types/semver": "^7",
|
|
20
20
|
"typescript": "^7",
|
package/src/log/commonLogger.ts
CHANGED
|
@@ -48,7 +48,13 @@ export interface LogEntry extends LogData {
|
|
|
48
48
|
|
|
49
49
|
export type LogSink = (entry: LogEntry) => void
|
|
50
50
|
|
|
51
|
-
|
|
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:
|
|
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:
|
|
78
|
-
log:
|
|
79
|
-
warn:
|
|
80
|
-
error:
|
|
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'] ?
|
|
130
|
-
warn: level <= commonLogLevelNumber['warn'] ?
|
|
131
|
-
error:
|
|
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:
|
|
142
|
-
log:
|
|
143
|
-
warn:
|
|
144
|
-
error:
|
|
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
|
}
|