Skip to content

Commit 4f032a8

Browse files
committed
feat(ProcLog): static methods wrapConsole, wrapDebug on ProcLog
1 parent 1dee762 commit 4f032a8

File tree

3 files changed

+164
-77
lines changed

3 files changed

+164
-77
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
/**

test/ProcLog.test.js

Lines changed: 127 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,95 +1,147 @@
11
import assert from 'node:assert'
22
import { LogEcs } from '../src/index.js'
33
import { ProcLog, initProcLog, EVENT_PROC_LOG } from '../src/ProcLog.js'
4+
import debug from 'debug'
45

56
describe('ProcLog', function () {
6-
beforeEach(function () {
7-
initProcLog()
8-
})
9-
10-
it('should log via process.emit', function () {
11-
const { lines } = myInitProcLog()
12-
const log = new ProcLog('test:1')
13-
log.log('a log line')
14-
assert.deepEqual(lines, ['LOG', 'test:1', 'a log line', []])
15-
})
7+
describe('general', function () {
8+
beforeEach(function () {
9+
initProcLog()
10+
})
1611

17-
it('should log deep object', function () {
18-
const { lines } = myInitProcLog()
19-
const log = new ProcLog('test:2')
20-
log.log({ a: { nested: 'object' } })
21-
assert.deepEqual(lines, [
22-
'LOG',
23-
'test:2',
24-
{
25-
a: {
26-
nested: 'object'
27-
}
28-
},
29-
[]
30-
])
31-
})
12+
it('should log via process.emit', function () {
13+
const { lines } = myInitProcLog()
14+
const log = new ProcLog('test:1')
15+
log.log('a log line')
16+
assert.deepEqual(lines, ['LOG', 'test:1', 'a log line', []])
17+
})
3218

33-
it('should log deep object with format', function () {
34-
const { lines } = myInitProcLog()
35-
const log = new ProcLog('test:2')
36-
log.info('%j', { a: { nested: 'object' } })
37-
assert.deepEqual(lines, [
38-
'INFO',
39-
'test:2',
40-
'%j',
41-
[
19+
it('should log deep object', function () {
20+
const { lines } = myInitProcLog()
21+
const log = new ProcLog('test:2')
22+
log.log({ a: { nested: 'object' } })
23+
assert.deepEqual(lines, [
24+
'LOG',
25+
'test:2',
4226
{
4327
a: {
4428
nested: 'object'
4529
}
46-
}
47-
]
48-
])
49-
})
30+
},
31+
[]
32+
])
33+
})
5034

51-
it('should use the Ecs logger', function () {
52-
initProcLog({ Log: LogEcs, json: true, colors: false })
53-
const { lines } = myInitProcLog()
54-
const log = new ProcLog('test:3')
55-
log.warn('%j', { a: { nested: 'object' } })
56-
assert.deepEqual(lines, [
57-
'WARN',
58-
'test:3',
59-
'%j',
60-
[
61-
{
62-
a: {
63-
nested: 'object'
35+
it('should log deep object with format', function () {
36+
const { lines } = myInitProcLog()
37+
const log = new ProcLog('test:2')
38+
log.info('%j', { a: { nested: 'object' } })
39+
assert.deepEqual(lines, [
40+
'INFO',
41+
'test:2',
42+
'%j',
43+
[
44+
{
45+
a: {
46+
nested: 'object'
47+
}
48+
}
49+
]
50+
])
51+
})
52+
53+
it('should use the Ecs logger', function () {
54+
initProcLog({ Log: LogEcs, json: true, colors: false })
55+
const { lines } = myInitProcLog()
56+
const log = new ProcLog('test:3')
57+
log.warn('%j', { a: { nested: 'object' } })
58+
assert.deepEqual(lines, [
59+
'WARN',
60+
'test:3',
61+
'%j',
62+
[
63+
{
64+
a: {
65+
nested: 'object'
66+
}
67+
}
68+
]
69+
])
70+
})
71+
72+
it('should not use number level or serializers', function () {
73+
initProcLog({
74+
levelNumbers: true,
75+
serializers: {
76+
err: (err) => {
77+
return err?.message
6478
}
6579
}
66-
]
67-
])
80+
})
81+
const { lines } = myInitProcLog()
82+
const log = new ProcLog('test:4')
83+
log.error({ err: { name: 'Error', message: 'error' } })
84+
assert.deepEqual(lines, [
85+
'ERROR',
86+
'test:4',
87+
{
88+
err: {
89+
message: 'error',
90+
name: 'Error'
91+
}
92+
},
93+
[]
94+
])
95+
})
6896
})
6997

70-
it('should not use number level or serializers', function () {
71-
initProcLog({
72-
levelNumbers: true,
73-
serializers: {
74-
err: (err) => {
75-
return err?.message
76-
}
77-
}
98+
describe('wrap console', function () {
99+
let unwrap
100+
before(function () {
101+
initProcLog()
102+
103+
unwrap = ProcLog.wrapConsole('test', {
104+
level: 'trace',
105+
namespaces: 'test'
106+
})
107+
})
108+
after(function () {
109+
unwrap()
110+
})
111+
112+
it('shall wrap console.log', function () {
113+
console.log('log %s', 'log')
114+
console.trace('trace')
115+
console.debug({ debug: true })
116+
console.info('log %j', { info: 1 })
117+
console.warn('warn')
118+
console.error(new Error('Baam'))
119+
})
120+
121+
it('shall not wrap console twice', function () {
122+
const unwrap1 = ProcLog.wrapConsole('test1')
123+
const unwrap2 = ProcLog.wrapConsole('test2')
124+
assert.strictEqual(unwrap1, unwrap)
125+
assert.strictEqual(unwrap2, unwrap)
126+
})
127+
})
128+
129+
describe('wrap debug', function () {
130+
let unwrap
131+
before(function () {
132+
const options = { level: 'debug', namespaces: '*' }
133+
initProcLog(options)
134+
unwrap = ProcLog.wrapDebug(options)
135+
})
136+
after(function () {
137+
unwrap()
138+
})
139+
140+
it('shall wrap debug', function () {
141+
const log = debug('namespace')
142+
log.enabled = '*'
143+
log('hello %s', 'log')
78144
})
79-
const { lines } = myInitProcLog()
80-
const log = new ProcLog('test:4')
81-
log.error({ err: { name: 'Error', message: 'error' } })
82-
assert.deepEqual(lines, [
83-
'ERROR',
84-
'test:4',
85-
{
86-
err: {
87-
message: 'error',
88-
name: 'Error'
89-
}
90-
},
91-
[]
92-
])
93145
})
94146
})
95147

types/ProcLog.d.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
export function initProcLog(options?: LogOptionsWithCustomLog): void;
66
/** @typedef {import('./utils.js').Level} Level */
77
/** @typedef {import('./node.js').LogOptions} LogOptions */
8+
/** @typedef {import('./node.js').LogOptionWrapConsole} LogOptionWrapConsole */
89
/** @typedef {LogOptions & {Log: typeof Log}} LogOptionsWithCustomLog */
910
/**
1011
* @typedef {object} ProcLogOptions
@@ -44,6 +45,17 @@ export const EVENT_PROC_LOG: "log-level";
4445
* ```
4546
*/
4647
export class ProcLog extends LogBase {
48+
/**
49+
* @param {string} [name]
50+
* @param {ProcLogOptions & LogOptionWrapConsole} [opts]
51+
* @returns {() => void} unwrap functions
52+
*/
53+
static wrapConsole(name?: string, opts?: ProcLogOptions & LogOptionWrapConsole): () => void;
54+
/**
55+
* @param {ProcLogOptions} [opts]
56+
* @returns {() => void} unwrap functions
57+
*/
58+
static wrapDebug(opts?: ProcLogOptions): () => void;
4759
/**
4860
* creates a new logger
4961
* @param {string} name - namespace of Logger
@@ -53,6 +65,7 @@ export class ProcLog extends LogBase {
5365
}
5466
export type Level = import("./utils.js").Level;
5567
export type LogOptions = import("./node.js").LogOptions;
68+
export type LogOptionWrapConsole = import("./node.js").LogOptionWrapConsole;
5669
export type LogOptionsWithCustomLog = LogOptions & {
5770
Log: typeof Log;
5871
};

0 commit comments

Comments
 (0)