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