-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathProcLog.js
More file actions
93 lines (86 loc) · 2.58 KB
/
ProcLog.js
File metadata and controls
93 lines (86 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import { LogBase } from './LogBase.js'
import { Log } from './node.js'
import { inspectOpts, inspectNamespaces, INFO } from './utils.js'
/**
* @typedef {import('./node.js').LogOptions} LogOptions
*/
/**
* @typedef {LogOptions & {Log: typeof Log}} LogOptionsWithCustomLog
*/
export const EVENT_NAME = 'log-level'
const defaultOptions = {
level: INFO,
namespaces: undefined
}
/**
* Decouple logging via process event 'log'. This allows to use a different
* logger framework than 'debug-level'. In such cases you'd need to adapt your
* framework of choice for logging. Check `initProcLog()` for inspiration.
*
* Emits the following process event:
* ```
* process.emit('log', level, name, fmt, args)
* ```
* where
* - `level` is TRACE, DEBUG, INFO, WARN, ERROR, FATAL, LOG
* - `name` is namespace of the logger
* - `fmt` is optional formatter, e.g. `%s`
* - `args` is an array of arguments passed to the logger
*
* @example
* ```js
* import { ProcLog, initProcLog } from 'debug-level'
*
* // initialize process event logging with 'debug-level'
* // define here serializer, stream options, etc.
* initProcLog({ serializers: {...}, Log: LogEcs })
*
* // add a logger with a namespace
* // use options only for defining the logLevel (or leave undefined to control
* // via env-vars)
* const log = new ProcLog('app:namespace')
* // add some logging
* log.info('show some logging')
* ```
*/
export class ProcLog extends LogBase {
/**
* creates a new logger
* @param {String} name - namespace of Logger
* @param {LogOptions} [opts] - see Log.options
*/
constructor(name, opts) {
const _opts = {
...defaultOptions,
...inspectOpts(process.env),
...inspectNamespaces(process.env),
...opts,
// disallow numbers in event
levelNumbers: false,
// don't use serializers, define them in the initProcLog options
serializers: {}
}
super(name, _opts)
}
_log(level, fmt, args) {
// @ts-expect-error
process.emit(EVENT_NAME, level, this.name, fmt, args)
}
}
/**
* logging via process event 'log'
* @param {LogOptionsWithCustomLog} [options]
*/
export function initProcLog(options) {
const LogCls = options?.Log || Log
const logger = {}
const getLogger = (namespace) =>
logger[namespace] || (logger[namespace] = new LogCls(namespace, options))
// prevent multiple log-lines from adding more than one listener
process.removeAllListeners(EVENT_NAME)
// listen on event
process.on(EVENT_NAME, (level, namespace, fmt, args) => {
const log = getLogger(namespace)
log[level.toLowerCase()]?.(fmt, ...args)
})
}