-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathProcLog.test.js
More file actions
105 lines (98 loc) · 2.19 KB
/
ProcLog.test.js
File metadata and controls
105 lines (98 loc) · 2.19 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
94
95
96
97
98
99
100
101
102
103
104
105
import assert from 'node:assert'
import { LogEcs } from '../src/index.js'
import { ProcLog, initProcLog, EVENT_NAME } from '../src/ProcLog.js'
describe('ProcLog', function () {
beforeEach(function () {
initProcLog()
})
it('should log via process.emit', function () {
const { lines } = myInitProcLog()
const log = new ProcLog('test:1')
log.log('a log line')
assert.deepEqual(lines, ['LOG', 'test:1', 'a log line', []])
})
it('should log deep object', function () {
const { lines } = myInitProcLog()
const log = new ProcLog('test:2')
log.log({ a: { nested: 'object' } })
assert.deepEqual(lines, [
'LOG',
'test:2',
{
a: {
nested: 'object'
}
},
[]
])
})
it('should log deep object with format', function () {
const { lines } = myInitProcLog()
const log = new ProcLog('test:2')
log.info('%j', { a: { nested: 'object' } })
assert.deepEqual(lines, [
'INFO',
'test:2',
'%j',
[
{
a: {
nested: 'object'
}
}
]
])
})
it('should use the Ecs logger', function () {
initProcLog({ Log: LogEcs, json: true, colors: false })
const { lines } = myInitProcLog()
const log = new ProcLog('test:3')
log.warn('%j', { a: { nested: 'object' } })
assert.deepEqual(lines, [
'WARN',
'test:3',
'%j',
[
{
a: {
nested: 'object'
}
}
]
])
})
it('should not use number level or serializers', function () {
initProcLog({
levelNumbers: true,
serializers: {
err: (err) => {
return err?.message
}
}
})
const { lines } = myInitProcLog()
const log = new ProcLog('test:4')
log.error({ err: { name: 'Error', message: 'error' } })
assert.deepEqual(lines, [
'ERROR',
'test:4',
{
err: {
message: 'error',
name: 'Error'
}
},
[]
])
})
})
const myInitProcLog = () => {
let lines = []
const reset = () => {
lines = []
}
process.on(EVENT_NAME, (...args) => {
lines.push(...args)
})
return { lines, reset }
}