forked from pinojs/pino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpretty.test.js
392 lines (331 loc) · 12.2 KB
/
pretty.test.js
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
'use strict'
const { Writable } = require('stream')
const { test } = require('tap')
const { join } = require('path')
const execa = require('execa')
const writer = require('flush-write-stream')
const { once } = require('./helper')
const pino = require('../')
const strip = require('strip-ansi')
// silence warnings
process.removeAllListeners('warning')
// This test MUST be the first
test('deprecation', ({ equal, plan }) => {
plan(1)
process.once('warning', function (warning) {
equal(warning.code, 'PINODEP008')
})
pino({
prettyPrint: true
})
})
test('can be enabled via exported pino function', async ({ not }) => {
let actual = ''
const child = execa(process.argv[0], [join(__dirname, 'fixtures', 'pretty', 'basic.js')])
child.stdout.pipe(writer((s, enc, cb) => {
actual += s
cb()
}))
await once(child, 'close')
not(strip(actual).match(/\(123456 on abcdefghijklmnopqr\): h/), null)
})
test('can be enabled via exported pino function with pretty configuration', async ({ not }) => {
let actual = ''
const child = execa(process.argv[0], [join(__dirname, 'fixtures', 'pretty', 'level-first.js')])
child.stdout.pipe(writer((s, enc, cb) => {
actual += s
cb()
}))
await once(child, 'close')
not(strip(actual).match(/^INFO.*h/), null)
})
test('can be enabled via exported pino function with prettifier', async ({ not }) => {
let actual = ''
const child = execa(process.argv[0], [join(__dirname, 'fixtures', 'pretty', 'pretty-factory.js')])
child.stdout.pipe(writer((s, enc, cb) => {
actual += s
cb()
}))
await once(child, 'close')
not(strip(actual).match(/^INFO.*h/), null)
})
test('does not throw error when enabled with stream specified', async ({ doesNotThrow }) => {
doesNotThrow(() => pino({ prettyPrint: true }, process.stdout))
})
test('throws when prettyPrint is true but pino-pretty module is not installed', async ({ throws }) => {
// pino pretty *is* installed, and probably also cached, so rather than
// messing with the filesystem the simplest way to generate a not found
// error is to simulate it:
const prettyFactory = require('pino-pretty').prettyFactory
require('pino-pretty').prettyFactory = () => {
throw Error('Cannot find module \'pino-pretty\'')
}
throws(() => pino({ prettyPrint: true }), 'Missing `pino-pretty` module: `pino-pretty` must be installed separately')
require('pino-pretty').prettyFactory = prettyFactory
})
test('throws when prettyPrint has invalid options', async ({ throws }) => {
throws(() => pino({ prettyPrint: { ignore: ['hostname'] } }), 'opts.ignore.split is not a function')
})
test('can send pretty print to custom stream', async ({ equal }) => {
const dest = new Writable({
objectMode: true,
write (formatted, enc) {
equal(/^INFO.*foo\n$/.test(formatted), true)
}
})
const log = pino({
prettifier: require('pino-pretty').prettyFactory,
prettyPrint: {
levelFirst: true,
colorize: false
}
}, dest)
log.info('foo')
})
test('ignores `undefined` from prettifier', async ({ equal }) => {
let actual = ''
const child = execa(process.argv[0], [join(__dirname, 'fixtures', 'pretty', 'skipped-output.js')])
child.stdout.pipe(writer((s, enc) => {
actual += s
}))
await once(child, 'close')
equal(actual, '')
})
test('parses and outputs chindings', async ({ equal, not }) => {
let actual = ''
const child = execa(process.argv[0], [join(__dirname, 'fixtures', 'pretty', 'child.js')])
child.stdout.pipe(writer((s, enc, cb) => {
actual += s
cb()
}))
await once(child, 'close')
not(strip(actual).match(/\(123456 on abcdefghijklmnopqr\): h/), null)
not(strip(actual).match(/\(123456 on abcdefghijklmnopqr\): h2/), null)
not(strip(actual).match(/a: 1/), null)
not(strip(actual).match(/b: 2/), null)
equal(strip(actual).match(/a: 1/g).length, 3)
})
test('applies updated chindings', async ({ not }) => {
let actual = ''
const child = execa(process.argv[0], [join(__dirname, 'fixtures', 'pretty', 'child-with-updated-chindings.js')])
child.stdout.pipe(writer((s, enc, cb) => {
actual += s
cb()
}))
await once(child, 'close')
not(strip(actual).match(/foo: 123/), null)
not(strip(actual).match(/foo: 456/), null)
not(strip(actual).match(/bar: 789/), null)
})
test('applies formatters', async ({ not }) => {
let actual = ''
const child = execa(process.argv[0], [join(__dirname, 'fixtures', 'pretty', 'formatters.js')])
child.stdout.pipe(writer((s, enc, cb) => {
actual += s
cb()
}))
await once(child, 'close')
not(strip(actual).match(/\(123456 on abcdefghijklmnopqr\): h/), null)
not(strip(actual).match(/foo: "formatted_bar"/), null)
})
test('parses and outputs chindings with serializer', async ({ equal, not }) => {
let actual = ''
const child = execa(process.argv[0], [join(__dirname, 'fixtures', 'pretty', 'child-with-serializer.js')])
child.stdout.pipe(writer((s, enc, cb) => {
actual += s
cb()
}))
await once(child, 'close')
not(strip(actual).match(/\(123456 on abcdefghijklmnopqr\): h/), null)
not(strip(actual).match(/\(123456 on abcdefghijklmnopqr\): h2/), null)
not(strip(actual).match(/\(123456 on abcdefghijklmnopqr\): h3/), null)
not(strip(actual).match(/\(123456 on abcdefghijklmnopqr\): h4/), null)
not(strip(actual).match(/a: 2/), null)
not(strip(actual).match(/a: 16/), null)
not(strip(actual).match(/a: 42/), null)
equal(strip(actual).match(/a: /g).length, 4)
})
test('applies serializers', async ({ not }) => {
let actual = ''
const child = execa(process.argv[0], [join(__dirname, 'fixtures', 'pretty', 'serializers.js')])
child.stdout.pipe(writer((s, enc, cb) => {
actual += s
cb()
}))
await once(child, 'close')
not(strip(actual).match(/\(123456 on abcdefghijklmnopqr\): h/), null)
not(strip(actual).match(/foo: "bar"/), null)
})
test('applies redaction rules', async ({ equal, not }) => {
let actual = ''
const child = execa(process.argv[0], [join(__dirname, 'fixtures', 'pretty', 'redact.js')])
child.stdout.pipe(writer((s, enc, cb) => {
actual += s
cb()
}))
await once(child, 'close')
not(strip(actual).match(/\(123456 on abcdefghijklmnopqr\): h/), null)
not(strip(actual).match(/\[Redacted\]/), null)
equal(strip(actual).match(/object/), null)
})
test('dateformat', async ({ not }) => {
let actual = ''
const child = execa(process.argv[0], [join(__dirname, 'fixtures', 'pretty', 'dateformat.js')])
child.stdout.pipe(writer((s, enc, cb) => {
actual += s
cb()
}))
await once(child, 'close')
not(strip(actual).match(/\(123456 on abcdefghijklmnopqr\): h/), null)
})
test('without timestamp', async ({ not }) => {
let actual = ''
const child = execa(process.argv[0], [join(__dirname, 'fixtures', 'pretty', 'no-time.js')])
child.stdout.pipe(writer((s, enc, cb) => {
actual += s
cb()
}))
await once(child, 'close')
not(strip(actual).slice(2), '[]')
})
test('with custom timestamp', async ({ equal }) => {
let actual = ''
const child = execa(process.argv[0], [join(__dirname, 'fixtures', 'pretty', 'custom-time.js')])
child.stdout.pipe(writer((s, enc, cb) => {
actual += s
cb()
}))
await once(child, 'close')
equal(strip(actual).slice(0, 6), '[test]')
})
test('with custom timestamp label', async ({ equal }) => {
let actual = ''
const child = execa(process.argv[0], [join(__dirname, 'fixtures', 'pretty', 'custom-time-label.js')])
child.stdout.pipe(writer((s, enc, cb) => {
actual += s
cb()
}))
await once(child, 'close')
equal(strip(actual).slice(0, 6), '[test]')
})
test('errors', async ({ not }) => {
let actual = ''
const child = execa(process.argv[0], [join(__dirname, 'fixtures', 'pretty', 'error.js')])
child.stdout.pipe(writer((s, enc, cb) => {
actual += s
cb()
}))
await once(child, 'close')
not(strip(actual).match(/\(123456 on abcdefghijklmnopqr\): kaboom/), null)
not(strip(actual).match(/\(123456 on abcdefghijklmnopqr\): with a message/), null)
not(strip(actual).match(/.*error\.js.*/), null)
})
test('errors with props', async ({ not }) => {
let actual = ''
const child = execa(process.argv[0], [join(__dirname, 'fixtures', 'pretty', 'error-props.js')])
child.stdout.pipe(writer((s, enc, cb) => {
actual += s
cb()
}))
await once(child, 'close')
not(strip(actual).match(/\(123456 on abcdefghijklmnopqr\): kaboom/), null)
not(strip(actual).match(/"code": "ENOENT"/), null)
not(strip(actual).match(/"errno": 1/), null)
not(strip(actual).match(/.*error-props\.js.*/), null)
})
test('final works with pretty', async ({ not }) => {
let actual = ''
const child = execa(process.argv[0], [join(__dirname, 'fixtures', 'pretty', 'final.js')])
child.stdout.pipe(writer((s, enc, cb) => {
actual += s
cb()
}))
await once(child, 'close')
not(strip(actual).match(/WARN\s+\(123456 on abcdefghijklmnopqr\): pino.final with prettyPrint does not support flushing/), null)
not(strip(actual).match(/INFO\s+\(123456 on abcdefghijklmnopqr\): beforeExit/), null)
})
test('final works when returning a logger', async ({ not }) => {
let actual = ''
const child = execa(process.argv[0], [join(__dirname, 'fixtures', 'pretty', 'final-return.js')])
child.stdout.pipe(writer((s, enc, cb) => {
actual += s
cb()
}))
await once(child, 'close')
not(strip(actual).match(/WARN\s+\(123456 on abcdefghijklmnopqr\): pino.final with prettyPrint does not support flushing/), null)
not(strip(actual).match(/INFO\s+\(123456 on abcdefghijklmnopqr\): after/), null)
})
test('final works without prior logging', async ({ not }) => {
let actual = ''
const child = execa(process.argv[0], [join(__dirname, 'fixtures', 'pretty', 'final-no-log-before.js')])
child.stdout.pipe(writer((s, enc, cb) => {
actual += s
cb()
}))
await once(child, 'close')
not(strip(actual).match(/WARN\s*: pino.final with prettyPrint does not support flushing/), null)
not(strip(actual).match(/INFO\s*\(123456 on abcdefghijklmnopqr\): beforeExit/), null)
})
test('suppress flush sync warning when corresponding option is specified', async ({ equal }) => {
let actual = ''
const child = execa(process.argv[0], [join(__dirname, 'fixtures', 'pretty', 'suppress-flush-sync-warning.js')])
child.stdout.pipe(writer((s, enc, cb) => {
actual += s
cb()
}))
await once(child, 'close')
equal(strip(actual).match(/WARN\s+\(123456 on abcdefghijklmnopqr\): pino.final with prettyPrint does not support flushing/), null)
})
test('works as expected with an object with the msg prop', async ({ not }) => {
let actual = ''
const child = execa(process.argv[0], [join(__dirname, 'fixtures', 'pretty', 'obj-msg-prop.js')])
child.stdout.pipe(writer((s, enc, cb) => {
actual += s
cb()
}))
await once(child, 'close')
not(strip(actual).match(/\(123456 on abcdefghijklmnopqr\): hello/), null)
})
test('handles objects with null prototypes', async ({ not }) => {
let actual = ''
const child = execa(process.argv[0], [join(__dirname, 'fixtures', 'pretty', 'null-prototype.js')])
child.stdout.pipe(writer((s, enc, cb) => {
actual += s
cb()
}))
await once(child, 'close')
not(strip(actual).match(/\(123456 on abcdefghijklmnopqr\): hello\s+foo: "bar"/), null)
})
test('should not lose stream metadata for streams with `needsMetadataGsym` flag', async ({ not }) => {
const dest = new Writable({
objectMode: true,
write () {
not(typeof this.lastLevel === 'undefined', true)
not(typeof this.lastMsg === 'undefined', true)
not(typeof this.lastObj === 'undefined', true)
not(typeof this.lastTime === 'undefined', true)
not(typeof this.lastLogger === 'undefined', true)
}
})
dest[pino.symbols.needsMetadataGsym] = true
const log = pino({
prettyPrint: true
}, dest)
log.info('foo')
})
test('should not add stream metadata for streams without `needsMetadataGsym` flag', async ({ equal }) => {
const dest = new Writable({
objectMode: true,
write () {
equal(typeof this.lastLevel === 'undefined', true)
equal(typeof this.lastMsg === 'undefined', true)
equal(typeof this.lastObj === 'undefined', true)
equal(typeof this.lastTime === 'undefined', true)
equal(typeof this.lastLogger === 'undefined', true)
}
})
const log = pino({
prettyPrint: true
}, dest)
log.info('foo')
})