Skip to content

Commit 572d1fb

Browse files
committed
feat(LogEcs): static methods wrapConsole, wrapDebug on LogEcs
1 parent a46d4f5 commit 572d1fb

File tree

5 files changed

+87
-17
lines changed

5 files changed

+87
-17
lines changed

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/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
})

types/ecs/LogEcs.d.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
/**
2-
* @typedef {import('../serializers/index.js').Serializer} Serializer
3-
*/
4-
/**
5-
* @typedef {import('../node.js').LogOptions & {serializers: Record<string, Serializer>}} LogOptionsEcs
6-
* @typedef {import('../node.js').LogOptionWrapConsole} LogOptionWrapConsole
7-
*/
1+
/** @typedef {import('../serializers/index.js').Serializer} Serializer */
2+
/** @typedef {import('../node.js').LogOptions & {serializers: Record<string, Serializer>}} LogOptionsEcs */
3+
/** @typedef {import('../node.js').LogOptionWrapConsole} LogOptionWrapConsole */
84
/**
95
* Elastic Common Schema (ECS) compatible logger;
106
* See [field reference](https://www.elastic.co/guide/en/ecs/current/ecs-field-reference.html)
@@ -13,6 +9,17 @@
139
* (response) keys in objects
1410
*/
1511
export class LogEcs extends Log {
12+
/**
13+
* @param {string} [name]
14+
* @param {LogOptionsEcs & LogOptionWrapConsole} [opts]
15+
* @returns {() => void} unwrap function
16+
*/
17+
static wrapConsole(name?: string, opts?: LogOptionsEcs & LogOptionWrapConsole): () => void;
18+
/**
19+
* @param {LogOptionsEcs} [opts]
20+
* @returns {() => void} unwrap function
21+
*/
22+
static wrapDebug(opts?: LogOptionsEcs): () => void;
1623
/**
1724
* @param {string} name logger namespace
1825
* @param {LogOptionsEcs} [opts]

types/wrapDebug.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
export function wrapDebug(Log: any): () => void;
1+
export function wrapDebug(Log: any, opts: any): () => void;

0 commit comments

Comments
 (0)