Skip to content

Commit 6a4a086

Browse files
authored
Merge pull request #30 from commenthol/feat-logecs-wraps
feat: static methods wrapConsole, wrapDebug on LogEcs, ProcLog
2 parents a46d4f5 + 4f032a8 commit 6a4a086

File tree

10 files changed

+259
-100
lines changed

10 files changed

+259
-100
lines changed

src/ProcLog.js

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,14 @@
11
import { LogBase } from './LogBase.js'
22
import { Log } from './node.js'
33
import { inspectOpts, inspectNamespaces, INFO } from './utils.js'
4+
import { wrapConsole } from './wrapConsole.js'
5+
import { wrapDebug } from './wrapDebug.js'
46

57
/** @typedef {import('./utils.js').Level} Level */
68
/** @typedef {import('./node.js').LogOptions} LogOptions */
9+
/** @typedef {import('./node.js').LogOptionWrapConsole} LogOptionWrapConsole */
710
/** @typedef {LogOptions & {Log: typeof Log}} LogOptionsWithCustomLog */
11+
812
/**
913
* @typedef {object} ProcLogOptions
1014
* @property {Level} [level] log level
@@ -13,7 +17,7 @@ import { inspectOpts, inspectNamespaces, INFO } from './utils.js'
1317

1418
export const EVENT_PROC_LOG = 'log-level'
1519

16-
const defaultOptions = {
20+
const options = {
1721
level: INFO,
1822
namespaces: undefined
1923
}
@@ -57,7 +61,7 @@ export class ProcLog extends LogBase {
5761
*/
5862
constructor(name, opts) {
5963
const _opts = {
60-
...defaultOptions,
64+
...options,
6165
...inspectOpts(process.env),
6266
...inspectNamespaces(process.env),
6367
...opts,
@@ -73,6 +77,24 @@ export class ProcLog extends LogBase {
7377
// @ts-expect-error
7478
process.emit(EVENT_PROC_LOG, level, this.name, fmt, args)
7579
}
80+
81+
/**
82+
* @param {string} [name]
83+
* @param {ProcLogOptions & LogOptionWrapConsole} [opts]
84+
* @returns {() => void} unwrap functions
85+
*/
86+
static wrapConsole(name = 'console', opts) {
87+
const log = new ProcLog(name, opts)
88+
return wrapConsole(log, opts)
89+
}
90+
91+
/**
92+
* @param {ProcLogOptions} [opts]
93+
* @returns {() => void} unwrap functions
94+
*/
95+
static wrapDebug(opts) {
96+
return wrapDebug(ProcLog, opts)
97+
}
7698
}
7799

78100
/**

src/ecs/LogEcs.js

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
11
import { Log, stringify } from '../node.js'
22
import { ecsSerializers } from './serializers.js'
3+
import { wrapConsole } from '../wrapConsole.js'
4+
import { wrapDebug } from '../wrapDebug.js'
35

4-
/**
5-
* @typedef {import('../serializers/index.js').Serializer} Serializer
6-
*/
7-
/**
8-
* @typedef {import('../node.js').LogOptions & {serializers: Record<string, Serializer>}} LogOptionsEcs
9-
* @typedef {import('../node.js').LogOptionWrapConsole} LogOptionWrapConsole
10-
*/
6+
/** @typedef {import('../serializers/index.js').Serializer} Serializer */
7+
/** @typedef {import('../node.js').LogOptions & {serializers: Record<string, Serializer>}} LogOptionsEcs */
8+
/** @typedef {import('../node.js').LogOptionWrapConsole} LogOptionWrapConsole */
119

1210
/**
1311
* Elastic Common Schema (ECS) compatible logger;
@@ -33,6 +31,24 @@ export class LogEcs extends Log {
3331
this.toJson = this._toJson
3432
}
3533

34+
/**
35+
* @param {string} [name]
36+
* @param {LogOptionsEcs & LogOptionWrapConsole} [opts]
37+
* @returns {() => void} unwrap function
38+
*/
39+
static wrapConsole(name = 'console', opts) {
40+
const log = new LogEcs(name, opts)
41+
return wrapConsole(log, opts)
42+
}
43+
44+
/**
45+
* @param {LogOptionsEcs} [opts]
46+
* @returns {() => void} unwrap function
47+
*/
48+
static wrapDebug(opts) {
49+
return wrapDebug(LogEcs, opts)
50+
}
51+
3652
/* c8 ignore next 18 */
3753
_applySerializers(obj) {
3854
const ecsObj = {}

src/node.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ export class Log extends LogBase {
9292
options.serializers,
9393
opts ? opts.serializers : {}
9494
)
95-
const _opts = Object.assign({}, options, opts, { serializers })
95+
const _opts = { ...options, ...opts, serializers }
9696
super(name, _opts)
9797

9898
const colorFn = (n) => chalk.hex(n)
@@ -195,10 +195,11 @@ export class Log extends LogBase {
195195
}
196196

197197
/**
198-
* @returns {() => void} unwrap functions
198+
* @param {LogOptions} [opts] - see Log.options
199+
* @returns {() => void} unwrap function
199200
*/
200-
static wrapDebug() {
201-
return wrapDebug(Log)
201+
static wrapDebug(opts) {
202+
return wrapDebug(Log, opts)
202203
}
203204

204205
/**

src/wrapDebug.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const unwrap = () => {
1111
}
1212
}
1313

14-
export function wrapDebug(Log) {
14+
export function wrapDebug(Log, opts) {
1515
if (wrapped) return unwrap
1616
class Loggers {
1717
constructor() {
@@ -21,7 +21,7 @@ export function wrapDebug(Log) {
2121
get(namespace) {
2222
let logger = this.cache[namespace]
2323
if (!logger) {
24-
logger = this.cache[namespace] = new Log(namespace)
24+
logger = this.cache[namespace] = new Log(namespace, opts)
2525
}
2626
return logger
2727
}

test/LogEcs.test.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import assert from 'assert'
22
import os from 'os'
33
import sinon from 'sinon'
4+
import debug from 'debug'
45
import { startTimeKey } from '../src/serializers/res.js'
56
import { LogEcs, ecsSerializers } from '../src/ecs/index.js'
67
import { httpLogs, logger } from '../src/index.js'
@@ -413,4 +414,50 @@ describe('LogEcs', function () {
413414
)
414415
})
415416
})
417+
418+
describe('wrap console', function () {
419+
let unwrap
420+
before(function () {
421+
unwrap = LogEcs.wrapConsole('test', {
422+
level: 'trace',
423+
namespaces: 'test'
424+
})
425+
})
426+
after(function () {
427+
unwrap()
428+
})
429+
430+
it('shall wrap console.log', function () {
431+
console.log('log %s', 'log')
432+
console.trace('trace')
433+
console.debug({ debug: true })
434+
console.info('log %j', { info: 1 })
435+
console.warn('warn')
436+
console.error(new Error('Baam'))
437+
})
438+
439+
it('shall not wrap console twice', function () {
440+
const unwrap1 = LogEcs.wrapConsole('test1')
441+
const unwrap2 = LogEcs.wrapConsole('test2')
442+
assert.strictEqual(unwrap1, unwrap)
443+
assert.strictEqual(unwrap2, unwrap)
444+
})
445+
})
446+
447+
describe('wrap debug', function () {
448+
let unwrap
449+
before(function () {
450+
const options = { level: 'debug', namespaces: '*' }
451+
unwrap = LogEcs.wrapDebug(options)
452+
})
453+
after(function () {
454+
unwrap()
455+
})
456+
457+
it('shall wrap debug', function () {
458+
const log = debug('namespace')
459+
log.enabled = '*'
460+
log('hello %s', 'log')
461+
})
462+
})
416463
})

0 commit comments

Comments
 (0)