sync mode on
This commit is contained in:
1 parent
1818932244
commit
1174b328e5
415 files changed
+44294
-1182
No files matched your search
+886
@@ -0,0 +1,886 @@
|
||||
'use strict'
|
||||
|
||||
const os = require('node:os')
|
||||
const { readFileSync } = require('node:fs')
|
||||
const test = require('node:test')
|
||||
const assert = require('node:assert')
|
||||
|
||||
const { sink, check, match, once, watchFileCreated, file } = require('./helper')
|
||||
const pino = require('../')
|
||||
const { version } = require('../package.json')
|
||||
const { pid } = process
|
||||
const hostname = os.hostname()
|
||||
|
||||
test('pino version is exposed on export', () => {
|
||||
assert.equal(pino.version, version)
|
||||
})
|
||||
|
||||
test('pino version is exposed on instance', () => {
|
||||
const instance = pino()
|
||||
assert.equal(instance.version, version)
|
||||
})
|
||||
|
||||
test('child instance exposes pino version', () => {
|
||||
const child = pino().child({ foo: 'bar' })
|
||||
assert.equal(child.version, version)
|
||||
})
|
||||
|
||||
test('bindings are exposed on every instance', () => {
|
||||
const instance = pino()
|
||||
assert.deepEqual(instance.bindings(), {})
|
||||
})
|
||||
|
||||
test('bindings contain the name and the child bindings', () => {
|
||||
const instance = pino({ name: 'basicTest', level: 'info' }).child({ foo: 'bar' }).child({ a: 2 })
|
||||
assert.deepEqual(instance.bindings(), { name: 'basicTest', foo: 'bar', a: 2 })
|
||||
})
|
||||
|
||||
test('set bindings on instance', () => {
|
||||
const instance = pino({ name: 'basicTest', level: 'info' })
|
||||
instance.setBindings({ foo: 'bar' })
|
||||
assert.deepEqual(instance.bindings(), { name: 'basicTest', foo: 'bar' })
|
||||
})
|
||||
|
||||
test('newly set bindings overwrite old bindings', () => {
|
||||
const instance = pino({ name: 'basicTest', level: 'info', base: { foo: 'bar' } })
|
||||
instance.setBindings({ foo: 'baz' })
|
||||
assert.deepEqual(instance.bindings(), { name: 'basicTest', foo: 'baz' })
|
||||
})
|
||||
|
||||
test('set bindings on child instance', () => {
|
||||
const child = pino({ name: 'basicTest', level: 'info' }).child({})
|
||||
child.setBindings({ foo: 'bar' })
|
||||
assert.deepEqual(child.bindings(), { name: 'basicTest', foo: 'bar' })
|
||||
})
|
||||
|
||||
test('child should have bindings set by parent', () => {
|
||||
const instance = pino({ name: 'basicTest', level: 'info' })
|
||||
instance.setBindings({ foo: 'bar' })
|
||||
const child = instance.child({})
|
||||
assert.deepEqual(child.bindings(), { name: 'basicTest', foo: 'bar' })
|
||||
})
|
||||
|
||||
test('child should not share bindings of parent set after child creation', () => {
|
||||
const instance = pino({ name: 'basicTest', level: 'info' })
|
||||
const child = instance.child({})
|
||||
instance.setBindings({ foo: 'bar' })
|
||||
assert.deepEqual(instance.bindings(), { name: 'basicTest', foo: 'bar' })
|
||||
assert.deepEqual(child.bindings(), { name: 'basicTest' })
|
||||
})
|
||||
|
||||
function levelTest (name, level) {
|
||||
test(`${name} logs as ${level}`, async () => {
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
instance.level = name
|
||||
instance[name]('hello world')
|
||||
check(assert.equal, await once(stream, 'data'), level, 'hello world')
|
||||
})
|
||||
|
||||
test(`passing objects at level ${name}`, async () => {
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
instance.level = name
|
||||
const obj = { hello: 'world' }
|
||||
instance[name](obj)
|
||||
|
||||
const result = await once(stream, 'data')
|
||||
assert.equal(new Date(result.time) <= new Date(), true, 'time is greater than Date.now()')
|
||||
assert.equal(result.pid, pid)
|
||||
assert.equal(result.hostname, hostname)
|
||||
assert.equal(result.level, level)
|
||||
assert.equal(result.hello, 'world')
|
||||
assert.deepEqual(Object.keys(obj), ['hello'])
|
||||
})
|
||||
|
||||
test(`passing an object and a string at level ${name}`, async () => {
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
instance.level = name
|
||||
const obj = { hello: 'world' }
|
||||
instance[name](obj, 'a string')
|
||||
const result = await once(stream, 'data')
|
||||
assert.equal(new Date(result.time) <= new Date(), true, 'time is greater than Date.now()')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level,
|
||||
msg: 'a string',
|
||||
hello: 'world'
|
||||
})
|
||||
assert.deepEqual(Object.keys(obj), ['hello'])
|
||||
})
|
||||
|
||||
test(`passing a undefined and a string at level ${name}`, async () => {
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
instance.level = name
|
||||
instance[name](undefined, 'a string')
|
||||
const result = await once(stream, 'data')
|
||||
assert.equal(new Date(result.time) <= new Date(), true, 'time is greater than Date.now()')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level,
|
||||
msg: 'a string'
|
||||
})
|
||||
})
|
||||
|
||||
test(`overriding object key by string at level ${name}`, async () => {
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
instance.level = name
|
||||
instance[name]({ hello: 'world', msg: 'object' }, 'string')
|
||||
const result = await once(stream, 'data')
|
||||
assert.equal(new Date(result.time) <= new Date(), true, 'time is greater than Date.now()')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level,
|
||||
msg: 'string',
|
||||
hello: 'world'
|
||||
})
|
||||
})
|
||||
|
||||
test(`formatting logs as ${name}`, async () => {
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
instance.level = name
|
||||
instance[name]('hello %d', 42)
|
||||
const result = await once(stream, 'data')
|
||||
check(assert.equal, result, level, 'hello 42')
|
||||
})
|
||||
|
||||
test(`formatting a symbol at level ${name}`, async () => {
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
instance.level = name
|
||||
|
||||
const sym = Symbol('foo')
|
||||
instance[name]('hello %s', sym)
|
||||
|
||||
const result = await once(stream, 'data')
|
||||
|
||||
check(assert.equal, result, level, 'hello Symbol(foo)')
|
||||
})
|
||||
|
||||
test(`passing error with a serializer at level ${name}`, async () => {
|
||||
const stream = sink()
|
||||
const err = new Error('myerror')
|
||||
const instance = pino({
|
||||
serializers: {
|
||||
err: pino.stdSerializers.err
|
||||
}
|
||||
}, stream)
|
||||
instance.level = name
|
||||
instance[name]({ err })
|
||||
const result = await once(stream, 'data')
|
||||
assert.equal(new Date(result.time) <= new Date(), true, 'time is greater than Date.now()')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level,
|
||||
err: {
|
||||
type: 'Error',
|
||||
message: err.message,
|
||||
stack: err.stack
|
||||
},
|
||||
msg: err.message
|
||||
})
|
||||
})
|
||||
|
||||
test(`child logger for level ${name}`, async () => {
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
instance.level = name
|
||||
const child = instance.child({ hello: 'world' })
|
||||
child[name]('hello world')
|
||||
const result = await once(stream, 'data')
|
||||
assert.equal(new Date(result.time) <= new Date(), true, 'time is greater than Date.now()')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level,
|
||||
msg: 'hello world',
|
||||
hello: 'world'
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
levelTest('fatal', 60)
|
||||
levelTest('error', 50)
|
||||
levelTest('warn', 40)
|
||||
levelTest('info', 30)
|
||||
levelTest('debug', 20)
|
||||
levelTest('trace', 10)
|
||||
|
||||
test('serializers can return undefined to strip field', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({
|
||||
serializers: {
|
||||
test () { return undefined }
|
||||
}
|
||||
}, stream)
|
||||
|
||||
instance.info({ test: 'sensitive info' })
|
||||
const result = await once(stream, 'data')
|
||||
assert.equal('test' in result, false)
|
||||
})
|
||||
|
||||
test('streams receive a message event with PINO_CONFIG', (t, end) => {
|
||||
const stream = sink()
|
||||
stream.once('message', (message) => {
|
||||
match(message, {
|
||||
code: 'PINO_CONFIG',
|
||||
config: {
|
||||
errorKey: 'err',
|
||||
levels: {
|
||||
labels: {
|
||||
10: 'trace',
|
||||
20: 'debug',
|
||||
30: 'info',
|
||||
40: 'warn',
|
||||
50: 'error',
|
||||
60: 'fatal'
|
||||
},
|
||||
values: {
|
||||
debug: 20,
|
||||
error: 50,
|
||||
fatal: 60,
|
||||
info: 30,
|
||||
trace: 10,
|
||||
warn: 40
|
||||
}
|
||||
},
|
||||
messageKey: 'msg'
|
||||
}
|
||||
})
|
||||
end()
|
||||
})
|
||||
pino(stream)
|
||||
})
|
||||
|
||||
test('does not explode with a circular ref', () => {
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
const b = {}
|
||||
const a = {
|
||||
hello: b
|
||||
}
|
||||
b.a = a // circular ref
|
||||
assert.doesNotThrow(() => instance.info(a))
|
||||
})
|
||||
|
||||
test('set the name', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({
|
||||
name: 'hello'
|
||||
}, stream)
|
||||
instance.fatal('this is fatal')
|
||||
const result = await once(stream, 'data')
|
||||
assert.equal(new Date(result.time) <= new Date(), true, 'time is greater than Date.now()')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 60,
|
||||
name: 'hello',
|
||||
msg: 'this is fatal'
|
||||
})
|
||||
})
|
||||
|
||||
test('set the messageKey', async () => {
|
||||
const stream = sink()
|
||||
const message = 'hello world'
|
||||
const messageKey = 'fooMessage'
|
||||
const instance = pino({
|
||||
messageKey
|
||||
}, stream)
|
||||
instance.info(message)
|
||||
const result = await once(stream, 'data')
|
||||
assert.equal(new Date(result.time) <= new Date(), true, 'time is greater than Date.now()')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 30,
|
||||
fooMessage: message
|
||||
})
|
||||
})
|
||||
|
||||
test('set the nestedKey', async () => {
|
||||
const stream = sink()
|
||||
const object = { hello: 'world' }
|
||||
const nestedKey = 'stuff'
|
||||
const instance = pino({
|
||||
nestedKey
|
||||
}, stream)
|
||||
instance.info(object)
|
||||
const result = await once(stream, 'data')
|
||||
assert.equal(new Date(result.time) <= new Date(), true, 'time is greater than Date.now()')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 30,
|
||||
stuff: object
|
||||
})
|
||||
})
|
||||
|
||||
test('set undefined properties', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
instance.info({ hello: 'world', property: undefined })
|
||||
const result = await once(stream, 'data')
|
||||
assert.equal(new Date(result.time) <= new Date(), true, 'time is greater than Date.now()')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 30,
|
||||
hello: 'world'
|
||||
})
|
||||
})
|
||||
|
||||
test('prototype properties are not logged', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
instance.info(Object.create({ hello: 'world' }))
|
||||
const { hello } = await once(stream, 'data')
|
||||
assert.equal(hello, undefined)
|
||||
})
|
||||
|
||||
test('set the base', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({
|
||||
base: {
|
||||
a: 'b'
|
||||
}
|
||||
}, stream)
|
||||
|
||||
instance.fatal('this is fatal')
|
||||
const result = await once(stream, 'data')
|
||||
assert.equal(new Date(result.time) <= new Date(), true, 'time is greater than Date.now()')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
a: 'b',
|
||||
level: 60,
|
||||
msg: 'this is fatal'
|
||||
})
|
||||
})
|
||||
|
||||
test('set the base to null', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({
|
||||
base: null
|
||||
}, stream)
|
||||
instance.fatal('this is fatal')
|
||||
const result = await once(stream, 'data')
|
||||
assert.equal(new Date(result.time) <= new Date(), true, 'time is greater than Date.now()')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
level: 60,
|
||||
msg: 'this is fatal'
|
||||
})
|
||||
})
|
||||
|
||||
test('set the base to null and use a formatter', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({
|
||||
base: null,
|
||||
formatters: {
|
||||
log (input) {
|
||||
return Object.assign({}, input, { additionalMessage: 'using pino' })
|
||||
}
|
||||
}
|
||||
}, stream)
|
||||
instance.fatal('this is fatal too')
|
||||
const result = await once(stream, 'data')
|
||||
assert.equal(new Date(result.time) <= new Date(), true, 'time is greater than Date.now()')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
level: 60,
|
||||
msg: 'this is fatal too',
|
||||
additionalMessage: 'using pino'
|
||||
})
|
||||
})
|
||||
|
||||
test('throw if creating child without bindings', () => {
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
try {
|
||||
instance.child()
|
||||
assert.fail('it should throw')
|
||||
} catch (err) {
|
||||
assert.equal(err.message, 'missing bindings for child Pino')
|
||||
}
|
||||
})
|
||||
|
||||
test('correctly escapes msg strings with stray double quote at end', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({
|
||||
name: 'hello'
|
||||
}, stream)
|
||||
|
||||
instance.fatal('this contains "')
|
||||
const result = await once(stream, 'data')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 60,
|
||||
name: 'hello',
|
||||
msg: 'this contains "'
|
||||
})
|
||||
})
|
||||
|
||||
test('correctly escape msg strings with unclosed double quote', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({
|
||||
name: 'hello'
|
||||
}, stream)
|
||||
instance.fatal('" this contains')
|
||||
const result = await once(stream, 'data')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 60,
|
||||
name: 'hello',
|
||||
msg: '" this contains'
|
||||
})
|
||||
})
|
||||
|
||||
test('correctly escape quote in a key', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
const obj = { 'some"obj': 'world' }
|
||||
instance.info(obj, 'a string')
|
||||
const result = await once(stream, 'data')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
level: 30,
|
||||
pid,
|
||||
hostname,
|
||||
msg: 'a string',
|
||||
'some"obj': 'world'
|
||||
})
|
||||
assert.deepEqual(Object.keys(obj), ['some"obj'])
|
||||
})
|
||||
|
||||
// https://github.com/pinojs/pino/issues/139
|
||||
test('object and format string', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
instance.info({}, 'foo %s', 'bar')
|
||||
|
||||
const result = await once(stream, 'data')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 30,
|
||||
msg: 'foo bar'
|
||||
})
|
||||
})
|
||||
|
||||
test('object and format string property', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
instance.info({ answer: 42 }, 'foo %s', 'bar')
|
||||
const result = await once(stream, 'data')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 30,
|
||||
msg: 'foo bar',
|
||||
answer: 42
|
||||
})
|
||||
})
|
||||
|
||||
test('correctly strip undefined when returned from toJSON', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({
|
||||
test: 'this'
|
||||
}, stream)
|
||||
instance.fatal({ test: { toJSON () { return undefined } } })
|
||||
const result = await once(stream, 'data')
|
||||
assert.equal('test' in result, false)
|
||||
})
|
||||
|
||||
test('correctly supports stderr', (t, end) => {
|
||||
// stderr inherits from Stream, rather than Writable
|
||||
const dest = {
|
||||
writable: true,
|
||||
write (result) {
|
||||
result = JSON.parse(result)
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 60,
|
||||
msg: 'a message'
|
||||
})
|
||||
end()
|
||||
}
|
||||
}
|
||||
const instance = pino(dest)
|
||||
instance.fatal('a message')
|
||||
})
|
||||
|
||||
test('normalize number to string', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
instance.info(1)
|
||||
const result = await once(stream, 'data')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 30,
|
||||
msg: '1'
|
||||
})
|
||||
})
|
||||
|
||||
test('normalize number to string with an object', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
instance.info({ answer: 42 }, 1)
|
||||
const result = await once(stream, 'data')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 30,
|
||||
msg: '1',
|
||||
answer: 42
|
||||
})
|
||||
})
|
||||
|
||||
test('handles objects with null prototype', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
const o = Object.create(null)
|
||||
o.test = 'test'
|
||||
instance.info(o)
|
||||
const result = await once(stream, 'data')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 30,
|
||||
test: 'test'
|
||||
})
|
||||
})
|
||||
|
||||
test('pino.destination', async () => {
|
||||
const tmp = file()
|
||||
const instance = pino(pino.destination(tmp))
|
||||
instance.info('hello')
|
||||
await watchFileCreated(tmp)
|
||||
const result = JSON.parse(readFileSync(tmp).toString())
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 30,
|
||||
msg: 'hello'
|
||||
})
|
||||
})
|
||||
|
||||
test('auto pino.destination with a string', async () => {
|
||||
const tmp = file()
|
||||
const instance = pino(tmp)
|
||||
instance.info('hello')
|
||||
await watchFileCreated(tmp)
|
||||
const result = JSON.parse(readFileSync(tmp).toString())
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 30,
|
||||
msg: 'hello'
|
||||
})
|
||||
})
|
||||
|
||||
test('auto pino.destination with a string as second argument', async () => {
|
||||
const tmp = file()
|
||||
const instance = pino(null, tmp)
|
||||
instance.info('hello')
|
||||
await watchFileCreated(tmp)
|
||||
const result = JSON.parse(readFileSync(tmp).toString())
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 30,
|
||||
msg: 'hello'
|
||||
})
|
||||
})
|
||||
|
||||
test('does not override opts with a string as second argument', async () => {
|
||||
const tmp = file()
|
||||
const instance = pino({
|
||||
timestamp: () => ',"time":"none"'
|
||||
}, tmp)
|
||||
instance.info('hello')
|
||||
await watchFileCreated(tmp)
|
||||
const result = JSON.parse(readFileSync(tmp).toString())
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 30,
|
||||
time: 'none',
|
||||
msg: 'hello'
|
||||
})
|
||||
})
|
||||
|
||||
// https://github.com/pinojs/pino/issues/222
|
||||
test('children with same names render in correct order', async () => {
|
||||
const stream = sink()
|
||||
const root = pino(stream)
|
||||
root.child({ a: 1 }).child({ a: 2 }).info({ a: 3 })
|
||||
const { a } = await once(stream, 'data')
|
||||
assert.equal(a, 3, 'last logged object takes precedence')
|
||||
})
|
||||
|
||||
test('use `safe-stable-stringify` to avoid circular dependencies', async () => {
|
||||
const stream = sink()
|
||||
const root = pino(stream)
|
||||
// circular depth
|
||||
const obj = {}
|
||||
obj.a = obj
|
||||
root.info(obj)
|
||||
const { a } = await once(stream, 'data')
|
||||
assert.deepEqual(a, { a: '[Circular]' })
|
||||
})
|
||||
|
||||
test('correctly log non circular objects', async () => {
|
||||
const stream = sink()
|
||||
const root = pino(stream)
|
||||
const obj = {}
|
||||
let parent = obj
|
||||
for (let i = 0; i < 10; i++) {
|
||||
parent.node = {}
|
||||
parent = parent.node
|
||||
}
|
||||
root.info(obj)
|
||||
const { node } = await once(stream, 'data')
|
||||
assert.deepEqual(node, { node: { node: { node: { node: { node: { node: { node: { node: { node: {} } } } } } } } } })
|
||||
})
|
||||
|
||||
test('safe-stable-stringify must be used when interpolating', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
|
||||
const o = { a: { b: {} } }
|
||||
o.a.b.c = o.a.b
|
||||
instance.info('test %j', o)
|
||||
|
||||
const { msg } = await once(stream, 'data')
|
||||
assert.equal(msg, 'test {"a":{"b":{"c":"[Circular]"}}}')
|
||||
})
|
||||
|
||||
test('throws when setting useOnlyCustomLevels without customLevels', () => {
|
||||
assert.throws(
|
||||
() => {
|
||||
pino({
|
||||
useOnlyCustomLevels: true
|
||||
})
|
||||
},
|
||||
/customLevels is required if useOnlyCustomLevels is set true/
|
||||
)
|
||||
})
|
||||
|
||||
test('correctly log Infinity', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
|
||||
const o = { num: Infinity }
|
||||
instance.info(o)
|
||||
|
||||
const { num } = await once(stream, 'data')
|
||||
assert.equal(num, null)
|
||||
})
|
||||
|
||||
test('correctly log -Infinity', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
|
||||
const o = { num: -Infinity }
|
||||
instance.info(o)
|
||||
|
||||
const { num } = await once(stream, 'data')
|
||||
assert.equal(num, null)
|
||||
})
|
||||
|
||||
test('correctly log NaN', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
|
||||
const o = { num: NaN }
|
||||
instance.info(o)
|
||||
|
||||
const { num } = await once(stream, 'data')
|
||||
assert.equal(num, null)
|
||||
})
|
||||
|
||||
test('offers a .default() method to please typescript', async () => {
|
||||
assert.equal(pino.default, pino)
|
||||
|
||||
const stream = sink()
|
||||
const instance = pino.default(stream)
|
||||
instance.info('hello world')
|
||||
check(assert.equal, await once(stream, 'data'), 30, 'hello world')
|
||||
})
|
||||
|
||||
test('correctly skip function', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
|
||||
const o = { num: NaN }
|
||||
instance.info(o, () => {})
|
||||
|
||||
const { msg } = await once(stream, 'data')
|
||||
assert.equal(msg, undefined)
|
||||
})
|
||||
|
||||
test('correctly skip Infinity', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
|
||||
const o = { num: NaN }
|
||||
instance.info(o, Infinity)
|
||||
|
||||
const { msg } = await once(stream, 'data')
|
||||
assert.equal(msg, null)
|
||||
})
|
||||
|
||||
test('correctly log number', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
|
||||
const o = { num: NaN }
|
||||
instance.info(o, 42)
|
||||
|
||||
const { msg } = await once(stream, 'data')
|
||||
assert.equal(msg, 42)
|
||||
})
|
||||
|
||||
test('nestedKey should not be used for non-objects', async () => {
|
||||
const stream = sink()
|
||||
const message = 'hello'
|
||||
const nestedKey = 'stuff'
|
||||
const instance = pino({
|
||||
nestedKey
|
||||
}, stream)
|
||||
instance.info(message)
|
||||
const result = await once(stream, 'data')
|
||||
delete result.time
|
||||
assert.deepStrictEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 30,
|
||||
msg: message
|
||||
})
|
||||
})
|
||||
|
||||
test('throws if prettyPrint is passed in as an option', async () => {
|
||||
assert.throws(
|
||||
() => {
|
||||
pino({
|
||||
prettyPrint: true
|
||||
})
|
||||
},
|
||||
Error('prettyPrint option is no longer supported, see the pino-pretty package (https://github.com/pinojs/pino-pretty)')
|
||||
)
|
||||
})
|
||||
|
||||
test('Should invoke `onChild` with the newly created child', () => {
|
||||
let innerChild
|
||||
const child = pino({
|
||||
onChild: (instance) => {
|
||||
innerChild = instance
|
||||
}
|
||||
}).child({ foo: 'bar' })
|
||||
assert.equal(child, innerChild)
|
||||
})
|
||||
|
||||
test('logger message should have the prefix message that defined in the logger creation', async () => {
|
||||
const stream = sink()
|
||||
const logger = pino({
|
||||
msgPrefix: 'My name is Bond '
|
||||
}, stream)
|
||||
assert.equal(logger.msgPrefix, 'My name is Bond ')
|
||||
logger.info('James Bond')
|
||||
const { msg } = await once(stream, 'data')
|
||||
assert.equal(msg, 'My name is Bond James Bond')
|
||||
})
|
||||
|
||||
test('child message should have the prefix message that defined in the child creation', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
const child = instance.child({}, { msgPrefix: 'My name is Bond ' })
|
||||
child.info('James Bond')
|
||||
const { msg } = await once(stream, 'data')
|
||||
assert.equal(msg, 'My name is Bond James Bond')
|
||||
})
|
||||
|
||||
test('child message should have the prefix message that defined in the child creation when logging with log meta', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
const child = instance.child({}, { msgPrefix: 'My name is Bond ' })
|
||||
child.info({ hello: 'world' }, 'James Bond')
|
||||
const { msg, hello } = await once(stream, 'data')
|
||||
assert.equal(hello, 'world')
|
||||
assert.equal(msg, 'My name is Bond James Bond')
|
||||
})
|
||||
|
||||
test('logged message should not have the prefix when not providing any message', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
const child = instance.child({}, { msgPrefix: 'This should not be shown ' })
|
||||
child.info({ hello: 'world' })
|
||||
const { msg, hello } = await once(stream, 'data')
|
||||
assert.equal(hello, 'world')
|
||||
assert.equal(msg, undefined)
|
||||
})
|
||||
|
||||
test('child message should append parent prefix to current prefix that defined in the child creation', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({
|
||||
msgPrefix: 'My name is Bond '
|
||||
}, stream)
|
||||
const child = instance.child({}, { msgPrefix: 'James ' })
|
||||
child.info('Bond')
|
||||
assert.equal(child.msgPrefix, 'My name is Bond James ')
|
||||
const { msg } = await once(stream, 'data')
|
||||
assert.equal(msg, 'My name is Bond James Bond')
|
||||
})
|
||||
|
||||
test('child message should inherent parent prefix', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({
|
||||
msgPrefix: 'My name is Bond '
|
||||
}, stream)
|
||||
const child = instance.child({})
|
||||
child.info('James Bond')
|
||||
const { msg } = await once(stream, 'data')
|
||||
assert.equal(msg, 'My name is Bond James Bond')
|
||||
})
|
||||
|
||||
test('grandchild message should inherent parent prefix', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
const child = instance.child({}, { msgPrefix: 'My name is Bond ' })
|
||||
const grandchild = child.child({})
|
||||
grandchild.info('James Bond')
|
||||
const { msg } = await once(stream, 'data')
|
||||
assert.equal(msg, 'My name is Bond James Bond')
|
||||
})
|
||||
+59
@@ -0,0 +1,59 @@
|
||||
'use strict'
|
||||
|
||||
const test = require('node:test')
|
||||
const assert = require('node:assert')
|
||||
const { join } = require('node:path')
|
||||
const { fork } = require('node:child_process')
|
||||
const tspl = require('@matteo.collina/tspl')
|
||||
const { once } = require('./helper')
|
||||
const pino = require('..')
|
||||
|
||||
if (process.platform === 'win32') {
|
||||
console.log('skipping on windows')
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
if (process.env.CITGM) {
|
||||
// This looks like a some form of limitations of the CITGM test runner
|
||||
// or the HW/SW we run it on. This file can hang on Node.js v18.x.
|
||||
// The failure does not reproduce locally or on our CI.
|
||||
// Skipping it is the only way to keep pino in CITGM.
|
||||
// https://github.com/nodejs/citgm/pull/1002#issuecomment-1751942988
|
||||
console.log('Skipping on Node.js core CITGM because it hangs on v18.x')
|
||||
process.exit(0)
|
||||
}
|
||||
|
||||
function testFile (file) {
|
||||
file = join('fixtures', 'broken-pipe', file)
|
||||
test(file, async () => {
|
||||
const child = fork(join(__dirname, file), { silent: true })
|
||||
child.stdout.destroy()
|
||||
|
||||
child.stderr.pipe(process.stdout)
|
||||
|
||||
const res = await once(child, 'close')
|
||||
assert.equal(res, 0) // process exits successfully
|
||||
})
|
||||
}
|
||||
|
||||
testFile('basic.js')
|
||||
testFile('destination.js')
|
||||
testFile('syncfalse.js')
|
||||
|
||||
test('let error pass through', async (t) => {
|
||||
const plan = tspl(t, { plan: 3 })
|
||||
const stream = pino.destination({ sync: true })
|
||||
|
||||
// side effect of the pino constructor is that it will set an
|
||||
// event handler for error
|
||||
pino(stream)
|
||||
|
||||
process.nextTick(() => stream.emit('error', new Error('kaboom')))
|
||||
process.nextTick(() => stream.emit('error', new Error('kaboom')))
|
||||
|
||||
stream.on('error', (err) => {
|
||||
plan.equal(err.message, 'kaboom')
|
||||
})
|
||||
|
||||
await plan
|
||||
})
|
||||
+132
@@ -0,0 +1,132 @@
|
||||
'use strict'
|
||||
const test = require('tape')
|
||||
const pino = require('../browser')
|
||||
|
||||
test('child has parent level', ({ end, same, is }) => {
|
||||
const instance = pino({
|
||||
level: 'error',
|
||||
browser: {}
|
||||
})
|
||||
|
||||
const child = instance.child({})
|
||||
|
||||
same(child.level, instance.level)
|
||||
end()
|
||||
})
|
||||
|
||||
test('child can set level at creation time', ({ end, same, is }) => {
|
||||
const instance = pino({
|
||||
level: 'error',
|
||||
browser: {}
|
||||
})
|
||||
|
||||
const child = instance.child({}, { level: 'info' }) // first bindings, then options
|
||||
|
||||
same(child.level, 'info')
|
||||
end()
|
||||
})
|
||||
|
||||
test('changing child level does not affect parent', ({ end, same, is }) => {
|
||||
const instance = pino({
|
||||
level: 'error',
|
||||
browser: {}
|
||||
})
|
||||
|
||||
const child = instance.child({})
|
||||
child.level = 'info'
|
||||
|
||||
same(instance.level, 'error')
|
||||
end()
|
||||
})
|
||||
|
||||
test('child should log, if its own level allows it', ({ end, same, is }) => {
|
||||
const expected = [
|
||||
{
|
||||
level: 30,
|
||||
msg: 'this is info'
|
||||
},
|
||||
{
|
||||
level: 40,
|
||||
msg: 'this is warn'
|
||||
},
|
||||
{
|
||||
level: 50,
|
||||
msg: 'this is an error'
|
||||
}
|
||||
]
|
||||
const instance = pino({
|
||||
level: 'error',
|
||||
browser: {
|
||||
write (actual) {
|
||||
checkLogObjects(is, same, actual, expected.shift())
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const child = instance.child({})
|
||||
child.level = 'info'
|
||||
|
||||
child.debug('this is debug')
|
||||
child.info('this is info')
|
||||
child.warn('this is warn')
|
||||
child.error('this is an error')
|
||||
|
||||
same(expected.length, 0, 'not all messages were read')
|
||||
end()
|
||||
})
|
||||
|
||||
test('changing child log level should not affect parent log behavior', ({ end, same, is }) => {
|
||||
const expected = [
|
||||
{
|
||||
level: 50,
|
||||
msg: 'this is an error'
|
||||
},
|
||||
{
|
||||
level: 60,
|
||||
msg: 'this is fatal'
|
||||
}
|
||||
]
|
||||
const instance = pino({
|
||||
level: 'error',
|
||||
browser: {
|
||||
write (actual) {
|
||||
checkLogObjects(is, same, actual, expected.shift())
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const child = instance.child({})
|
||||
child.level = 'info'
|
||||
|
||||
instance.warn('this is warn')
|
||||
instance.error('this is an error')
|
||||
instance.fatal('this is fatal')
|
||||
|
||||
same(expected.length, 0, 'not all messages were read')
|
||||
end()
|
||||
})
|
||||
|
||||
test('onChild callback should be called when new child is created', ({ end, pass, plan }) => {
|
||||
plan(1)
|
||||
const instance = pino({
|
||||
level: 'error',
|
||||
browser: {},
|
||||
onChild: (_child) => {
|
||||
pass('onChild callback was called')
|
||||
end()
|
||||
}
|
||||
})
|
||||
|
||||
instance.child({})
|
||||
})
|
||||
|
||||
function checkLogObjects (is, same, actual, expected) {
|
||||
is(actual.time <= Date.now(), true, 'time is greater than Date.now()')
|
||||
|
||||
const actualCopy = Object.assign({}, actual)
|
||||
const expectedCopy = Object.assign({}, expected)
|
||||
delete actualCopy.time
|
||||
delete expectedCopy.time
|
||||
|
||||
same(actualCopy, expectedCopy)
|
||||
}
|
||||
+87
@@ -0,0 +1,87 @@
|
||||
'use strict'
|
||||
const test = require('tape')
|
||||
const pino = require('../browser')
|
||||
|
||||
test('set browser opts disabled to true', ({ end, same }) => {
|
||||
const instance = pino({
|
||||
browser: {
|
||||
disabled: true,
|
||||
write (actual) {
|
||||
checkLogObjects(same, actual, [])
|
||||
}
|
||||
}
|
||||
})
|
||||
instance.info('hello world')
|
||||
instance.error('this is an error')
|
||||
instance.fatal('this is fatal')
|
||||
|
||||
end()
|
||||
})
|
||||
|
||||
test('set browser opts disabled to false', ({ end, same }) => {
|
||||
const expected = [
|
||||
{
|
||||
level: 30,
|
||||
msg: 'hello world'
|
||||
},
|
||||
{
|
||||
level: 50,
|
||||
msg: 'this is an error'
|
||||
},
|
||||
{
|
||||
level: 60,
|
||||
msg: 'this is fatal'
|
||||
}
|
||||
]
|
||||
const instance = pino({
|
||||
browser: {
|
||||
disabled: false,
|
||||
write (actual) {
|
||||
checkLogObjects(same, actual, expected.shift())
|
||||
}
|
||||
}
|
||||
})
|
||||
instance.info('hello world')
|
||||
instance.error('this is an error')
|
||||
instance.fatal('this is fatal')
|
||||
|
||||
end()
|
||||
})
|
||||
|
||||
test('disabled is not set in browser opts', ({ end, same }) => {
|
||||
const expected = [
|
||||
{
|
||||
level: 30,
|
||||
msg: 'hello world'
|
||||
},
|
||||
{
|
||||
level: 50,
|
||||
msg: 'this is an error'
|
||||
},
|
||||
{
|
||||
level: 60,
|
||||
msg: 'this is fatal'
|
||||
}
|
||||
]
|
||||
const instance = pino({
|
||||
browser: {
|
||||
write (actual) {
|
||||
checkLogObjects(same, actual, expected.shift())
|
||||
}
|
||||
}
|
||||
})
|
||||
instance.info('hello world')
|
||||
instance.error('this is an error')
|
||||
instance.fatal('this is fatal')
|
||||
|
||||
end()
|
||||
})
|
||||
|
||||
function checkLogObjects (same, actual, expected, is) {
|
||||
const actualCopy = Object.assign({}, actual)
|
||||
const expectedCopy = Object.assign({}, expected)
|
||||
delete actualCopy.time
|
||||
delete expectedCopy.time
|
||||
|
||||
same(actualCopy, expectedCopy)
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
'use strict'
|
||||
Object.freeze(console)
|
||||
const test = require('tape')
|
||||
const pino = require('../browser')
|
||||
|
||||
test('silent level', ({ end, fail, pass }) => {
|
||||
pino({
|
||||
level: 'silent',
|
||||
browser: { }
|
||||
})
|
||||
end()
|
||||
})
|
||||
+101
@@ -0,0 +1,101 @@
|
||||
'use strict'
|
||||
|
||||
const { describe, test } = require('node:test')
|
||||
const assert = require('node:assert')
|
||||
const pino = require('../browser')
|
||||
|
||||
const customLevels = {
|
||||
trace: 10,
|
||||
debug: 20,
|
||||
info: 30,
|
||||
warn: 40,
|
||||
error: 50,
|
||||
fatal: 60
|
||||
}
|
||||
|
||||
describe('Default levels suite', () => {
|
||||
test('can check if current level enabled', async () => {
|
||||
const log = pino({ level: 'debug' })
|
||||
assert.equal(true, log.isLevelEnabled('debug'))
|
||||
})
|
||||
|
||||
test('can check if current level enabled when as object', async () => {
|
||||
const log = pino({ asObject: true, level: 'debug' })
|
||||
assert.equal(true, log.isLevelEnabled('debug'))
|
||||
})
|
||||
|
||||
test('can check if level enabled after level set', async () => {
|
||||
const log = pino()
|
||||
assert.equal(false, log.isLevelEnabled('debug'))
|
||||
log.level = 'debug'
|
||||
assert.equal(true, log.isLevelEnabled('debug'))
|
||||
})
|
||||
|
||||
test('can check if higher level enabled', async () => {
|
||||
const log = pino({ level: 'debug' })
|
||||
assert.equal(true, log.isLevelEnabled('error'))
|
||||
})
|
||||
|
||||
test('can check if lower level is disabled', async () => {
|
||||
const log = pino({ level: 'error' })
|
||||
assert.equal(false, log.isLevelEnabled('trace'))
|
||||
})
|
||||
|
||||
test('ASC: can check if child has current level enabled', async () => {
|
||||
const log = pino().child({}, { level: 'debug' })
|
||||
assert.equal(true, log.isLevelEnabled('debug'))
|
||||
assert.equal(true, log.isLevelEnabled('error'))
|
||||
assert.equal(false, log.isLevelEnabled('trace'))
|
||||
})
|
||||
|
||||
test('can check if custom level is enabled', async () => {
|
||||
const log = pino({
|
||||
customLevels: { foo: 35 },
|
||||
level: 'debug'
|
||||
})
|
||||
assert.equal(true, log.isLevelEnabled('foo'))
|
||||
assert.equal(true, log.isLevelEnabled('error'))
|
||||
assert.equal(false, log.isLevelEnabled('trace'))
|
||||
})
|
||||
})
|
||||
|
||||
describe('Custom levels suite', () => {
|
||||
test('can check if current level enabled', async () => {
|
||||
const log = pino({ level: 'debug', customLevels })
|
||||
assert.equal(true, log.isLevelEnabled('debug'))
|
||||
})
|
||||
|
||||
test('can check if level enabled after level set', async () => {
|
||||
const log = pino({ customLevels })
|
||||
assert.equal(false, log.isLevelEnabled('debug'))
|
||||
log.level = 'debug'
|
||||
assert.equal(true, log.isLevelEnabled('debug'))
|
||||
})
|
||||
|
||||
test('can check if higher level enabled', async () => {
|
||||
const log = pino({ level: 'debug', customLevels })
|
||||
assert.equal(true, log.isLevelEnabled('error'))
|
||||
})
|
||||
|
||||
test('can check if lower level is disabled', async () => {
|
||||
const log = pino({ level: 'error', customLevels })
|
||||
assert.equal(false, log.isLevelEnabled('trace'))
|
||||
})
|
||||
|
||||
test('can check if child has current level enabled', async () => {
|
||||
const log = pino().child({ customLevels }, { level: 'debug' })
|
||||
assert.equal(true, log.isLevelEnabled('debug'))
|
||||
assert.equal(true, log.isLevelEnabled('error'))
|
||||
assert.equal(false, log.isLevelEnabled('trace'))
|
||||
})
|
||||
|
||||
test('can check if custom level is enabled', async () => {
|
||||
const log = pino({
|
||||
customLevels: { foo: 35, ...customLevels },
|
||||
level: 'debug'
|
||||
})
|
||||
assert.equal(true, log.isLevelEnabled('foo'))
|
||||
assert.equal(true, log.isLevelEnabled('error'))
|
||||
assert.equal(false, log.isLevelEnabled('trace'))
|
||||
})
|
||||
})
|
||||
+241
@@ -0,0 +1,241 @@
|
||||
'use strict'
|
||||
const test = require('tape')
|
||||
const pino = require('../browser')
|
||||
|
||||
test('set the level by string', ({ end, same, is }) => {
|
||||
const expected = [
|
||||
{
|
||||
level: 50,
|
||||
msg: 'this is an error'
|
||||
},
|
||||
{
|
||||
level: 60,
|
||||
msg: 'this is fatal'
|
||||
}
|
||||
]
|
||||
const instance = pino({
|
||||
browser: {
|
||||
write (actual) {
|
||||
checkLogObjects(is, same, actual, expected.shift())
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
instance.level = 'error'
|
||||
instance.info('hello world')
|
||||
instance.error('this is an error')
|
||||
instance.fatal('this is fatal')
|
||||
|
||||
end()
|
||||
})
|
||||
|
||||
test('set the level by string. init with silent', ({ end, same, is }) => {
|
||||
const expected = [
|
||||
{
|
||||
level: 50,
|
||||
msg: 'this is an error'
|
||||
},
|
||||
{
|
||||
level: 60,
|
||||
msg: 'this is fatal'
|
||||
}
|
||||
]
|
||||
const instance = pino({
|
||||
level: 'silent',
|
||||
browser: {
|
||||
write (actual) {
|
||||
checkLogObjects(is, same, actual, expected.shift())
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
instance.level = 'error'
|
||||
instance.info('hello world')
|
||||
instance.error('this is an error')
|
||||
instance.fatal('this is fatal')
|
||||
|
||||
end()
|
||||
})
|
||||
|
||||
test('set the level by string. init with silent and transmit', ({ end, same, is }) => {
|
||||
const expected = [
|
||||
{
|
||||
level: 50,
|
||||
msg: 'this is an error'
|
||||
},
|
||||
{
|
||||
level: 60,
|
||||
msg: 'this is fatal'
|
||||
}
|
||||
]
|
||||
const instance = pino({
|
||||
level: 'silent',
|
||||
browser: {
|
||||
write (actual) {
|
||||
checkLogObjects(is, same, actual, expected.shift())
|
||||
}
|
||||
},
|
||||
transmit: {
|
||||
send () {}
|
||||
}
|
||||
})
|
||||
|
||||
instance.level = 'error'
|
||||
instance.info('hello world')
|
||||
instance.error('this is an error')
|
||||
instance.fatal('this is fatal')
|
||||
|
||||
end()
|
||||
})
|
||||
|
||||
test('set the level via constructor', ({ end, same, is }) => {
|
||||
const expected = [
|
||||
{
|
||||
level: 50,
|
||||
msg: 'this is an error'
|
||||
},
|
||||
{
|
||||
level: 60,
|
||||
msg: 'this is fatal'
|
||||
}
|
||||
]
|
||||
const instance = pino({
|
||||
level: 'error',
|
||||
browser: {
|
||||
write (actual) {
|
||||
checkLogObjects(is, same, actual, expected.shift())
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
instance.info('hello world')
|
||||
instance.error('this is an error')
|
||||
instance.fatal('this is fatal')
|
||||
|
||||
end()
|
||||
})
|
||||
|
||||
test('set custom level and use it', ({ end, same, is }) => {
|
||||
const expected = [
|
||||
{
|
||||
level: 31,
|
||||
msg: 'this is a custom level'
|
||||
}
|
||||
]
|
||||
const instance = pino({
|
||||
customLevels: {
|
||||
success: 31
|
||||
},
|
||||
browser: {
|
||||
write (actual) {
|
||||
checkLogObjects(is, same, actual, expected.shift())
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
instance.success('this is a custom level')
|
||||
|
||||
end()
|
||||
})
|
||||
|
||||
test('the wrong level throws', ({ end, throws }) => {
|
||||
const instance = pino()
|
||||
throws(() => {
|
||||
instance.level = 'kaboom'
|
||||
})
|
||||
end()
|
||||
})
|
||||
|
||||
test('the wrong level by number throws', ({ end, throws }) => {
|
||||
const instance = pino()
|
||||
throws(() => {
|
||||
instance.levelVal = 55
|
||||
})
|
||||
end()
|
||||
})
|
||||
|
||||
test('exposes level string mappings', ({ end, is }) => {
|
||||
is(pino.levels.values.error, 50)
|
||||
end()
|
||||
})
|
||||
|
||||
test('exposes level number mappings', ({ end, is }) => {
|
||||
is(pino.levels.labels[50], 'error')
|
||||
end()
|
||||
})
|
||||
|
||||
test('returns level integer', ({ end, is }) => {
|
||||
const instance = pino({ level: 'error' })
|
||||
is(instance.levelVal, 50)
|
||||
end()
|
||||
})
|
||||
|
||||
test('silent level via constructor', ({ end, fail }) => {
|
||||
const instance = pino({
|
||||
level: 'silent',
|
||||
browser: {
|
||||
write () {
|
||||
fail('no data should be logged')
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
Object.keys(pino.levels.values).forEach((level) => {
|
||||
instance[level]('hello world')
|
||||
})
|
||||
|
||||
end()
|
||||
})
|
||||
|
||||
test('silent level by string', ({ end, fail }) => {
|
||||
const instance = pino({
|
||||
browser: {
|
||||
write () {
|
||||
fail('no data should be logged')
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
instance.level = 'silent'
|
||||
|
||||
Object.keys(pino.levels.values).forEach((level) => {
|
||||
instance[level]('hello world')
|
||||
})
|
||||
|
||||
end()
|
||||
})
|
||||
|
||||
test('exposed levels', ({ end, same }) => {
|
||||
same(Object.keys(pino.levels.values), [
|
||||
'fatal',
|
||||
'error',
|
||||
'warn',
|
||||
'info',
|
||||
'debug',
|
||||
'trace'
|
||||
])
|
||||
end()
|
||||
})
|
||||
|
||||
test('exposed labels', ({ end, same }) => {
|
||||
same(Object.keys(pino.levels.labels), [
|
||||
'10',
|
||||
'20',
|
||||
'30',
|
||||
'40',
|
||||
'50',
|
||||
'60'
|
||||
])
|
||||
end()
|
||||
})
|
||||
|
||||
function checkLogObjects (is, same, actual, expected) {
|
||||
is(actual.time <= Date.now(), true, 'time is greater than Date.now()')
|
||||
|
||||
const actualCopy = Object.assign({}, actual)
|
||||
const expectedCopy = Object.assign({}, expected)
|
||||
delete actualCopy.time
|
||||
delete expectedCopy.time
|
||||
|
||||
same(actualCopy, expectedCopy)
|
||||
}
|
||||
+352
@@ -0,0 +1,352 @@
|
||||
'use strict'
|
||||
// eslint-disable-next-line
|
||||
if (typeof $1 !== 'undefined') $1 = arguments.callee.caller.arguments[0]
|
||||
|
||||
const test = require('tape')
|
||||
const fresh = require('import-fresh')
|
||||
const pino = require('../browser')
|
||||
|
||||
const parentSerializers = {
|
||||
test: () => 'parent'
|
||||
}
|
||||
|
||||
const childSerializers = {
|
||||
test: () => 'child'
|
||||
}
|
||||
|
||||
test('serializers override values', ({ end, is }) => {
|
||||
const parent = pino({
|
||||
serializers: parentSerializers,
|
||||
browser: {
|
||||
serialize: true,
|
||||
write (o) {
|
||||
is(o.test, 'parent')
|
||||
end()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
parent.fatal({ test: 'test' })
|
||||
})
|
||||
|
||||
test('without the serialize option, serializers do not override values', ({ end, is }) => {
|
||||
const parent = pino({
|
||||
serializers: parentSerializers,
|
||||
browser: {
|
||||
write (o) {
|
||||
is(o.test, 'test')
|
||||
end()
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
parent.fatal({ test: 'test' })
|
||||
})
|
||||
|
||||
if (process.title !== 'browser') {
|
||||
test('if serialize option is true, standard error serializer is auto enabled', ({ end, same }) => {
|
||||
const err = Error('test')
|
||||
err.code = 'test'
|
||||
err.type = 'Error' // get that cov
|
||||
const expect = pino.stdSerializers.err(err)
|
||||
|
||||
const consoleError = console.error
|
||||
console.error = function (err) {
|
||||
same(err, expect)
|
||||
}
|
||||
|
||||
const logger = fresh('../browser')({
|
||||
browser: { serialize: true }
|
||||
})
|
||||
|
||||
console.error = consoleError
|
||||
|
||||
logger.fatal(err)
|
||||
end()
|
||||
})
|
||||
|
||||
test('if serialize option is array, standard error serializer is auto enabled', ({ end, same }) => {
|
||||
const err = Error('test')
|
||||
err.code = 'test'
|
||||
const expect = pino.stdSerializers.err(err)
|
||||
|
||||
const consoleError = console.error
|
||||
console.error = function (err) {
|
||||
same(err, expect)
|
||||
}
|
||||
|
||||
const logger = fresh('../browser', require)({
|
||||
browser: { serialize: [] }
|
||||
})
|
||||
|
||||
console.error = consoleError
|
||||
|
||||
logger.fatal(err)
|
||||
end()
|
||||
})
|
||||
|
||||
test('if serialize option is array containing !stdSerializers.err, standard error serializer is disabled', ({ end, is }) => {
|
||||
const err = Error('test')
|
||||
err.code = 'test'
|
||||
const expect = err
|
||||
|
||||
const consoleError = console.error
|
||||
console.error = function (err) {
|
||||
is(err, expect)
|
||||
}
|
||||
|
||||
const logger = fresh('../browser', require)({
|
||||
browser: { serialize: ['!stdSerializers.err'] }
|
||||
})
|
||||
|
||||
console.error = consoleError
|
||||
|
||||
logger.fatal(err)
|
||||
end()
|
||||
})
|
||||
|
||||
test('in browser, serializers apply to all objects', ({ end, is }) => {
|
||||
const consoleError = console.error
|
||||
console.error = function (test, test2, test3, test4, test5) {
|
||||
is(test.key, 'serialized')
|
||||
is(test2.key2, 'serialized2')
|
||||
is(test5.key3, 'serialized3')
|
||||
}
|
||||
|
||||
const logger = fresh('../browser', require)({
|
||||
serializers: {
|
||||
key: () => 'serialized',
|
||||
key2: () => 'serialized2',
|
||||
key3: () => 'serialized3'
|
||||
},
|
||||
browser: { serialize: true }
|
||||
})
|
||||
|
||||
console.error = consoleError
|
||||
|
||||
logger.fatal({ key: 'test' }, { key2: 'test' }, 'str should skip', [{ foo: 'array should skip' }], { key3: 'test' })
|
||||
end()
|
||||
})
|
||||
|
||||
test('serialize can be an array of selected serializers', ({ end, is }) => {
|
||||
const consoleError = console.error
|
||||
console.error = function (test, test2, test3, test4, test5) {
|
||||
is(test.key, 'test')
|
||||
is(test2.key2, 'serialized2')
|
||||
is(test5.key3, 'test')
|
||||
}
|
||||
|
||||
const logger = fresh('../browser', require)({
|
||||
serializers: {
|
||||
key: () => 'serialized',
|
||||
key2: () => 'serialized2',
|
||||
key3: () => 'serialized3'
|
||||
},
|
||||
browser: { serialize: ['key2'] }
|
||||
})
|
||||
|
||||
console.error = consoleError
|
||||
|
||||
logger.fatal({ key: 'test' }, { key2: 'test' }, 'str should skip', [{ foo: 'array should skip' }], { key3: 'test' })
|
||||
end()
|
||||
})
|
||||
|
||||
test('serialize filter applies to child loggers', ({ end, is }) => {
|
||||
const consoleError = console.error
|
||||
console.error = function (binding, test, test2, test3, test4, test5) {
|
||||
is(test.key, 'test')
|
||||
is(test2.key2, 'serialized2')
|
||||
is(test5.key3, 'test')
|
||||
}
|
||||
|
||||
const logger = fresh('../browser', require)({
|
||||
browser: { serialize: ['key2'] }
|
||||
})
|
||||
|
||||
console.error = consoleError
|
||||
|
||||
logger.child({
|
||||
aBinding: 'test'
|
||||
}, {
|
||||
serializers: {
|
||||
key: () => 'serialized',
|
||||
key2: () => 'serialized2',
|
||||
key3: () => 'serialized3'
|
||||
}
|
||||
}).fatal({ key: 'test' }, { key2: 'test' }, 'str should skip', [{ foo: 'array should skip' }], { key3: 'test' })
|
||||
end()
|
||||
})
|
||||
|
||||
test('serialize filter applies to child loggers through bindings', ({ end, is }) => {
|
||||
const consoleError = console.error
|
||||
console.error = function (binding, test, test2, test3, test4, test5) {
|
||||
is(test.key, 'test')
|
||||
is(test2.key2, 'serialized2')
|
||||
is(test5.key3, 'test')
|
||||
}
|
||||
|
||||
const logger = fresh('../browser', require)({
|
||||
browser: { serialize: ['key2'] }
|
||||
})
|
||||
|
||||
console.error = consoleError
|
||||
|
||||
logger.child({
|
||||
aBinding: 'test',
|
||||
serializers: {
|
||||
key: () => 'serialized',
|
||||
key2: () => 'serialized2',
|
||||
key3: () => 'serialized3'
|
||||
}
|
||||
}).fatal({ key: 'test' }, { key2: 'test' }, 'str should skip', [{ foo: 'array should skip' }], { key3: 'test' })
|
||||
end()
|
||||
})
|
||||
|
||||
test('parent serializers apply to child bindings', ({ end, is }) => {
|
||||
const consoleError = console.error
|
||||
console.error = function (binding) {
|
||||
is(binding.key, 'serialized')
|
||||
}
|
||||
|
||||
const logger = fresh('../browser', require)({
|
||||
serializers: {
|
||||
key: () => 'serialized'
|
||||
},
|
||||
browser: { serialize: true }
|
||||
})
|
||||
|
||||
console.error = consoleError
|
||||
|
||||
logger.child({ key: 'test' }).fatal({ test: 'test' })
|
||||
end()
|
||||
})
|
||||
|
||||
test('child serializers apply to child bindings', ({ end, is }) => {
|
||||
const consoleError = console.error
|
||||
console.error = function (binding) {
|
||||
is(binding.key, 'serialized')
|
||||
}
|
||||
|
||||
const logger = fresh('../browser', require)({
|
||||
browser: { serialize: true }
|
||||
})
|
||||
|
||||
console.error = consoleError
|
||||
|
||||
logger.child({
|
||||
key: 'test'
|
||||
}, {
|
||||
serializers: {
|
||||
key: () => 'serialized'
|
||||
}
|
||||
}).fatal({ test: 'test' })
|
||||
end()
|
||||
})
|
||||
}
|
||||
|
||||
test('child does not overwrite parent serializers', ({ end, is }) => {
|
||||
let c = 0
|
||||
const parent = pino({
|
||||
serializers: parentSerializers,
|
||||
browser: {
|
||||
serialize: true,
|
||||
write (o) {
|
||||
c++
|
||||
if (c === 1) is(o.test, 'parent')
|
||||
if (c === 2) {
|
||||
is(o.test, 'child')
|
||||
end()
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
const child = parent.child({}, { serializers: childSerializers })
|
||||
|
||||
parent.fatal({ test: 'test' })
|
||||
child.fatal({ test: 'test' })
|
||||
})
|
||||
|
||||
test('children inherit parent serializers', ({ end, is }) => {
|
||||
const parent = pino({
|
||||
serializers: parentSerializers,
|
||||
browser: {
|
||||
serialize: true,
|
||||
write (o) {
|
||||
is(o.test, 'parent')
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const child = parent.child({ a: 'property' })
|
||||
child.fatal({ test: 'test' })
|
||||
end()
|
||||
})
|
||||
|
||||
test('children serializers get called', ({ end, is }) => {
|
||||
const parent = pino({
|
||||
browser: {
|
||||
serialize: true,
|
||||
write (o) {
|
||||
is(o.test, 'child')
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const child = parent.child({ a: 'property' }, { serializers: childSerializers })
|
||||
|
||||
child.fatal({ test: 'test' })
|
||||
end()
|
||||
})
|
||||
|
||||
test('children serializers get called when inherited from parent', ({ end, is }) => {
|
||||
const parent = pino({
|
||||
serializers: parentSerializers,
|
||||
browser: {
|
||||
serialize: true,
|
||||
write: (o) => {
|
||||
is(o.test, 'pass')
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const child = parent.child({}, { serializers: { test: () => 'pass' } })
|
||||
|
||||
child.fatal({ test: 'fail' })
|
||||
end()
|
||||
})
|
||||
|
||||
test('non overridden serializers are available in the children', ({ end, is }) => {
|
||||
const pSerializers = {
|
||||
onlyParent: () => 'parent',
|
||||
shared: () => 'parent'
|
||||
}
|
||||
|
||||
const cSerializers = {
|
||||
shared: () => 'child',
|
||||
onlyChild: () => 'child'
|
||||
}
|
||||
|
||||
let c = 0
|
||||
|
||||
const parent = pino({
|
||||
serializers: pSerializers,
|
||||
browser: {
|
||||
serialize: true,
|
||||
write (o) {
|
||||
c++
|
||||
if (c === 1) is(o.shared, 'child')
|
||||
if (c === 2) is(o.onlyParent, 'parent')
|
||||
if (c === 3) is(o.onlyChild, 'child')
|
||||
if (c === 4) is(o.onlyChild, 'test')
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const child = parent.child({}, { serializers: cSerializers })
|
||||
|
||||
child.fatal({ shared: 'test' })
|
||||
child.fatal({ onlyParent: 'test' })
|
||||
child.fatal({ onlyChild: 'test' })
|
||||
parent.fatal({ onlyChild: 'test' })
|
||||
end()
|
||||
})
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
'use strict'
|
||||
const test = require('tape')
|
||||
const pino = require('../browser')
|
||||
|
||||
Date.now = () => 1599400603614
|
||||
|
||||
test('null timestamp', ({ end, is }) => {
|
||||
const instance = pino({
|
||||
timestamp: pino.stdTimeFunctions.nullTime,
|
||||
browser: {
|
||||
asObject: true,
|
||||
write: function (o) {
|
||||
is(o.time, undefined)
|
||||
}
|
||||
}
|
||||
})
|
||||
instance.info('hello world')
|
||||
end()
|
||||
})
|
||||
|
||||
test('iso timestamp', ({ end, is }) => {
|
||||
const instance = pino({
|
||||
timestamp: pino.stdTimeFunctions.isoTime,
|
||||
browser: {
|
||||
asObject: true,
|
||||
write: function (o) {
|
||||
is(o.time, '2020-09-06T13:56:43.614Z')
|
||||
}
|
||||
}
|
||||
})
|
||||
instance.info('hello world')
|
||||
end()
|
||||
})
|
||||
|
||||
test('epoch timestamp', ({ end, is }) => {
|
||||
const instance = pino({
|
||||
timestamp: pino.stdTimeFunctions.epochTime,
|
||||
browser: {
|
||||
asObject: true,
|
||||
write: function (o) {
|
||||
is(o.time, 1599400603614)
|
||||
}
|
||||
}
|
||||
})
|
||||
instance.info('hello world')
|
||||
end()
|
||||
})
|
||||
|
||||
test('unix timestamp', ({ end, is }) => {
|
||||
const instance = pino({
|
||||
timestamp: pino.stdTimeFunctions.unixTime,
|
||||
browser: {
|
||||
asObject: true,
|
||||
write: function (o) {
|
||||
is(o.time, Math.round(1599400603614 / 1000.0))
|
||||
}
|
||||
}
|
||||
})
|
||||
instance.info('hello world')
|
||||
end()
|
||||
})
|
||||
|
||||
test('epoch timestamp by default', ({ end, is }) => {
|
||||
const instance = pino({
|
||||
browser: {
|
||||
asObject: true,
|
||||
write: function (o) {
|
||||
is(o.time, 1599400603614)
|
||||
}
|
||||
}
|
||||
})
|
||||
instance.info('hello world')
|
||||
end()
|
||||
})
|
||||
|
||||
test('not print timestamp if the option is false', ({ end, is }) => {
|
||||
const instance = pino({
|
||||
timestamp: false,
|
||||
browser: {
|
||||
asObject: true,
|
||||
write: function (o) {
|
||||
is(o.time, undefined)
|
||||
}
|
||||
}
|
||||
})
|
||||
instance.info('hello world')
|
||||
end()
|
||||
})
|
||||
+417
@@ -0,0 +1,417 @@
|
||||
'use strict'
|
||||
const test = require('tape')
|
||||
const pino = require('../browser')
|
||||
|
||||
function noop () {}
|
||||
|
||||
test('throws if transmit object does not have send function', ({ end, throws }) => {
|
||||
throws(() => {
|
||||
pino({ browser: { transmit: {} } })
|
||||
})
|
||||
|
||||
throws(() => {
|
||||
pino({ browser: { transmit: { send: 'not a func' } } })
|
||||
})
|
||||
|
||||
end()
|
||||
})
|
||||
|
||||
test('calls send function after write', ({ end, is }) => {
|
||||
let c = 0
|
||||
const logger = pino({
|
||||
browser: {
|
||||
write: () => {
|
||||
c++
|
||||
},
|
||||
transmit: {
|
||||
send () { is(c, 1) }
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
logger.fatal({ test: 'test' })
|
||||
end()
|
||||
})
|
||||
|
||||
test('passes send function the logged level', ({ end, is }) => {
|
||||
const logger = pino({
|
||||
browser: {
|
||||
write () {},
|
||||
transmit: {
|
||||
send (level) {
|
||||
is(level, 'fatal')
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
logger.fatal({ test: 'test' })
|
||||
end()
|
||||
})
|
||||
|
||||
test('passes send function message strings in logEvent object when asObject is not set', ({ end, same, is }) => {
|
||||
const logger = pino({
|
||||
browser: {
|
||||
write: noop,
|
||||
transmit: {
|
||||
send (level, { messages }) {
|
||||
is(messages[0], 'test')
|
||||
is(messages[1], 'another test')
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
logger.fatal('test', 'another test')
|
||||
|
||||
end()
|
||||
})
|
||||
|
||||
test('passes send function message objects in logEvent object when asObject is not set', ({ end, same, is }) => {
|
||||
const logger = pino({
|
||||
browser: {
|
||||
write: noop,
|
||||
transmit: {
|
||||
send (level, { messages }) {
|
||||
same(messages[0], { test: 'test' })
|
||||
is(messages[1], 'another test')
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
logger.fatal({ test: 'test' }, 'another test')
|
||||
|
||||
end()
|
||||
})
|
||||
|
||||
test('passes send function message strings in logEvent object when asObject is set', ({ end, same, is }) => {
|
||||
const logger = pino({
|
||||
browser: {
|
||||
asObject: true,
|
||||
write: noop,
|
||||
transmit: {
|
||||
send (level, { messages }) {
|
||||
is(messages[0], 'test')
|
||||
is(messages[1], 'another test')
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
logger.fatal('test', 'another test')
|
||||
|
||||
end()
|
||||
})
|
||||
|
||||
test('passes send function message objects in logEvent object when asObject is set', ({ end, same, is }) => {
|
||||
const logger = pino({
|
||||
browser: {
|
||||
asObject: true,
|
||||
write: noop,
|
||||
transmit: {
|
||||
send (level, { messages }) {
|
||||
same(messages[0], { test: 'test' })
|
||||
is(messages[1], 'another test')
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
logger.fatal({ test: 'test' }, 'another test')
|
||||
|
||||
end()
|
||||
})
|
||||
|
||||
test('supplies a timestamp (ts) in logEvent object which is exactly the same as the `time` property in asObject mode', ({ end, is }) => {
|
||||
let expected
|
||||
const logger = pino({
|
||||
browser: {
|
||||
asObject: true, // implicit because `write`, but just to be explicit
|
||||
write (o) {
|
||||
expected = o.time
|
||||
},
|
||||
transmit: {
|
||||
send (level, logEvent) {
|
||||
is(logEvent.ts, expected)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
logger.fatal('test')
|
||||
end()
|
||||
})
|
||||
|
||||
test('passes send function child bindings via logEvent object', ({ end, same, is }) => {
|
||||
const logger = pino({
|
||||
browser: {
|
||||
write: noop,
|
||||
transmit: {
|
||||
send (level, logEvent) {
|
||||
const messages = logEvent.messages
|
||||
const bindings = logEvent.bindings
|
||||
same(bindings[0], { first: 'binding' })
|
||||
same(bindings[1], { second: 'binding2' })
|
||||
same(messages[0], { test: 'test' })
|
||||
is(messages[1], 'another test')
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
logger
|
||||
.child({ first: 'binding' })
|
||||
.child({ second: 'binding2' })
|
||||
.fatal({ test: 'test' }, 'another test')
|
||||
end()
|
||||
})
|
||||
|
||||
test('passes send function level:{label, value} via logEvent object', ({ end, is }) => {
|
||||
const logger = pino({
|
||||
browser: {
|
||||
write: noop,
|
||||
transmit: {
|
||||
send (level, logEvent) {
|
||||
const label = logEvent.level.label
|
||||
const value = logEvent.level.value
|
||||
|
||||
is(label, 'fatal')
|
||||
is(value, 60)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
logger.fatal({ test: 'test' }, 'another test')
|
||||
end()
|
||||
})
|
||||
|
||||
test('calls send function according to transmit.level', ({ end, is }) => {
|
||||
let c = 0
|
||||
const logger = pino({
|
||||
browser: {
|
||||
write: noop,
|
||||
transmit: {
|
||||
level: 'error',
|
||||
send (level) {
|
||||
c++
|
||||
if (c === 1) is(level, 'error')
|
||||
if (c === 2) is(level, 'fatal')
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
logger.warn('ignored')
|
||||
logger.error('test')
|
||||
logger.fatal('test')
|
||||
end()
|
||||
})
|
||||
|
||||
test('transmit.level defaults to logger level', ({ end, is }) => {
|
||||
let c = 0
|
||||
const logger = pino({
|
||||
level: 'error',
|
||||
browser: {
|
||||
write: noop,
|
||||
transmit: {
|
||||
send (level) {
|
||||
c++
|
||||
if (c === 1) is(level, 'error')
|
||||
if (c === 2) is(level, 'fatal')
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
logger.warn('ignored')
|
||||
logger.error('test')
|
||||
logger.fatal('test')
|
||||
end()
|
||||
})
|
||||
|
||||
test('transmit.level is effective even if lower than logger level', ({ end, is }) => {
|
||||
let c = 0
|
||||
const logger = pino({
|
||||
level: 'error',
|
||||
browser: {
|
||||
write: noop,
|
||||
transmit: {
|
||||
level: 'info',
|
||||
send (level) {
|
||||
c++
|
||||
if (c === 1) is(level, 'warn')
|
||||
if (c === 2) is(level, 'error')
|
||||
if (c === 3) is(level, 'fatal')
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
logger.warn('ignored')
|
||||
logger.error('test')
|
||||
logger.fatal('test')
|
||||
end()
|
||||
})
|
||||
|
||||
test('applies all serializers to messages and bindings (serialize:false - default)', ({ end, same, is }) => {
|
||||
const logger = pino({
|
||||
serializers: {
|
||||
first: () => 'first',
|
||||
second: () => 'second',
|
||||
test: () => 'serialize it'
|
||||
},
|
||||
browser: {
|
||||
write: noop,
|
||||
transmit: {
|
||||
send (level, logEvent) {
|
||||
const messages = logEvent.messages
|
||||
const bindings = logEvent.bindings
|
||||
same(bindings[0], { first: 'first' })
|
||||
same(bindings[1], { second: 'second' })
|
||||
same(messages[0], { test: 'serialize it' })
|
||||
is(messages[1].type, 'Error')
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
logger
|
||||
.child({ first: 'binding' })
|
||||
.child({ second: 'binding2' })
|
||||
.fatal({ test: 'test' }, Error())
|
||||
end()
|
||||
})
|
||||
|
||||
test('applies all serializers to messages and bindings (serialize:true)', ({ end, same, is }) => {
|
||||
const logger = pino({
|
||||
serializers: {
|
||||
first: () => 'first',
|
||||
second: () => 'second',
|
||||
test: () => 'serialize it'
|
||||
},
|
||||
browser: {
|
||||
serialize: true,
|
||||
write: noop,
|
||||
transmit: {
|
||||
send (level, logEvent) {
|
||||
const messages = logEvent.messages
|
||||
const bindings = logEvent.bindings
|
||||
same(bindings[0], { first: 'first' })
|
||||
same(bindings[1], { second: 'second' })
|
||||
same(messages[0], { test: 'serialize it' })
|
||||
is(messages[1].type, 'Error')
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
logger
|
||||
.child({ first: 'binding' })
|
||||
.child({ second: 'binding2' })
|
||||
.fatal({ test: 'test' }, Error())
|
||||
end()
|
||||
})
|
||||
|
||||
test('extracts correct bindings and raw messages over multiple transmits', ({ end, same, is }) => {
|
||||
let messages = null
|
||||
let bindings = null
|
||||
|
||||
const logger = pino({
|
||||
browser: {
|
||||
write: noop,
|
||||
transmit: {
|
||||
send (level, logEvent) {
|
||||
messages = logEvent.messages
|
||||
bindings = logEvent.bindings
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const child = logger.child({ child: true })
|
||||
const grandchild = child.child({ grandchild: true })
|
||||
|
||||
logger.fatal({ test: 'parent:test1' })
|
||||
logger.fatal({ test: 'parent:test2' })
|
||||
same([], bindings)
|
||||
same([{ test: 'parent:test2' }], messages)
|
||||
|
||||
child.fatal({ test: 'child:test1' })
|
||||
child.fatal({ test: 'child:test2' })
|
||||
same([{ child: true }], bindings)
|
||||
same([{ test: 'child:test2' }], messages)
|
||||
|
||||
grandchild.fatal({ test: 'grandchild:test1' })
|
||||
grandchild.fatal({ test: 'grandchild:test2' })
|
||||
same([{ child: true }, { grandchild: true }], bindings)
|
||||
same([{ test: 'grandchild:test2' }], messages)
|
||||
|
||||
end()
|
||||
})
|
||||
|
||||
test('does not log below configured level', ({ end, is }) => {
|
||||
let message = null
|
||||
const logger = pino({
|
||||
level: 'info',
|
||||
browser: {
|
||||
write (o) {
|
||||
message = o.msg
|
||||
},
|
||||
transmit: {
|
||||
send () { }
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
logger.debug('this message is silent')
|
||||
is(message, null)
|
||||
|
||||
end()
|
||||
})
|
||||
|
||||
test('silent level prevents logging even with transmit', ({ end, fail }) => {
|
||||
const logger = pino({
|
||||
level: 'silent',
|
||||
browser: {
|
||||
write () {
|
||||
fail('no data should be logged by the write method')
|
||||
},
|
||||
transmit: {
|
||||
send () {
|
||||
fail('no data should be logged by the send method')
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
Object.keys(pino.levels.values).forEach((level) => {
|
||||
logger[level]('ignored')
|
||||
})
|
||||
|
||||
end()
|
||||
})
|
||||
|
||||
test('does not call send when transmit.level is set to silent', ({ end, fail, is }) => {
|
||||
let c = 0
|
||||
const logger = pino({
|
||||
level: 'trace',
|
||||
browser: {
|
||||
write () {
|
||||
c++
|
||||
},
|
||||
transmit: {
|
||||
level: 'silent',
|
||||
send () {
|
||||
fail('no data should be logged by the transmit method')
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const levels = Object.keys(pino.levels.values)
|
||||
levels.forEach((level) => {
|
||||
logger[level]('message')
|
||||
})
|
||||
|
||||
is(c, levels.length, 'write must be called exactly once per level')
|
||||
end()
|
||||
})
|
||||
+679
@@ -0,0 +1,679 @@
|
||||
'use strict'
|
||||
const test = require('tape')
|
||||
const fresh = require('import-fresh')
|
||||
const pinoStdSerializers = require('pino-std-serializers')
|
||||
const pino = require('../browser')
|
||||
|
||||
levelTest('fatal')
|
||||
levelTest('error')
|
||||
levelTest('warn')
|
||||
levelTest('info')
|
||||
levelTest('debug')
|
||||
levelTest('trace')
|
||||
|
||||
test('silent level', ({ end, fail, pass }) => {
|
||||
const instance = pino({
|
||||
level: 'silent',
|
||||
browser: { write: fail }
|
||||
})
|
||||
instance.info('test')
|
||||
const child = instance.child({ test: 'test' })
|
||||
child.info('msg-test')
|
||||
// use setTimeout because setImmediate isn't supported in most browsers
|
||||
setTimeout(() => {
|
||||
pass()
|
||||
end()
|
||||
}, 0)
|
||||
})
|
||||
|
||||
test('enabled false', ({ end, fail, pass }) => {
|
||||
const instance = pino({
|
||||
enabled: false,
|
||||
browser: { write: fail }
|
||||
})
|
||||
instance.info('test')
|
||||
const child = instance.child({ test: 'test' })
|
||||
child.info('msg-test')
|
||||
// use setTimeout because setImmediate isn't supported in most browsers
|
||||
setTimeout(() => {
|
||||
pass()
|
||||
end()
|
||||
}, 0)
|
||||
})
|
||||
|
||||
test('throw if creating child without bindings', ({ end, throws }) => {
|
||||
const instance = pino()
|
||||
throws(() => instance.child())
|
||||
end()
|
||||
})
|
||||
|
||||
test('stubs write, flush and ee methods on instance', ({ end, ok, is }) => {
|
||||
const instance = pino()
|
||||
|
||||
ok(isFunc(instance.setMaxListeners))
|
||||
ok(isFunc(instance.getMaxListeners))
|
||||
ok(isFunc(instance.emit))
|
||||
ok(isFunc(instance.addListener))
|
||||
ok(isFunc(instance.on))
|
||||
ok(isFunc(instance.prependListener))
|
||||
ok(isFunc(instance.once))
|
||||
ok(isFunc(instance.prependOnceListener))
|
||||
ok(isFunc(instance.removeListener))
|
||||
ok(isFunc(instance.removeAllListeners))
|
||||
ok(isFunc(instance.listeners))
|
||||
ok(isFunc(instance.listenerCount))
|
||||
ok(isFunc(instance.eventNames))
|
||||
ok(isFunc(instance.write))
|
||||
ok(isFunc(instance.flush))
|
||||
|
||||
is(instance.on(), undefined)
|
||||
|
||||
end()
|
||||
})
|
||||
|
||||
test('exposes levels object', ({ end, same }) => {
|
||||
same(pino.levels, {
|
||||
values: {
|
||||
fatal: 60,
|
||||
error: 50,
|
||||
warn: 40,
|
||||
info: 30,
|
||||
debug: 20,
|
||||
trace: 10
|
||||
},
|
||||
labels: {
|
||||
10: 'trace',
|
||||
20: 'debug',
|
||||
30: 'info',
|
||||
40: 'warn',
|
||||
50: 'error',
|
||||
60: 'fatal'
|
||||
}
|
||||
})
|
||||
|
||||
end()
|
||||
})
|
||||
|
||||
test('exposes faux stdSerializers', ({ end, ok, same }) => {
|
||||
ok(pino.stdSerializers)
|
||||
// make sure faux stdSerializers match pino-std-serializers
|
||||
for (const serializer in pinoStdSerializers) {
|
||||
ok(pino.stdSerializers[serializer], `pino.stdSerializers.${serializer}`)
|
||||
}
|
||||
// confirm faux methods return empty objects
|
||||
same(pino.stdSerializers.req(), {})
|
||||
same(pino.stdSerializers.mapHttpRequest(), {})
|
||||
same(pino.stdSerializers.mapHttpResponse(), {})
|
||||
same(pino.stdSerializers.res(), {})
|
||||
// confirm wrapping function is a passthrough
|
||||
const noChange = { foo: 'bar', fuz: 42 }
|
||||
same(pino.stdSerializers.wrapRequestSerializer(noChange), noChange)
|
||||
same(pino.stdSerializers.wrapResponseSerializer(noChange), noChange)
|
||||
end()
|
||||
})
|
||||
|
||||
test('exposes err stdSerializer', ({ end, ok }) => {
|
||||
ok(pino.stdSerializers.err)
|
||||
ok(pino.stdSerializers.err(Error()))
|
||||
end()
|
||||
})
|
||||
|
||||
consoleMethodTest('error')
|
||||
consoleMethodTest('fatal', 'error')
|
||||
consoleMethodTest('warn')
|
||||
consoleMethodTest('info')
|
||||
consoleMethodTest('debug')
|
||||
consoleMethodTest('trace')
|
||||
absentConsoleMethodTest('error', 'log')
|
||||
absentConsoleMethodTest('warn', 'error')
|
||||
absentConsoleMethodTest('info', 'log')
|
||||
absentConsoleMethodTest('debug', 'log')
|
||||
absentConsoleMethodTest('trace', 'log')
|
||||
|
||||
// do not run this with airtap
|
||||
if (process.title !== 'browser') {
|
||||
test('in absence of console, log methods become noops', ({ end, ok }) => {
|
||||
const console = global.console
|
||||
delete global.console
|
||||
const instance = fresh('../browser')()
|
||||
global.console = console
|
||||
ok(fnName(instance.log).match(/noop/))
|
||||
ok(fnName(instance.fatal).match(/noop/))
|
||||
ok(fnName(instance.error).match(/noop/))
|
||||
ok(fnName(instance.warn).match(/noop/))
|
||||
ok(fnName(instance.info).match(/noop/))
|
||||
ok(fnName(instance.debug).match(/noop/))
|
||||
ok(fnName(instance.trace).match(/noop/))
|
||||
end()
|
||||
})
|
||||
}
|
||||
|
||||
test('opts.browser.asObject logs pino-like object to console', ({ end, ok, is }) => {
|
||||
const info = console.info
|
||||
console.info = function (o) {
|
||||
is(o.level, 30)
|
||||
is(o.msg, 'test')
|
||||
ok(o.time)
|
||||
console.info = info
|
||||
}
|
||||
const instance = require('../browser')({
|
||||
browser: {
|
||||
asObject: true
|
||||
}
|
||||
})
|
||||
|
||||
instance.info('test')
|
||||
end()
|
||||
})
|
||||
|
||||
test('opts.browser.asObject uses opts.messageKey in logs', ({ end, ok, is }) => {
|
||||
const messageKey = 'message'
|
||||
const instance = require('../browser')({
|
||||
messageKey,
|
||||
browser: {
|
||||
asObject: true,
|
||||
write: function (o) {
|
||||
is(o.level, 30)
|
||||
is(o[messageKey], 'test')
|
||||
ok(o.time)
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
instance.info('test')
|
||||
end()
|
||||
})
|
||||
|
||||
test('opts.browser.asObjectBindingsOnly passes the bindings but keep the message unformatted', ({ end, ok, is, deepEqual }) => {
|
||||
const messageKey = 'message'
|
||||
const instance = require('../browser')({
|
||||
messageKey,
|
||||
browser: {
|
||||
asObjectBindingsOnly: true,
|
||||
write: function (o, msg, ...args) {
|
||||
is(o.level, 30)
|
||||
ok(o.time)
|
||||
is(msg, 'test %s')
|
||||
deepEqual(args, ['foo'])
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
instance.info('test %s', 'foo')
|
||||
end()
|
||||
})
|
||||
|
||||
test('opts.browser.formatters (level) logs pino-like object to console', ({ end, ok, is }) => {
|
||||
const info = console.info
|
||||
console.info = function (o) {
|
||||
is(o.level, 30)
|
||||
is(o.label, 'info')
|
||||
is(o.msg, 'test')
|
||||
ok(o.time)
|
||||
console.info = info
|
||||
}
|
||||
const instance = require('../browser')({
|
||||
browser: {
|
||||
formatters: {
|
||||
level (label, number) {
|
||||
return { label, level: number }
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
instance.info('test')
|
||||
end()
|
||||
})
|
||||
|
||||
test('opts.browser.formatters (log) logs pino-like object to console', ({ end, ok, is }) => {
|
||||
const info = console.info
|
||||
console.info = function (o) {
|
||||
is(o.level, 30)
|
||||
is(o.msg, 'test')
|
||||
is(o.hello, 'world')
|
||||
is(o.newField, 'test')
|
||||
ok(o.time, `Logged at ${o.time}`)
|
||||
console.info = info
|
||||
}
|
||||
const instance = require('../browser')({
|
||||
browser: {
|
||||
formatters: {
|
||||
log (o) {
|
||||
return { ...o, newField: 'test', time: `Logged at ${o.time}` }
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
instance.info({ hello: 'world' }, 'test')
|
||||
end()
|
||||
})
|
||||
|
||||
test('opts.browser.serialize and opts.browser.transmit only serializes log data once', ({ end, ok, is }) => {
|
||||
const instance = require('../browser')({
|
||||
serializers: {
|
||||
extras (data) {
|
||||
return { serializedExtras: data }
|
||||
}
|
||||
},
|
||||
browser: {
|
||||
serialize: ['extras'],
|
||||
transmit: {
|
||||
level: 'info',
|
||||
send (level, o) {
|
||||
is(o.messages[0].extras.serializedExtras, 'world')
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
instance.info({ extras: 'world' }, 'test')
|
||||
end()
|
||||
})
|
||||
|
||||
test('opts.browser.serialize and opts.asObject only serializes log data once', ({ end, ok, is }) => {
|
||||
const instance = require('../browser')({
|
||||
serializers: {
|
||||
extras (data) {
|
||||
return { serializedExtras: data }
|
||||
}
|
||||
},
|
||||
browser: {
|
||||
serialize: ['extras'],
|
||||
asObject: true,
|
||||
write: function (o) {
|
||||
is(o.extras.serializedExtras, 'world')
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
instance.info({ extras: 'world' }, 'test')
|
||||
end()
|
||||
})
|
||||
|
||||
test('opts.browser.serialize, opts.asObject and opts.browser.transmit only serializes log data once', ({ end, ok, is }) => {
|
||||
const instance = require('../browser')({
|
||||
serializers: {
|
||||
extras (data) {
|
||||
return { serializedExtras: data }
|
||||
}
|
||||
},
|
||||
browser: {
|
||||
serialize: ['extras'],
|
||||
asObject: true,
|
||||
transmit: {
|
||||
send (level, o) {
|
||||
is(o.messages[0].extras.serializedExtras, 'world')
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
instance.info({ extras: 'world' }, 'test')
|
||||
end()
|
||||
})
|
||||
|
||||
test('opts.browser.write func log single string', ({ end, ok, is }) => {
|
||||
const instance = pino({
|
||||
browser: {
|
||||
write: function (o) {
|
||||
is(o.level, 30)
|
||||
is(o.msg, 'test')
|
||||
ok(o.time)
|
||||
}
|
||||
}
|
||||
})
|
||||
instance.info('test')
|
||||
|
||||
end()
|
||||
})
|
||||
|
||||
test('opts.browser.write func string joining', ({ end, ok, is }) => {
|
||||
const instance = pino({
|
||||
browser: {
|
||||
write: function (o) {
|
||||
is(o.level, 30)
|
||||
is(o.msg, 'test test2 test3')
|
||||
ok(o.time)
|
||||
}
|
||||
}
|
||||
})
|
||||
instance.info('test %s %s', 'test2', 'test3')
|
||||
|
||||
end()
|
||||
})
|
||||
|
||||
test('opts.browser.write func string joining when asObject is true', ({ end, ok, is }) => {
|
||||
const instance = pino({
|
||||
browser: {
|
||||
asObject: true,
|
||||
write: function (o) {
|
||||
is(o.level, 30)
|
||||
is(o.msg, 'test test2 test3')
|
||||
ok(o.time)
|
||||
}
|
||||
}
|
||||
})
|
||||
instance.info('test %s %s', 'test2', 'test3')
|
||||
|
||||
end()
|
||||
})
|
||||
|
||||
test('opts.browser.write func string object joining', ({ end, ok, is }) => {
|
||||
const instance = pino({
|
||||
browser: {
|
||||
write: function (o) {
|
||||
is(o.level, 30)
|
||||
is(o.msg, 'test {"test":"test2"} {"test":"test3"}')
|
||||
ok(o.time)
|
||||
}
|
||||
}
|
||||
})
|
||||
instance.info('test %j %j', { test: 'test2' }, { test: 'test3' })
|
||||
|
||||
end()
|
||||
})
|
||||
|
||||
test('opts.browser.write func string object joining when asObject is true', ({ end, ok, is }) => {
|
||||
const instance = pino({
|
||||
browser: {
|
||||
asObject: true,
|
||||
write: function (o) {
|
||||
is(o.level, 30)
|
||||
is(o.msg, 'test {"test":"test2"} {"test":"test3"}')
|
||||
ok(o.time)
|
||||
}
|
||||
}
|
||||
})
|
||||
instance.info('test %j %j', { test: 'test2' }, { test: 'test3' })
|
||||
|
||||
end()
|
||||
})
|
||||
|
||||
test('opts.browser.write func string interpolation', ({ end, ok, is }) => {
|
||||
const instance = pino({
|
||||
browser: {
|
||||
write: function (o) {
|
||||
is(o.level, 30)
|
||||
is(o.msg, 'test2 test ({"test":"test3"})')
|
||||
ok(o.time)
|
||||
}
|
||||
}
|
||||
})
|
||||
instance.info('%s test (%j)', 'test2', { test: 'test3' })
|
||||
|
||||
end()
|
||||
})
|
||||
|
||||
test('opts.browser.write func number', ({ end, ok, is }) => {
|
||||
const instance = pino({
|
||||
browser: {
|
||||
write: function (o) {
|
||||
is(o.level, 30)
|
||||
is(o.msg, 1)
|
||||
ok(o.time)
|
||||
}
|
||||
}
|
||||
})
|
||||
instance.info(1)
|
||||
|
||||
end()
|
||||
})
|
||||
|
||||
test('opts.browser.write func log single object', ({ end, ok, is }) => {
|
||||
const instance = pino({
|
||||
browser: {
|
||||
write: function (o) {
|
||||
is(o.level, 30)
|
||||
is(o.test, 'test')
|
||||
ok(o.time)
|
||||
}
|
||||
}
|
||||
})
|
||||
instance.info({ test: 'test' })
|
||||
|
||||
end()
|
||||
})
|
||||
|
||||
test('opts.browser.write obj writes to methods corresponding to level', ({ end, ok, is }) => {
|
||||
const instance = pino({
|
||||
browser: {
|
||||
write: {
|
||||
error: function (o) {
|
||||
is(o.level, 50)
|
||||
is(o.test, 'test')
|
||||
ok(o.time)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
instance.error({ test: 'test' })
|
||||
|
||||
end()
|
||||
})
|
||||
|
||||
test('opts.browser.asObject/write supports child loggers', ({ end, ok, is }) => {
|
||||
const instance = pino({
|
||||
browser: {
|
||||
write (o) {
|
||||
is(o.level, 30)
|
||||
is(o.test, 'test')
|
||||
is(o.msg, 'msg-test')
|
||||
ok(o.time)
|
||||
}
|
||||
}
|
||||
})
|
||||
const child = instance.child({ test: 'test' })
|
||||
child.info('msg-test')
|
||||
|
||||
end()
|
||||
})
|
||||
|
||||
test('opts.browser.asObject/write supports child child loggers', ({ end, ok, is }) => {
|
||||
const instance = pino({
|
||||
browser: {
|
||||
write (o) {
|
||||
is(o.level, 30)
|
||||
is(o.test, 'test')
|
||||
is(o.foo, 'bar')
|
||||
is(o.msg, 'msg-test')
|
||||
ok(o.time)
|
||||
}
|
||||
}
|
||||
})
|
||||
const child = instance.child({ test: 'test' }).child({ foo: 'bar' })
|
||||
child.info('msg-test')
|
||||
|
||||
end()
|
||||
})
|
||||
|
||||
test('opts.browser.asObject/write supports child child child loggers', ({ end, ok, is }) => {
|
||||
const instance = pino({
|
||||
browser: {
|
||||
write (o) {
|
||||
is(o.level, 30)
|
||||
is(o.test, 'test')
|
||||
is(o.foo, 'bar')
|
||||
is(o.baz, 'bop')
|
||||
is(o.msg, 'msg-test')
|
||||
ok(o.time)
|
||||
}
|
||||
}
|
||||
})
|
||||
const child = instance.child({ test: 'test' }).child({ foo: 'bar' }).child({ baz: 'bop' })
|
||||
child.info('msg-test')
|
||||
|
||||
end()
|
||||
})
|
||||
|
||||
test('opts.browser.asObject defensively mitigates naughty numbers', ({ end, pass }) => {
|
||||
const instance = pino({
|
||||
browser: { asObject: true, write: () => {} }
|
||||
})
|
||||
const child = instance.child({ test: 'test' })
|
||||
child._childLevel = -10
|
||||
child.info('test')
|
||||
pass() // if we reached here, there was no infinite loop, so, .. pass.
|
||||
|
||||
end()
|
||||
})
|
||||
|
||||
test('opts.browser.write obj falls back to console where a method is not supplied', ({ end, ok, is }) => {
|
||||
const info = console.info
|
||||
console.info = (o) => {
|
||||
is(o.level, 30)
|
||||
is(o.msg, 'test')
|
||||
ok(o.time)
|
||||
console.info = info
|
||||
}
|
||||
const instance = require('../browser')({
|
||||
browser: {
|
||||
write: {
|
||||
error (o) {
|
||||
is(o.level, 50)
|
||||
is(o.test, 'test')
|
||||
ok(o.time)
|
||||
}
|
||||
}
|
||||
}
|
||||
})
|
||||
instance.error({ test: 'test' })
|
||||
instance.info('test')
|
||||
|
||||
end()
|
||||
})
|
||||
|
||||
function levelTest (name) {
|
||||
test(name + ' logs', ({ end, is }) => {
|
||||
const msg = 'hello world'
|
||||
sink(name, (args) => {
|
||||
is(args[0], msg)
|
||||
end()
|
||||
})
|
||||
pino({ level: name })[name](msg)
|
||||
})
|
||||
|
||||
test('passing objects at level ' + name, ({ end, is }) => {
|
||||
const msg = { hello: 'world' }
|
||||
sink(name, (args) => {
|
||||
is(args[0], msg)
|
||||
end()
|
||||
})
|
||||
pino({ level: name })[name](msg)
|
||||
})
|
||||
|
||||
test('passing an object and a string at level ' + name, ({ end, is }) => {
|
||||
const a = { hello: 'world' }
|
||||
const b = 'a string'
|
||||
sink(name, (args) => {
|
||||
is(args[0], a)
|
||||
is(args[1], b)
|
||||
end()
|
||||
})
|
||||
pino({ level: name })[name](a, b)
|
||||
})
|
||||
|
||||
test('formatting logs as ' + name, ({ end, is }) => {
|
||||
sink(name, (args) => {
|
||||
is(args[0], 'hello %d')
|
||||
is(args[1], 42)
|
||||
end()
|
||||
})
|
||||
pino({ level: name })[name]('hello %d', 42)
|
||||
})
|
||||
|
||||
test('passing error at level ' + name, ({ end, is }) => {
|
||||
const err = new Error('myerror')
|
||||
sink(name, (args) => {
|
||||
is(args[0], err)
|
||||
end()
|
||||
})
|
||||
pino({ level: name })[name](err)
|
||||
})
|
||||
|
||||
test('passing error with a serializer at level ' + name, ({ end, is }) => {
|
||||
// in browser - should have no effect (should not crash)
|
||||
const err = new Error('myerror')
|
||||
sink(name, (args) => {
|
||||
is(args[0].err, err)
|
||||
end()
|
||||
})
|
||||
const instance = pino({
|
||||
level: name,
|
||||
serializers: {
|
||||
err: pino.stdSerializers.err
|
||||
}
|
||||
})
|
||||
instance[name]({ err })
|
||||
})
|
||||
|
||||
test('child logger for level ' + name, ({ end, is }) => {
|
||||
const msg = 'hello world'
|
||||
const parent = { hello: 'world' }
|
||||
sink(name, (args) => {
|
||||
is(args[0], parent)
|
||||
is(args[1], msg)
|
||||
end()
|
||||
})
|
||||
const instance = pino({ level: name })
|
||||
const child = instance.child(parent)
|
||||
child[name](msg)
|
||||
})
|
||||
|
||||
test('child-child logger for level ' + name, ({ end, is }) => {
|
||||
const msg = 'hello world'
|
||||
const grandParent = { hello: 'world' }
|
||||
const parent = { hello: 'you' }
|
||||
sink(name, (args) => {
|
||||
is(args[0], grandParent)
|
||||
is(args[1], parent)
|
||||
is(args[2], msg)
|
||||
end()
|
||||
})
|
||||
const instance = pino({ level: name })
|
||||
const child = instance.child(grandParent).child(parent)
|
||||
child[name](msg)
|
||||
})
|
||||
}
|
||||
|
||||
function consoleMethodTest (level, method) {
|
||||
if (!method) method = level
|
||||
test('pino().' + level + ' uses console.' + method, ({ end, is }) => {
|
||||
sink(method, (args) => {
|
||||
is(args[0], 'test')
|
||||
end()
|
||||
})
|
||||
const instance = require('../browser')({ level })
|
||||
instance[level]('test')
|
||||
})
|
||||
}
|
||||
|
||||
function absentConsoleMethodTest (method, fallback) {
|
||||
test('in absence of console.' + method + ', console.' + fallback + ' is used', ({ end, is }) => {
|
||||
const fn = console[method]
|
||||
console[method] = undefined
|
||||
sink(fallback, function (args) {
|
||||
is(args[0], 'test')
|
||||
end()
|
||||
console[method] = fn
|
||||
})
|
||||
const instance = require('../browser')({ level: method })
|
||||
instance[method]('test')
|
||||
})
|
||||
}
|
||||
|
||||
function isFunc (fn) { return typeof fn === 'function' }
|
||||
function fnName (fn) {
|
||||
const rx = /^\s*function\s*([^(]*)/i
|
||||
const match = rx.exec(fn)
|
||||
return match && match[1]
|
||||
}
|
||||
function sink (method, fn) {
|
||||
if (method === 'fatal') method = 'error'
|
||||
const orig = console[method]
|
||||
console[method] = function () {
|
||||
console[method] = orig
|
||||
fn(Array.prototype.slice.call(arguments))
|
||||
}
|
||||
}
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
'use strict'
|
||||
|
||||
const test = require('node:test')
|
||||
const assert = require('node:assert')
|
||||
const { PassThrough } = require('node:stream')
|
||||
|
||||
const { sink, once } = require('./helper')
|
||||
const pino = require('../')
|
||||
|
||||
test('Proxy and stream objects', async () => {
|
||||
const s = new PassThrough()
|
||||
s.resume()
|
||||
s.write('', () => {})
|
||||
const obj = { s, p: new Proxy({}, { get () { throw new Error('kaboom') } }) }
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
instance.info({ obj })
|
||||
|
||||
const result = await once(stream, 'data')
|
||||
|
||||
assert.equal(result.obj, '[unable to serialize, circular reference is too complex to analyze]')
|
||||
})
|
||||
|
||||
test('Proxy and stream objects', async () => {
|
||||
const s = new PassThrough()
|
||||
s.resume()
|
||||
s.write('', () => {})
|
||||
const obj = { s, p: new Proxy({}, { get () { throw new Error('kaboom') } }) }
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
instance.info(obj)
|
||||
|
||||
const result = await once(stream, 'data')
|
||||
|
||||
assert.equal(result.p, '[unable to serialize, circular reference is too complex to analyze]')
|
||||
})
|
||||
+34
@@ -0,0 +1,34 @@
|
||||
'use strict'
|
||||
|
||||
const test = require('node:test')
|
||||
const assert = require('node:assert')
|
||||
|
||||
const writer = require('flush-write-stream')
|
||||
const pino = require('../')
|
||||
|
||||
function capture () {
|
||||
const ws = writer((chunk, enc, cb) => {
|
||||
ws.data += chunk.toString()
|
||||
cb()
|
||||
})
|
||||
ws.data = ''
|
||||
return ws
|
||||
}
|
||||
|
||||
test('pino uses LF by default', async () => {
|
||||
const stream = capture()
|
||||
const logger = pino(stream)
|
||||
logger.info('foo')
|
||||
logger.error('bar')
|
||||
assert.ok(/foo[^\r\n]+\n[^\r\n]+bar[^\r\n]+\n/.test(stream.data))
|
||||
})
|
||||
|
||||
test('pino can log CRLF', async () => {
|
||||
const stream = capture()
|
||||
const logger = pino({
|
||||
crlf: true
|
||||
}, stream)
|
||||
logger.info('foo')
|
||||
logger.error('bar')
|
||||
assert.ok(/foo[^\n]+\r\n[^\n]+bar[^\n]+\r\n/.test(stream.data))
|
||||
})
|
||||
+267
@@ -0,0 +1,267 @@
|
||||
'use strict'
|
||||
|
||||
/* eslint no-prototype-builtins: 0 */
|
||||
|
||||
const test = require('node:test')
|
||||
const assert = require('node:assert')
|
||||
|
||||
const { sink, once } = require('./helper')
|
||||
const pino = require('../')
|
||||
|
||||
// Silence all warnings for this test
|
||||
process.removeAllListeners('warning')
|
||||
process.on('warning', () => {})
|
||||
|
||||
test('adds additional levels', async () => {
|
||||
const stream = sink()
|
||||
const logger = pino({
|
||||
customLevels: {
|
||||
foo: 35,
|
||||
bar: 45
|
||||
}
|
||||
}, stream)
|
||||
|
||||
logger.foo('test')
|
||||
const { level } = await once(stream, 'data')
|
||||
assert.equal(level, 35)
|
||||
})
|
||||
|
||||
test('custom levels does not override default levels', async () => {
|
||||
const stream = sink()
|
||||
const logger = pino({
|
||||
customLevels: {
|
||||
foo: 35
|
||||
}
|
||||
}, stream)
|
||||
|
||||
logger.info('test')
|
||||
const { level } = await once(stream, 'data')
|
||||
assert.equal(level, 30)
|
||||
})
|
||||
|
||||
test('default levels can be redefined using custom levels', async () => {
|
||||
const stream = sink()
|
||||
const logger = pino({
|
||||
customLevels: {
|
||||
info: 35,
|
||||
debug: 45
|
||||
},
|
||||
useOnlyCustomLevels: true
|
||||
}, stream)
|
||||
|
||||
assert.equal(logger.hasOwnProperty('info'), true)
|
||||
|
||||
logger.info('test')
|
||||
const { level } = await once(stream, 'data')
|
||||
assert.equal(level, 35)
|
||||
})
|
||||
|
||||
test('custom levels overrides default level label if use useOnlyCustomLevels', async () => {
|
||||
const stream = sink()
|
||||
const logger = pino({
|
||||
customLevels: {
|
||||
foo: 35
|
||||
},
|
||||
useOnlyCustomLevels: true,
|
||||
level: 'foo'
|
||||
}, stream)
|
||||
|
||||
assert.equal(logger.hasOwnProperty('info'), false)
|
||||
})
|
||||
|
||||
test('custom levels overrides default level value if use useOnlyCustomLevels', async () => {
|
||||
const stream = sink()
|
||||
const logger = pino({
|
||||
customLevels: {
|
||||
foo: 35
|
||||
},
|
||||
useOnlyCustomLevels: true,
|
||||
level: 35
|
||||
}, stream)
|
||||
|
||||
assert.equal(logger.hasOwnProperty('info'), false)
|
||||
})
|
||||
|
||||
test('custom levels are inherited by children', async () => {
|
||||
const stream = sink()
|
||||
const logger = pino({
|
||||
customLevels: {
|
||||
foo: 35
|
||||
}
|
||||
}, stream)
|
||||
|
||||
logger.child({ childMsg: 'ok' }).foo('test')
|
||||
const { msg, childMsg, level } = await once(stream, 'data')
|
||||
assert.equal(level, 35)
|
||||
assert.equal(childMsg, 'ok')
|
||||
assert.equal(msg, 'test')
|
||||
})
|
||||
|
||||
test('custom levels can be specified on child bindings', async () => {
|
||||
const stream = sink()
|
||||
const logger = pino(stream).child({
|
||||
childMsg: 'ok'
|
||||
}, {
|
||||
customLevels: {
|
||||
foo: 35
|
||||
}
|
||||
})
|
||||
|
||||
logger.foo('test')
|
||||
const { msg, childMsg, level } = await once(stream, 'data')
|
||||
assert.equal(level, 35)
|
||||
assert.equal(childMsg, 'ok')
|
||||
assert.equal(msg, 'test')
|
||||
})
|
||||
|
||||
test('customLevels property child bindings does not get logged', async () => {
|
||||
const stream = sink()
|
||||
const logger = pino(stream).child({
|
||||
childMsg: 'ok'
|
||||
}, {
|
||||
customLevels: {
|
||||
foo: 35
|
||||
}
|
||||
})
|
||||
|
||||
logger.foo('test')
|
||||
const { customLevels } = await once(stream, 'data')
|
||||
assert.equal(customLevels, undefined)
|
||||
})
|
||||
|
||||
test('throws when specifying pre-existing parent labels via child bindings', async () => {
|
||||
const stream = sink()
|
||||
assert.throws(
|
||||
() => pino({
|
||||
customLevels: {
|
||||
foo: 35
|
||||
}
|
||||
}, stream).child({}, {
|
||||
customLevels: {
|
||||
foo: 45
|
||||
}
|
||||
}),
|
||||
/levels cannot be overridden/
|
||||
)
|
||||
})
|
||||
|
||||
test('throws when specifying pre-existing parent values via child bindings', async () => {
|
||||
const stream = sink()
|
||||
assert.throws(
|
||||
() => pino({
|
||||
customLevels: {
|
||||
foo: 35
|
||||
}
|
||||
}, stream).child({}, {
|
||||
customLevels: {
|
||||
bar: 35
|
||||
}
|
||||
}),
|
||||
/pre-existing level values cannot be used for new levels/
|
||||
)
|
||||
})
|
||||
|
||||
test('throws when specifying core values via child bindings', async () => {
|
||||
const stream = sink()
|
||||
assert.throws(
|
||||
() => pino(stream).child({}, {
|
||||
customLevels: {
|
||||
foo: 30
|
||||
}
|
||||
}),
|
||||
/pre-existing level values cannot be used for new levels/
|
||||
)
|
||||
})
|
||||
|
||||
test('throws when useOnlyCustomLevels is set true without customLevels', async () => {
|
||||
const stream = sink()
|
||||
assert.throws(
|
||||
() => pino({
|
||||
useOnlyCustomLevels: true
|
||||
}, stream),
|
||||
/customLevels is required if useOnlyCustomLevels is set true/
|
||||
)
|
||||
})
|
||||
|
||||
test('custom level on one instance does not affect other instances', async () => {
|
||||
pino({
|
||||
customLevels: {
|
||||
foo: 37
|
||||
}
|
||||
})
|
||||
assert.equal(typeof pino().foo, 'undefined')
|
||||
})
|
||||
|
||||
test('setting level below or at custom level will successfully log', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ customLevels: { foo: 35 } }, stream)
|
||||
instance.level = 'foo'
|
||||
instance.info('nope')
|
||||
instance.foo('bar')
|
||||
const { msg } = await once(stream, 'data')
|
||||
assert.equal(msg, 'bar')
|
||||
})
|
||||
|
||||
test('custom level below level threshold will not log', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ customLevels: { foo: 15 } }, stream)
|
||||
instance.level = 'info'
|
||||
instance.info('bar')
|
||||
instance.foo('nope')
|
||||
const { msg } = await once(stream, 'data')
|
||||
assert.equal(msg, 'bar')
|
||||
})
|
||||
|
||||
test('does not share custom level state across siblings', async () => {
|
||||
const stream = sink()
|
||||
const logger = pino(stream)
|
||||
logger.child({}, {
|
||||
customLevels: { foo: 35 }
|
||||
})
|
||||
assert.doesNotThrow(() => {
|
||||
logger.child({}, {
|
||||
customLevels: { foo: 35 }
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
test('custom level does not affect the levels serializer', async () => {
|
||||
const stream = sink()
|
||||
const logger = pino({
|
||||
customLevels: {
|
||||
foo: 35,
|
||||
bar: 45
|
||||
},
|
||||
formatters: {
|
||||
level (label, number) {
|
||||
return { priority: number }
|
||||
}
|
||||
}
|
||||
}, stream)
|
||||
|
||||
logger.foo('test')
|
||||
const { priority } = await once(stream, 'data')
|
||||
assert.equal(priority, 35)
|
||||
})
|
||||
|
||||
test('When useOnlyCustomLevels is set to true, the level formatter should only get custom levels', async () => {
|
||||
const stream = sink()
|
||||
const logger = pino({
|
||||
customLevels: {
|
||||
answer: 42
|
||||
},
|
||||
useOnlyCustomLevels: true,
|
||||
level: 42,
|
||||
formatters: {
|
||||
level (label, number) {
|
||||
assert.equal(label, 'answer')
|
||||
assert.equal(number, 42)
|
||||
return { level: number }
|
||||
}
|
||||
}
|
||||
}, stream)
|
||||
|
||||
logger.answer('test')
|
||||
const { level } = await once(stream, 'data')
|
||||
assert.equal(level, 42)
|
||||
})
|
||||
+107
@@ -0,0 +1,107 @@
|
||||
'use strict'
|
||||
|
||||
const test = require('node:test')
|
||||
const os = require('node:os')
|
||||
const diagChan = require('node:diagnostics_channel')
|
||||
const { AsyncLocalStorage } = require('node:async_hooks')
|
||||
const { Writable } = require('node:stream')
|
||||
const tspl = require('@matteo.collina/tspl')
|
||||
const pino = require('../pino')
|
||||
|
||||
const hostname = os.hostname()
|
||||
const { pid } = process
|
||||
const AS_JSON_START = 'tracing:pino_asJson:start'
|
||||
const AS_JSON_END = 'tracing:pino_asJson:end'
|
||||
|
||||
// Skip tests if diagnostics_channel.tracingChannel is not available (Node < 18.19)
|
||||
const skip = typeof diagChan.tracingChannel !== 'function'
|
||||
|
||||
test.beforeEach(ctx => {
|
||||
ctx.pino = {
|
||||
ts: 1757512800000, // 2025-09-10T10:00:00.000-05:00
|
||||
now: Date.now
|
||||
}
|
||||
|
||||
Date.now = () => ctx.pino.ts
|
||||
|
||||
ctx.pino.dest = new Writable({
|
||||
objectMode: true,
|
||||
write (data, enc, cb) {
|
||||
cb()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
test.afterEach(ctx => {
|
||||
Date.now = ctx.pino.now
|
||||
})
|
||||
|
||||
test('asJson emits events', { skip }, async (t) => {
|
||||
const plan = tspl(t, { plan: 8 })
|
||||
const { dest } = t.pino
|
||||
const logger = pino({}, dest)
|
||||
const expectedArguments = [
|
||||
{},
|
||||
'testing',
|
||||
30,
|
||||
`,"time":${t.pino.ts}`
|
||||
]
|
||||
|
||||
let startEvent
|
||||
diagChan.subscribe(AS_JSON_START, startHandler)
|
||||
diagChan.subscribe(AS_JSON_END, endHandler)
|
||||
|
||||
logger.info('testing')
|
||||
await plan
|
||||
|
||||
diagChan.unsubscribe(AS_JSON_START, startHandler)
|
||||
diagChan.unsubscribe(AS_JSON_END, endHandler)
|
||||
|
||||
function startHandler (event) {
|
||||
startEvent = event
|
||||
plan.equal(Object.prototype.toString.call(event.instance), '[object Pino]')
|
||||
plan.equal(event.instance === logger, true)
|
||||
plan.deepStrictEqual(Array.from(event.arguments ?? []), expectedArguments)
|
||||
}
|
||||
|
||||
function endHandler (event) {
|
||||
plan.equal(Object.prototype.toString.call(event.instance), '[object Pino]')
|
||||
plan.equal(event.instance === logger, true)
|
||||
plan.deepStrictEqual(Array.from(event.arguments ?? []), expectedArguments)
|
||||
plan.equal(
|
||||
event.result,
|
||||
`{"level":30,"time":${t.pino.ts},"pid":${pid},"hostname":"${hostname}","msg":"testing"}\n`
|
||||
)
|
||||
|
||||
plan.equal(event.arguments === startEvent.arguments, true, 'same event object is supplied to both events')
|
||||
}
|
||||
})
|
||||
|
||||
test('asJson context is not lost', { skip }, async (t) => {
|
||||
const plan = tspl(t, { plan: 2 })
|
||||
const { dest } = t.pino
|
||||
const logger = pino({}, dest)
|
||||
const asyncLocalStorage = new AsyncLocalStorage()
|
||||
const localStore = { foo: 'bar' }
|
||||
|
||||
diagChan.subscribe(AS_JSON_START, startHandler)
|
||||
diagChan.subscribe(AS_JSON_END, endHandler)
|
||||
|
||||
asyncLocalStorage.run(localStore, () => {
|
||||
logger.info('testing')
|
||||
})
|
||||
await plan
|
||||
|
||||
diagChan.unsubscribe(AS_JSON_START, startHandler)
|
||||
diagChan.unsubscribe(AS_JSON_END, endHandler)
|
||||
|
||||
function startHandler () {
|
||||
const store = asyncLocalStorage.getStore()
|
||||
plan.equal(store === localStore, true)
|
||||
}
|
||||
|
||||
function endHandler () {
|
||||
const store = asyncLocalStorage.getStore()
|
||||
plan.equal(store === localStore, true)
|
||||
}
|
||||
})
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
'use strict'
|
||||
|
||||
const test = require('node:test')
|
||||
const assert = require('node:assert')
|
||||
|
||||
const { sink, once } = require('./helper')
|
||||
const stdSerializers = require('pino-std-serializers')
|
||||
const pino = require('../')
|
||||
|
||||
test('set the errorKey with error serializer', async () => {
|
||||
const stream = sink()
|
||||
const errorKey = 'error'
|
||||
const instance = pino({
|
||||
errorKey,
|
||||
serializers: { [errorKey]: stdSerializers.err }
|
||||
}, stream)
|
||||
instance.error(new ReferenceError('test'))
|
||||
const o = await once(stream, 'data')
|
||||
assert.equal(typeof o[errorKey], 'object')
|
||||
assert.equal(o[errorKey].type, 'ReferenceError')
|
||||
assert.equal(o[errorKey].message, 'test')
|
||||
assert.equal(typeof o[errorKey].stack, 'string')
|
||||
})
|
||||
|
||||
test('set the errorKey without error serializer', async () => {
|
||||
const stream = sink()
|
||||
const errorKey = 'error'
|
||||
const instance = pino({
|
||||
errorKey
|
||||
}, stream)
|
||||
instance.error(new ReferenceError('test'))
|
||||
const o = await once(stream, 'data')
|
||||
assert.equal(typeof o[errorKey], 'object')
|
||||
assert.equal(o[errorKey].type, 'ReferenceError')
|
||||
assert.equal(o[errorKey].message, 'test')
|
||||
assert.equal(typeof o[errorKey].stack, 'string')
|
||||
})
|
||||
+403
@@ -0,0 +1,403 @@
|
||||
'use strict'
|
||||
|
||||
/* eslint no-prototype-builtins: 0 */
|
||||
|
||||
const test = require('node:test')
|
||||
const assert = require('node:assert')
|
||||
const os = require('node:os')
|
||||
const tspl = require('@matteo.collina/tspl')
|
||||
|
||||
const { sink, once } = require('./helper')
|
||||
const pino = require('../')
|
||||
|
||||
const { pid } = process
|
||||
const hostname = os.hostname()
|
||||
const level = 50
|
||||
const name = 'error'
|
||||
|
||||
test('err is serialized with additional properties set on the Error object', async () => {
|
||||
const stream = sink()
|
||||
const err = Object.assign(new Error('myerror'), { foo: 'bar' })
|
||||
const instance = pino(stream)
|
||||
instance.level = name
|
||||
instance[name](err)
|
||||
const result = await once(stream, 'data')
|
||||
assert.ok(new Date(result.time) <= new Date(), 'time is greater than Date.now()')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level,
|
||||
err: {
|
||||
type: 'Error',
|
||||
message: err.message,
|
||||
stack: err.stack,
|
||||
foo: err.foo
|
||||
},
|
||||
msg: err.message
|
||||
})
|
||||
})
|
||||
|
||||
test('type should be detected based on constructor', async () => {
|
||||
class Bar extends Error {}
|
||||
const stream = sink()
|
||||
const err = new Bar('myerror')
|
||||
const instance = pino(stream)
|
||||
instance.level = name
|
||||
instance[name](err)
|
||||
const result = await once(stream, 'data')
|
||||
assert.ok(new Date(result.time) <= new Date(), 'time is greater than Date.now()')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level,
|
||||
err: {
|
||||
type: 'Bar',
|
||||
message: err.message,
|
||||
stack: err.stack
|
||||
},
|
||||
msg: err.message
|
||||
})
|
||||
})
|
||||
|
||||
test('type, message and stack should be first level properties', async () => {
|
||||
const stream = sink()
|
||||
const err = Object.assign(new Error('foo'), { foo: 'bar' })
|
||||
const instance = pino(stream)
|
||||
instance.level = name
|
||||
instance[name](err)
|
||||
|
||||
const result = await once(stream, 'data')
|
||||
assert.ok(new Date(result.time) <= new Date(), 'time is greater than Date.now()')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level,
|
||||
err: {
|
||||
type: 'Error',
|
||||
message: err.message,
|
||||
stack: err.stack,
|
||||
foo: err.foo
|
||||
},
|
||||
msg: err.message
|
||||
})
|
||||
})
|
||||
|
||||
test('err serializer', async () => {
|
||||
const stream = sink()
|
||||
const err = Object.assign(new Error('myerror'), { foo: 'bar' })
|
||||
const instance = pino({
|
||||
serializers: {
|
||||
err: pino.stdSerializers.err
|
||||
}
|
||||
}, stream)
|
||||
|
||||
instance.level = name
|
||||
instance[name]({ err })
|
||||
const result = await once(stream, 'data')
|
||||
assert.ok(new Date(result.time) <= new Date(), 'time is greater than Date.now()')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level,
|
||||
err: {
|
||||
type: 'Error',
|
||||
message: err.message,
|
||||
stack: err.stack,
|
||||
foo: err.foo
|
||||
},
|
||||
msg: err.message
|
||||
})
|
||||
})
|
||||
|
||||
test('an error with statusCode property is not confused for a http response', async () => {
|
||||
const stream = sink()
|
||||
const err = Object.assign(new Error('StatusCodeErr'), { statusCode: 500 })
|
||||
const instance = pino(stream)
|
||||
|
||||
instance.level = name
|
||||
instance[name](err)
|
||||
const result = await once(stream, 'data')
|
||||
|
||||
assert.ok(new Date(result.time) <= new Date(), 'time is greater than Date.now()')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level,
|
||||
err: {
|
||||
type: 'Error',
|
||||
message: err.message,
|
||||
stack: err.stack,
|
||||
statusCode: err.statusCode
|
||||
},
|
||||
msg: err.message
|
||||
})
|
||||
})
|
||||
|
||||
test('stack is omitted if it is not set on err', async (t) => {
|
||||
const plan = tspl(t, { plan: 2 })
|
||||
const err = new Error('myerror')
|
||||
delete err.stack
|
||||
const instance = pino(sink(function (chunk, enc, cb) {
|
||||
plan.ok(new Date(chunk.time) <= new Date(), 'time is greater than Date.now()')
|
||||
delete chunk.time
|
||||
plan.equal(chunk.hasOwnProperty('stack'), false)
|
||||
cb()
|
||||
}))
|
||||
|
||||
instance.level = name
|
||||
instance[name](err)
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('correctly ignores toString on errors', async () => {
|
||||
const err = new Error('myerror')
|
||||
err.toString = () => undefined
|
||||
const stream = sink()
|
||||
const instance = pino({
|
||||
test: 'this'
|
||||
}, stream)
|
||||
instance.fatal(err)
|
||||
const result = await once(stream, 'data')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 60,
|
||||
err: {
|
||||
type: 'Error',
|
||||
message: err.message,
|
||||
stack: err.stack
|
||||
},
|
||||
msg: err.message
|
||||
})
|
||||
})
|
||||
|
||||
test('assign mixin()', async () => {
|
||||
const err = new Error('myerror')
|
||||
const stream = sink()
|
||||
const instance = pino({
|
||||
mixin () {
|
||||
return { hello: 'world' }
|
||||
}
|
||||
}, stream)
|
||||
instance.fatal(err)
|
||||
const result = await once(stream, 'data')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 60,
|
||||
hello: 'world',
|
||||
err: {
|
||||
type: 'Error',
|
||||
message: err.message,
|
||||
stack: err.stack
|
||||
},
|
||||
msg: err.message
|
||||
})
|
||||
})
|
||||
|
||||
test('no err serializer', async () => {
|
||||
const err = new Error('myerror')
|
||||
const stream = sink()
|
||||
const instance = pino({
|
||||
serializers: {}
|
||||
}, stream)
|
||||
instance.fatal(err)
|
||||
const result = await once(stream, 'data')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 60,
|
||||
err: {
|
||||
type: 'Error',
|
||||
message: err.message,
|
||||
stack: err.stack
|
||||
},
|
||||
msg: err.message
|
||||
})
|
||||
})
|
||||
|
||||
test('empty serializer', async () => {
|
||||
const err = new Error('myerror')
|
||||
const stream = sink()
|
||||
const instance = pino({
|
||||
serializers: {
|
||||
err () {}
|
||||
}
|
||||
}, stream)
|
||||
instance.fatal(err)
|
||||
const result = await once(stream, 'data')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 60,
|
||||
msg: err.message
|
||||
})
|
||||
})
|
||||
|
||||
test('assign mixin()', async () => {
|
||||
const err = new Error('myerror')
|
||||
const stream = sink()
|
||||
const instance = pino({
|
||||
mixin () {
|
||||
return { hello: 'world' }
|
||||
}
|
||||
}, stream)
|
||||
instance.fatal(err)
|
||||
const result = await once(stream, 'data')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 60,
|
||||
hello: 'world',
|
||||
err: {
|
||||
type: 'Error',
|
||||
message: err.message,
|
||||
stack: err.stack
|
||||
},
|
||||
msg: err.message
|
||||
})
|
||||
})
|
||||
|
||||
test('no err serializer', async () => {
|
||||
const err = new Error('myerror')
|
||||
const stream = sink()
|
||||
const instance = pino({
|
||||
serializers: {}
|
||||
}, stream)
|
||||
instance.fatal(err)
|
||||
const result = await once(stream, 'data')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 60,
|
||||
err: {
|
||||
type: 'Error',
|
||||
message: err.message,
|
||||
stack: err.stack
|
||||
},
|
||||
msg: err.message
|
||||
})
|
||||
})
|
||||
|
||||
test('empty serializer', async () => {
|
||||
const err = new Error('myerror')
|
||||
const stream = sink()
|
||||
const instance = pino({
|
||||
serializers: {
|
||||
err () {}
|
||||
}
|
||||
}, stream)
|
||||
instance.fatal(err)
|
||||
const result = await once(stream, 'data')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 60,
|
||||
msg: err.message
|
||||
})
|
||||
})
|
||||
|
||||
test('correctly adds error information when nestedKey is used', async () => {
|
||||
const err = new Error('myerror')
|
||||
err.toString = () => undefined
|
||||
const stream = sink()
|
||||
const instance = pino({
|
||||
test: 'this',
|
||||
nestedKey: 'obj'
|
||||
}, stream)
|
||||
instance.fatal(err)
|
||||
const result = await once(stream, 'data')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 60,
|
||||
obj: {
|
||||
err: {
|
||||
type: 'Error',
|
||||
stack: err.stack,
|
||||
message: err.message
|
||||
}
|
||||
},
|
||||
msg: err.message
|
||||
})
|
||||
})
|
||||
|
||||
test('correctly adds msg on error when nestedKey is used', async () => {
|
||||
const err = new Error('myerror')
|
||||
err.toString = () => undefined
|
||||
const stream = sink()
|
||||
const instance = pino({
|
||||
test: 'this',
|
||||
nestedKey: 'obj'
|
||||
}, stream)
|
||||
instance.fatal(err, 'msg message')
|
||||
const result = await once(stream, 'data')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 60,
|
||||
obj: {
|
||||
err: {
|
||||
type: 'Error',
|
||||
stack: err.stack,
|
||||
message: err.message
|
||||
}
|
||||
},
|
||||
msg: 'msg message'
|
||||
})
|
||||
})
|
||||
|
||||
test('msg should take precedence over error message on mergingObject', async () => {
|
||||
const err = new Error('myerror')
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
instance.error({ msg: 'my message', err })
|
||||
const result = await once(stream, 'data')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 50,
|
||||
err: {
|
||||
type: 'Error',
|
||||
stack: err.stack,
|
||||
message: err.message
|
||||
},
|
||||
msg: 'my message'
|
||||
})
|
||||
})
|
||||
|
||||
test('considers messageKey when giving msg precedence over error', async () => {
|
||||
const err = new Error('myerror')
|
||||
const stream = sink()
|
||||
const instance = pino({ messageKey: 'message' }, stream)
|
||||
instance.error({ message: 'my message', err })
|
||||
const result = await once(stream, 'data')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 50,
|
||||
err: {
|
||||
type: 'Error',
|
||||
stack: err.stack,
|
||||
message: err.message
|
||||
},
|
||||
message: 'my message'
|
||||
})
|
||||
})
|
||||
+93
@@ -0,0 +1,93 @@
|
||||
'use strict'
|
||||
|
||||
const test = require('node:test')
|
||||
const assert = require('node:assert')
|
||||
const os = require('node:os')
|
||||
|
||||
const { sink, once } = require('./helper')
|
||||
const pino = require('../')
|
||||
|
||||
const { pid } = process
|
||||
const hostname = os.hostname()
|
||||
|
||||
function testEscape (ch, key) {
|
||||
test('correctly escape ' + ch, async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({
|
||||
name: 'hello'
|
||||
}, stream)
|
||||
instance.fatal('this contains ' + key)
|
||||
const result = await once(stream, 'data')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 60,
|
||||
name: 'hello',
|
||||
msg: 'this contains ' + key
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
testEscape('\\n', '\n')
|
||||
testEscape('\\/', '/')
|
||||
testEscape('\\\\', '\\')
|
||||
testEscape('\\r', '\r')
|
||||
testEscape('\\t', '\t')
|
||||
testEscape('\\b', '\b')
|
||||
|
||||
const toEscape = [
|
||||
'\u0000', // NUL Null character
|
||||
'\u0001', // SOH Start of Heading
|
||||
'\u0002', // STX Start of Text
|
||||
'\u0003', // ETX End-of-text character
|
||||
'\u0004', // EOT End-of-transmission character
|
||||
'\u0005', // ENQ Enquiry character
|
||||
'\u0006', // ACK Acknowledge character
|
||||
'\u0007', // BEL Bell character
|
||||
'\u0008', // BS Backspace
|
||||
'\u0009', // HT Horizontal tab
|
||||
'\u000A', // LF Line feed
|
||||
'\u000B', // VT Vertical tab
|
||||
'\u000C', // FF Form feed
|
||||
'\u000D', // CR Carriage return
|
||||
'\u000E', // SO Shift Out
|
||||
'\u000F', // SI Shift In
|
||||
'\u0010', // DLE Data Link Escape
|
||||
'\u0011', // DC1 Device Control 1
|
||||
'\u0012', // DC2 Device Control 2
|
||||
'\u0013', // DC3 Device Control 3
|
||||
'\u0014', // DC4 Device Control 4
|
||||
'\u0015', // NAK Negative-acknowledge character
|
||||
'\u0016', // SYN Synchronous Idle
|
||||
'\u0017', // ETB End of Transmission Block
|
||||
'\u0018', // CAN Cancel character
|
||||
'\u0019', // EM End of Medium
|
||||
'\u001A', // SUB Substitute character
|
||||
'\u001B', // ESC Escape character
|
||||
'\u001C', // FS File Separator
|
||||
'\u001D', // GS Group Separator
|
||||
'\u001E', // RS Record Separator
|
||||
'\u001F' // US Unit Separator
|
||||
]
|
||||
|
||||
toEscape.forEach((key) => {
|
||||
testEscape(JSON.stringify(key), key)
|
||||
})
|
||||
|
||||
test('correctly escape `hello \\u001F world \\n \\u0022`', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({
|
||||
name: 'hello'
|
||||
}, stream)
|
||||
instance.fatal('hello \u001F world \n \u0022')
|
||||
const result = await once(stream, 'data')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 60,
|
||||
name: 'hello',
|
||||
msg: 'hello \u001F world \n \u0022'
|
||||
})
|
||||
})
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert'
|
||||
|
||||
import pino from '../../pino.js'
|
||||
import helper from '../helper.js'
|
||||
|
||||
const { sink, check, once } = helper
|
||||
|
||||
test('esm support', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
instance.info('hello world')
|
||||
check(assert.equal, await once(stream, 'data'), 30, 'hello world')
|
||||
})
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
'use strict'
|
||||
|
||||
// Node v8 throw a `SyntaxError: Unexpected token import`
|
||||
// even if this branch is never touched in the code,
|
||||
// by using `eval` we can avoid this issue.
|
||||
// eslint-disable-next-line
|
||||
new Function('module', 'return import(module)')('./esm.mjs').catch((err) => {
|
||||
process.nextTick(() => {
|
||||
throw err
|
||||
})
|
||||
})
|
||||
|
||||
// Node v8 throw a `SyntaxError: Unexpected token import`
|
||||
// even if this branch is never touched in the code,
|
||||
// by using `eval` we can avoid this issue.
|
||||
// eslint-disable-next-line
|
||||
new Function('module', 'return import(module)')('./named-exports.mjs').catch((err) => {
|
||||
process.nextTick(() => {
|
||||
throw err
|
||||
})
|
||||
})
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
import test from 'node:test'
|
||||
import assert from 'node:assert'
|
||||
import { hostname } from 'node:os'
|
||||
import { readFileSync } from 'node:fs'
|
||||
|
||||
import { sink, check, once, watchFileCreated, file } from '../helper.js'
|
||||
import { pino, destination } from '../../pino.js'
|
||||
|
||||
test('named exports support', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
instance.info('hello world')
|
||||
check(assert.equal, await once(stream, 'data'), 30, 'hello world')
|
||||
})
|
||||
|
||||
test('destination', async () => {
|
||||
const tmp = file()
|
||||
const instance = pino(destination(tmp))
|
||||
instance.info('hello')
|
||||
await watchFileCreated(tmp)
|
||||
const result = JSON.parse(readFileSync(tmp).toString())
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid: process.pid,
|
||||
hostname,
|
||||
level: 30,
|
||||
msg: 'hello'
|
||||
})
|
||||
})
|
||||
+79
@@ -0,0 +1,79 @@
|
||||
'use strict'
|
||||
|
||||
const test = require('node:test')
|
||||
const assert = require('node:assert')
|
||||
const { join } = require('node:path')
|
||||
|
||||
const execa = require('execa')
|
||||
const writer = require('flush-write-stream')
|
||||
const { once } = require('./helper')
|
||||
|
||||
// https://github.com/pinojs/pino/issues/542
|
||||
test('pino.destination log everything when calling process.exit(0)', async () => {
|
||||
let actual = ''
|
||||
const child = execa(process.argv[0], [join(__dirname, 'fixtures', 'destination-exit.js')])
|
||||
|
||||
child.stdout.pipe(writer((s, enc, cb) => {
|
||||
actual += s
|
||||
cb()
|
||||
}))
|
||||
|
||||
await once(child, 'close')
|
||||
|
||||
assert.equal(actual.match(/hello/) != null, true)
|
||||
assert.equal(actual.match(/world/) != null, true)
|
||||
})
|
||||
|
||||
test('pino with no args log everything when calling process.exit(0)', async () => {
|
||||
let actual = ''
|
||||
const child = execa(process.argv[0], [join(__dirname, 'fixtures', 'default-exit.js')])
|
||||
|
||||
child.stdout.pipe(writer((s, enc, cb) => {
|
||||
actual += s
|
||||
cb()
|
||||
}))
|
||||
|
||||
await once(child, 'close')
|
||||
|
||||
assert.equal(actual.match(/hello/) != null, true)
|
||||
assert.equal(actual.match(/world/) != null, true)
|
||||
})
|
||||
|
||||
test('sync false logs everything when calling process.exit(0)', async () => {
|
||||
let actual = ''
|
||||
const child = execa(process.argv[0], [join(__dirname, 'fixtures', 'syncfalse-exit.js')])
|
||||
|
||||
child.stdout.pipe(writer((s, enc, cb) => {
|
||||
actual += s
|
||||
cb()
|
||||
}))
|
||||
|
||||
await once(child, 'close')
|
||||
|
||||
assert.equal(actual.match(/hello/) != null, true)
|
||||
assert.equal(actual.match(/world/) != null, true)
|
||||
})
|
||||
|
||||
test('sync false logs everything when calling flushSync', async () => {
|
||||
let actual = ''
|
||||
const child = execa(process.argv[0], [join(__dirname, 'fixtures', 'syncfalse-flush-exit.js')])
|
||||
|
||||
child.stdout.pipe(writer((s, enc, cb) => {
|
||||
actual += s
|
||||
cb()
|
||||
}))
|
||||
|
||||
await once(child, 'close')
|
||||
|
||||
assert.equal(actual.match(/hello/) != null, true)
|
||||
assert.equal(actual.match(/world/) != null, true)
|
||||
})
|
||||
|
||||
test('transports exits gracefully when logging in exit', async () => {
|
||||
const child = execa(process.argv[0], [join(__dirname, 'fixtures', 'transport-with-on-exit.js')])
|
||||
child.stdout.resume()
|
||||
|
||||
const code = await once(child, 'close')
|
||||
|
||||
assert.equal(code, 0)
|
||||
})
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
'use strict'
|
||||
|
||||
global.process = { __proto__: process, pid: 123456 }
|
||||
Date.now = function () { return 1459875739796 }
|
||||
require('node:os').hostname = function () { return 'abcdefghijklmnopqr' }
|
||||
|
||||
const pino = require('../../..')()
|
||||
|
||||
pino.info('hello world')
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
'use strict'
|
||||
|
||||
global.process = { __proto__: process, pid: 123456 }
|
||||
Date.now = function () { return 1459875739796 }
|
||||
require('node:os').hostname = function () { return 'abcdefghijklmnopqr' }
|
||||
|
||||
const pino = require('../../..')
|
||||
const logger = pino(pino.destination())
|
||||
|
||||
logger.info('hello world')
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
'use strict'
|
||||
|
||||
global.process = { __proto__: process, pid: 123456 }
|
||||
Date.now = function () { return 1459875739796 }
|
||||
require('node:os').hostname = function () { return 'abcdefghijklmnopqr' }
|
||||
|
||||
const pino = require('../../..')
|
||||
const logger = pino(pino.destination({ sync: false }))
|
||||
|
||||
for (var i = 0; i < 1000; i++) {
|
||||
logger.info('hello world')
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
const { Writable } = require('node:stream')
|
||||
|
||||
module.exports = (options) => {
|
||||
const myTransportStream = new Writable({
|
||||
autoDestroy: true,
|
||||
write (chunk, enc, cb) {
|
||||
// apply a transform and send to stdout
|
||||
console.log(chunk.toString().toUpperCase())
|
||||
cb()
|
||||
}
|
||||
})
|
||||
return myTransportStream
|
||||
}
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
const { Writable } = require('node:stream')
|
||||
|
||||
module.exports = () =>
|
||||
new Writable({
|
||||
autoDestroy: true,
|
||||
write (chunk, enc, cb) {
|
||||
setImmediate(() => {
|
||||
/* eslint-disable no-empty */
|
||||
for (let i = 0; i < 1e3; i++) {}
|
||||
process.exit(0)
|
||||
})
|
||||
}
|
||||
})
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
global.process = { __proto__: process, pid: 123456 }
|
||||
Date.now = function () { return 1459875739796 }
|
||||
require('node:os').hostname = function () { return 'abcdefghijklmnopqr' }
|
||||
const pino = require(require.resolve('./../../'))
|
||||
const logger = pino()
|
||||
logger.info('hello')
|
||||
logger.info('world')
|
||||
process.exit(0)
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
global.process = { __proto__: process, pid: 123456 }
|
||||
Date.now = function () { return 1459875739796 }
|
||||
require('node:os').hostname = function () { return 'abcdefghijklmnopqr' }
|
||||
const pino = require(require.resolve('./../../'))
|
||||
const logger = pino({}, pino.destination(1))
|
||||
logger.info('hello')
|
||||
logger.info('world')
|
||||
process.exit(0)
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
/* eslint-disable no-eval */
|
||||
|
||||
eval(`
|
||||
const pino = require('../../../')
|
||||
|
||||
const logger = pino(
|
||||
pino.transport({
|
||||
target: 'pino/file'
|
||||
})
|
||||
)
|
||||
|
||||
logger.info('done!')
|
||||
`)
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
const file1 = require("./file1.js")
|
||||
|
||||
file1()
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
const file12 = require("./file12.js")
|
||||
|
||||
file12()
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
const file2 = require("./file2.js")
|
||||
|
||||
module.exports = function () {
|
||||
file2()
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
const file11 = require("./file11.js")
|
||||
|
||||
module.exports = function () {
|
||||
file11()
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
const file12 = require("./file12.js")
|
||||
|
||||
module.exports = function () {
|
||||
file12()
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
const file13 = require("./file13.js")
|
||||
|
||||
module.exports = function () {
|
||||
file13()
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
const file14 = require("./file14.js")
|
||||
|
||||
module.exports = function () {
|
||||
file14()
|
||||
}
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
const pino = require("../../../../");
|
||||
|
||||
module.exports = function() {
|
||||
const logger = pino(
|
||||
pino.transport({
|
||||
target: 'pino/file'
|
||||
})
|
||||
)
|
||||
|
||||
logger.info('done!')
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
const file3 = require("./file3.js")
|
||||
|
||||
module.exports = function () {
|
||||
file3()
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
const file4 = require("./file4.js")
|
||||
|
||||
module.exports = function () {
|
||||
file4()
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
const file5 = require("./file5.js")
|
||||
|
||||
module.exports = function () {
|
||||
file5()
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
const file6 = require("./file6.js")
|
||||
|
||||
module.exports = function () {
|
||||
file6()
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
const file7 = require("./file7.js")
|
||||
|
||||
module.exports = function () {
|
||||
file7()
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
const file8 = require("./file8.js")
|
||||
|
||||
module.exports = function () {
|
||||
file8()
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
const file9 = require("./file9.js")
|
||||
|
||||
module.exports = function () {
|
||||
file9()
|
||||
}
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
const file10 = require("./file10.js")
|
||||
|
||||
module.exports = function () {
|
||||
file10()
|
||||
}
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
const { Writable } = require('node:stream')
|
||||
|
||||
module.exports = () => {
|
||||
return new Writable({
|
||||
autoDestroy: true,
|
||||
write (chunk, enc, cb) {
|
||||
cb()
|
||||
}
|
||||
})
|
||||
}
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
global.process = { __proto__: process, pid: 123456 }
|
||||
Date.now = function () { return 1459875739796 }
|
||||
require('node:os').hostname = function () { return 'abcdefghijklmnopqr' }
|
||||
const pino = require(require.resolve('./../../../'))
|
||||
const log = pino({ prettyPrint: true })
|
||||
const obj = Object.create(null)
|
||||
Object.assign(obj, { foo: 'bar' })
|
||||
log.info(obj, 'hello')
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
global.process = { __proto__: process, pid: 123456 }
|
||||
|
||||
const write = process.stdout.write.bind(process.stdout)
|
||||
process.stdout.write = function (chunk) {
|
||||
write('hack ' + chunk)
|
||||
}
|
||||
|
||||
Date.now = function () { return 1459875739796 }
|
||||
require('node:os').hostname = function () { return 'abcdefghijklmnopqr' }
|
||||
const pino = require(require.resolve('../../'))()
|
||||
pino.info('me')
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
global.process = { __proto__: process, pid: 123456 }
|
||||
Date.now = function () { return 1459875739796 }
|
||||
require('node:os').hostname = function () { return 'abcdefghijklmnopqr' }
|
||||
const pino = require(require.resolve('./../../'))
|
||||
const asyncLogger = pino(pino.destination({ sync: false })).child({ hello: 'world' })
|
||||
asyncLogger.info('h')
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
global.process = { __proto__: process, pid: 123456 }
|
||||
Date.now = function () { return 1459875739796 }
|
||||
require('node:os').hostname = function () { return 'abcdefghijklmnopqr' }
|
||||
const pino = require(require.resolve('./../../'))
|
||||
const dest = pino.destination({ dest: 1, minLength: 4096, sync: false })
|
||||
const logger = pino({}, dest)
|
||||
logger.info('hello')
|
||||
logger.info('world')
|
||||
process.exit(0)
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
global.process = { __proto__: process, pid: 123456 }
|
||||
Date.now = function () { return 1459875739796 }
|
||||
require('node:os').hostname = function () { return 'abcdefghijklmnopqr' }
|
||||
const pino = require(require.resolve('./../../'))
|
||||
const dest = pino.destination({ dest: 1, minLength: 4096, sync: false })
|
||||
const logger = pino({}, dest)
|
||||
logger.info('hello')
|
||||
logger.info('world')
|
||||
dest.flushSync()
|
||||
process.exit(0)
|
||||
+6
@@ -0,0 +1,6 @@
|
||||
global.process = { __proto__: process, pid: 123456 }
|
||||
Date.now = function () { return 1459875739796 }
|
||||
require('node:os').hostname = function () { return 'abcdefghijklmnopqr' }
|
||||
const pino = require(require.resolve('./../../'))
|
||||
const asyncLogger = pino(pino.destination({ minLength: 4096, sync: false }))
|
||||
asyncLogger.info('h')
|
||||
+2
@@ -0,0 +1,2 @@
|
||||
// This is a syntax error
|
||||
import
|
||||
+20
@@ -0,0 +1,20 @@
|
||||
'use strict'
|
||||
|
||||
const fs = require('node:fs')
|
||||
const { once } = require('node:events')
|
||||
const { Transform } = require('node:stream')
|
||||
|
||||
async function run (opts) {
|
||||
if (!opts.destination) throw new Error('kaboom')
|
||||
const stream = fs.createWriteStream(opts.destination)
|
||||
await once(stream, 'open')
|
||||
const t = new Transform({
|
||||
transform (chunk, enc, cb) {
|
||||
setImmediate(cb, null, chunk.toString().toUpperCase())
|
||||
}
|
||||
})
|
||||
t.pipe(stream)
|
||||
return t
|
||||
}
|
||||
|
||||
module.exports = run
|
||||
+13
@@ -0,0 +1,13 @@
|
||||
'use strict'
|
||||
|
||||
const fs = require('node:fs')
|
||||
const { once } = require('node:events')
|
||||
|
||||
async function run (opts) {
|
||||
if (!opts.destination) throw new Error('kaboom')
|
||||
const stream = fs.createWriteStream(opts.destination)
|
||||
await once(stream, 'open')
|
||||
return stream
|
||||
}
|
||||
|
||||
module.exports = run
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
import { createWriteStream } from 'node:fs'
|
||||
import { once } from 'node:events'
|
||||
|
||||
export default async function run (opts) {
|
||||
const stream = createWriteStream(opts.destination)
|
||||
await once(stream, 'open')
|
||||
return stream
|
||||
}
|
||||
+16
@@ -0,0 +1,16 @@
|
||||
'use strict'
|
||||
|
||||
const pino = require('../..')
|
||||
const transport = pino.transport({
|
||||
target: './to-file-transport-with-transform.js',
|
||||
options: {
|
||||
destination: process.argv[2]
|
||||
}
|
||||
})
|
||||
const logger = pino(transport)
|
||||
|
||||
logger.info('Hello')
|
||||
|
||||
logger.info('World')
|
||||
|
||||
process.exit(0)
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
'use strict'
|
||||
|
||||
const pino = require('../..')
|
||||
const transport = pino.transport({
|
||||
target: 'pino/file'
|
||||
})
|
||||
const logger = pino(transport)
|
||||
|
||||
logger.info('Hello')
|
||||
|
||||
process.exit(0)
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
'use strict'
|
||||
|
||||
const pino = require('../..')
|
||||
const transport = pino.transport({
|
||||
target: 'pino/file'
|
||||
})
|
||||
const logger = pino(transport)
|
||||
|
||||
transport.on('ready', function () {
|
||||
logger.info('Hello')
|
||||
process.exit(0)
|
||||
})
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
'use strict'
|
||||
|
||||
const { join } = require('node:path')
|
||||
const pino = require('../..')
|
||||
const transport = pino.transport({
|
||||
target: join(__dirname, 'transport-worker.js')
|
||||
})
|
||||
const logger = pino(transport)
|
||||
logger.info('Hello')
|
||||
+29
@@ -0,0 +1,29 @@
|
||||
'use strict'
|
||||
|
||||
const pino = require('../..')
|
||||
const transport = pino.transport({
|
||||
targets: [{
|
||||
level: 'info',
|
||||
target: 'pino/file',
|
||||
options: {
|
||||
destination: process.argv[2]
|
||||
}
|
||||
}]
|
||||
})
|
||||
const logger = pino(transport)
|
||||
|
||||
const toWrite = 1000000
|
||||
transport.on('ready', run)
|
||||
|
||||
let total = 0
|
||||
|
||||
function run () {
|
||||
if (total++ === 8) {
|
||||
return
|
||||
}
|
||||
|
||||
for (let i = 0; i < toWrite; i++) {
|
||||
logger.info(`hello ${i}`)
|
||||
}
|
||||
transport.once('drain', run)
|
||||
}
|
||||
+9
@@ -0,0 +1,9 @@
|
||||
'use strict'
|
||||
|
||||
const pino = require('../..')
|
||||
const transport = pino.transport({
|
||||
target: 'pino/file',
|
||||
options: { destination: '1' }
|
||||
})
|
||||
const logger = pino(transport)
|
||||
logger.info('Hello')
|
||||
+21
@@ -0,0 +1,21 @@
|
||||
'use strict'
|
||||
|
||||
const build = require('pino-abstract-transport')
|
||||
const { pipeline, Transform } = require('node:stream')
|
||||
module.exports = (options) => {
|
||||
return build(function (source) {
|
||||
const myTransportStream = new Transform({
|
||||
autoDestroy: true,
|
||||
objectMode: true,
|
||||
transform (chunk, enc, cb) {
|
||||
chunk.service = 'pino'
|
||||
this.push(JSON.stringify(chunk))
|
||||
cb()
|
||||
}
|
||||
})
|
||||
pipeline(source, myTransportStream, () => {})
|
||||
return myTransportStream
|
||||
}, {
|
||||
enablePipelining: true
|
||||
})
|
||||
}
|
||||
+33
@@ -0,0 +1,33 @@
|
||||
'use strict'
|
||||
|
||||
const build = require('pino-abstract-transport')
|
||||
const { pipeline, Transform } = require('node:stream')
|
||||
module.exports = () => {
|
||||
return build(function (source) {
|
||||
const myTransportStream = new Transform({
|
||||
autoDestroy: true,
|
||||
objectMode: true,
|
||||
transform (chunk, enc, cb) {
|
||||
const {
|
||||
time,
|
||||
level,
|
||||
[source.messageKey]: body,
|
||||
[source.errorKey]: error,
|
||||
...attributes
|
||||
} = chunk
|
||||
this.push(JSON.stringify({
|
||||
severityText: source.levels.labels[level],
|
||||
body,
|
||||
attributes,
|
||||
...(error && { error })
|
||||
}))
|
||||
cb()
|
||||
}
|
||||
})
|
||||
pipeline(source, myTransportStream, () => {})
|
||||
return myTransportStream
|
||||
}, {
|
||||
enablePipelining: true,
|
||||
expectPinoConfig: true
|
||||
})
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
'use strict'
|
||||
const pino = require('../..')
|
||||
const log = pino({
|
||||
transport: {
|
||||
target: 'pino/file',
|
||||
options: { destination: 1 }
|
||||
}
|
||||
})
|
||||
log.info('hello world!')
|
||||
process.on('exit', (code) => {
|
||||
log.info('Exiting peacefully')
|
||||
})
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
'use strict'
|
||||
|
||||
const { parentPort, workerData } = require('worker_threads')
|
||||
const { Writable } = require('node:stream')
|
||||
|
||||
module.exports = (options) => {
|
||||
const myTransportStream = new Writable({
|
||||
autoDestroy: true,
|
||||
write (chunk, enc, cb) {
|
||||
parentPort.postMessage({
|
||||
code: 'EVENT',
|
||||
name: 'workerData',
|
||||
args: [workerData]
|
||||
})
|
||||
cb()
|
||||
}
|
||||
})
|
||||
return myTransportStream
|
||||
}
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
'use strict'
|
||||
|
||||
const { Writable } = require('node:stream')
|
||||
const fs = require('node:fs')
|
||||
module.exports = (options) => {
|
||||
const myTransportStream = new Writable({
|
||||
autoDestroy: true,
|
||||
write (chunk, enc, cb) {
|
||||
// Bypass console.log() to avoid flakiness
|
||||
fs.writeSync(1, chunk.toString())
|
||||
cb()
|
||||
}
|
||||
})
|
||||
return myTransportStream
|
||||
}
|
||||
+3
@@ -0,0 +1,3 @@
|
||||
module.exports = {
|
||||
completelyUnrelatedProperty: 'Just a very incorrect transport worker implementation'
|
||||
}
|
||||
+12
@@ -0,0 +1,12 @@
|
||||
'use strict'
|
||||
|
||||
const fs = require('node:fs')
|
||||
const { once } = require('node:events')
|
||||
|
||||
async function run (opts) {
|
||||
const stream = fs.createWriteStream(opts.destination)
|
||||
await once(stream, 'open')
|
||||
return stream
|
||||
}
|
||||
|
||||
module.exports = run
|
||||
+5
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"name": "transport",
|
||||
"version": "0.0.1",
|
||||
"main": "./index.js"
|
||||
}
|
||||
+18
@@ -0,0 +1,18 @@
|
||||
import * as fs from 'node:fs'
|
||||
import { once } from 'node:events'
|
||||
import { Transform } from 'node:stream'
|
||||
|
||||
async function run (opts: { destination?: fs.PathLike }): Promise<Transform> {
|
||||
if (!opts.destination) throw new Error('kaboom')
|
||||
const stream = fs.createWriteStream(opts.destination)
|
||||
await once(stream, 'open')
|
||||
const t = new Transform({
|
||||
transform (chunk, enc, cb) {
|
||||
setImmediate(cb, null, chunk.toString().toUpperCase())
|
||||
}
|
||||
})
|
||||
t.pipe(stream)
|
||||
return t
|
||||
}
|
||||
|
||||
export default run
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
import * as fs from 'node:fs'
|
||||
import { once } from 'node:events'
|
||||
|
||||
async function run (opts: { destination?: fs.PathLike }): Promise<fs.WriteStream> {
|
||||
if (!opts.destination) throw new Error('kaboom')
|
||||
const stream = fs.createWriteStream(opts.destination, { encoding: 'utf8' })
|
||||
await once(stream, 'open')
|
||||
return stream
|
||||
}
|
||||
|
||||
export default run
|
||||
+36
@@ -0,0 +1,36 @@
|
||||
#!/usr/bin/env node
|
||||
|
||||
const execa = require('execa')
|
||||
const fs = require('node:fs')
|
||||
|
||||
const existsSync = fs.existsSync
|
||||
const stat = fs.promises.stat
|
||||
|
||||
// Hardcoded parameters
|
||||
const esVersions = ['es5', 'es6', 'es2017', 'esnext']
|
||||
const filesToTranspile = ['to-file-transport.ts']
|
||||
|
||||
async function transpile () {
|
||||
process.chdir(__dirname)
|
||||
|
||||
for (const sourceFileName of filesToTranspile) {
|
||||
const sourceStat = await stat(sourceFileName)
|
||||
|
||||
for (const esVersion of esVersions) {
|
||||
const intermediateFileName = sourceFileName.replace(/\.ts$/, '.js')
|
||||
const targetFileName = sourceFileName.replace(/\.ts$/, `.${esVersion}.cjs`)
|
||||
|
||||
const shouldTranspile = !existsSync(targetFileName) || (await stat(targetFileName)).mtimeMs < sourceStat.mtimeMs
|
||||
|
||||
if (shouldTranspile) {
|
||||
await execa('tsc', ['--target', esVersion, '--module', 'commonjs', sourceFileName])
|
||||
await execa('mv', [intermediateFileName, targetFileName])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
transpile().catch(err => {
|
||||
process.exitCode = 1
|
||||
throw err
|
||||
})
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
import pino from '../../..'
|
||||
import { join } from 'node:path'
|
||||
|
||||
const transport = pino.transport({
|
||||
target: join(__dirname, 'to-file-transport-with-transform.ts'),
|
||||
options: {
|
||||
destination: process.argv[2]
|
||||
}
|
||||
})
|
||||
const logger = pino(transport)
|
||||
|
||||
logger.info('Hello')
|
||||
logger.info('World')
|
||||
|
||||
process.exit(0)
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
import pino from '../../..'
|
||||
|
||||
const transport = pino.transport({
|
||||
target: 'pino/file'
|
||||
})
|
||||
const logger = pino(transport)
|
||||
|
||||
logger.info('Hello')
|
||||
|
||||
process.exit(0)
|
||||
+11
@@ -0,0 +1,11 @@
|
||||
import pino from '../../..'
|
||||
|
||||
const transport = pino.transport({
|
||||
target: 'pino/file'
|
||||
})
|
||||
const logger = pino(transport)
|
||||
|
||||
transport.on('ready', function () {
|
||||
logger.info('Hello')
|
||||
process.exit(0)
|
||||
})
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
import { join } from 'node:path'
|
||||
import pino from '../../..'
|
||||
|
||||
const transport = pino.transport({
|
||||
target: join(__dirname, 'transport-worker.ts')
|
||||
})
|
||||
const logger = pino(transport)
|
||||
logger.info('Hello')
|
||||
+8
@@ -0,0 +1,8 @@
|
||||
import pino from '../../..'
|
||||
|
||||
const transport = pino.transport({
|
||||
target: 'pino/file',
|
||||
options: { destination: '1' }
|
||||
})
|
||||
const logger = pino(transport)
|
||||
logger.info('Hello')
|
||||
+14
@@ -0,0 +1,14 @@
|
||||
import { Writable } from 'node:stream'
|
||||
|
||||
export default (): Writable => {
|
||||
const myTransportStream = new Writable({
|
||||
autoDestroy: true,
|
||||
write (chunk, _enc, cb) {
|
||||
console.log(chunk.toString())
|
||||
cb()
|
||||
},
|
||||
defaultEncoding: 'utf8'
|
||||
})
|
||||
|
||||
return myTransportStream
|
||||
}
|
||||
+364
@@ -0,0 +1,364 @@
|
||||
'use strict'
|
||||
/* eslint no-prototype-builtins: 0 */
|
||||
|
||||
const test = require('node:test')
|
||||
const assert = require('node:assert')
|
||||
const { hostname } = require('node:os')
|
||||
const { join } = require('node:path')
|
||||
const { readFile } = require('node:fs').promises
|
||||
const tspl = require('@matteo.collina/tspl')
|
||||
|
||||
const { sink, match, once, watchFileCreated, file } = require('./helper')
|
||||
const pino = require('../')
|
||||
|
||||
test('level formatter', async () => {
|
||||
const stream = sink()
|
||||
const logger = pino({
|
||||
formatters: {
|
||||
level (label, number) {
|
||||
return {
|
||||
log: {
|
||||
level: label
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}, stream)
|
||||
|
||||
const o = once(stream, 'data')
|
||||
logger.info('hello world')
|
||||
match(await o, {
|
||||
log: {
|
||||
level: 'info'
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
test('bindings formatter', async () => {
|
||||
const stream = sink()
|
||||
const logger = pino({
|
||||
formatters: {
|
||||
bindings (bindings) {
|
||||
return {
|
||||
process: {
|
||||
pid: bindings.pid
|
||||
},
|
||||
host: {
|
||||
name: bindings.hostname
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}, stream)
|
||||
|
||||
const o = once(stream, 'data')
|
||||
logger.info('hello world')
|
||||
match(await o, {
|
||||
process: {
|
||||
pid: process.pid
|
||||
},
|
||||
host: {
|
||||
name: hostname()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
test('no bindings formatter', async () => {
|
||||
const stream = sink()
|
||||
const logger = pino({
|
||||
formatters: {
|
||||
bindings (bindings) {
|
||||
return null
|
||||
}
|
||||
}
|
||||
}, stream)
|
||||
|
||||
const o = once(stream, 'data')
|
||||
logger.info('hello world')
|
||||
const log = await o
|
||||
assert.equal(log.hasOwnProperty('pid'), false)
|
||||
assert.equal(log.hasOwnProperty('hostname'), false)
|
||||
match(log, { msg: 'hello world' })
|
||||
})
|
||||
|
||||
test('log formatter', async (t) => {
|
||||
const plan = tspl(t, { plan: 1 })
|
||||
const stream = sink()
|
||||
const logger = pino({
|
||||
formatters: {
|
||||
log (obj) {
|
||||
plan.equal(obj.hasOwnProperty('msg'), false)
|
||||
return { hello: 'world', ...obj }
|
||||
}
|
||||
}
|
||||
}, stream)
|
||||
|
||||
const o = once(stream, 'data')
|
||||
logger.info({ foo: 'bar', nested: { object: true } }, 'hello world')
|
||||
match(await o, {
|
||||
hello: 'world',
|
||||
foo: 'bar',
|
||||
nested: { object: true }
|
||||
})
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('Formatters combined', async () => {
|
||||
const stream = sink()
|
||||
const logger = pino({
|
||||
formatters: {
|
||||
level (label, number) {
|
||||
return {
|
||||
log: {
|
||||
level: label
|
||||
}
|
||||
}
|
||||
},
|
||||
bindings (bindings) {
|
||||
return {
|
||||
process: {
|
||||
pid: bindings.pid
|
||||
},
|
||||
host: {
|
||||
name: bindings.hostname
|
||||
}
|
||||
}
|
||||
},
|
||||
log (obj) {
|
||||
return { hello: 'world', ...obj }
|
||||
}
|
||||
}
|
||||
}, stream)
|
||||
|
||||
const o = once(stream, 'data')
|
||||
logger.info({ foo: 'bar', nested: { object: true } }, 'hello world')
|
||||
match(await o, {
|
||||
log: {
|
||||
level: 'info'
|
||||
},
|
||||
process: {
|
||||
pid: process.pid
|
||||
},
|
||||
host: {
|
||||
name: hostname()
|
||||
},
|
||||
hello: 'world',
|
||||
foo: 'bar',
|
||||
nested: { object: true }
|
||||
})
|
||||
})
|
||||
|
||||
test('Formatters in child logger', async () => {
|
||||
const stream = sink()
|
||||
const logger = pino({
|
||||
formatters: {
|
||||
level (label, number) {
|
||||
return {
|
||||
log: {
|
||||
level: label
|
||||
}
|
||||
}
|
||||
},
|
||||
bindings (bindings) {
|
||||
return {
|
||||
process: {
|
||||
pid: bindings.pid
|
||||
},
|
||||
host: {
|
||||
name: bindings.hostname
|
||||
}
|
||||
}
|
||||
},
|
||||
log (obj) {
|
||||
return { hello: 'world', ...obj }
|
||||
}
|
||||
}
|
||||
}, stream)
|
||||
|
||||
const child = logger.child({
|
||||
foo: 'bar',
|
||||
nested: { object: true }
|
||||
}, {
|
||||
formatters: {
|
||||
bindings (bindings) {
|
||||
return { ...bindings, faz: 'baz' }
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const o = once(stream, 'data')
|
||||
child.info('hello world')
|
||||
match(await o, {
|
||||
log: {
|
||||
level: 'info'
|
||||
},
|
||||
process: {
|
||||
pid: process.pid
|
||||
},
|
||||
host: {
|
||||
name: hostname()
|
||||
},
|
||||
hello: 'world',
|
||||
foo: 'bar',
|
||||
nested: { object: true },
|
||||
faz: 'baz'
|
||||
})
|
||||
})
|
||||
|
||||
test('Formatters without bindings in child logger', async () => {
|
||||
const stream = sink()
|
||||
const logger = pino({
|
||||
formatters: {
|
||||
level (label, number) {
|
||||
return {
|
||||
log: {
|
||||
level: label
|
||||
}
|
||||
}
|
||||
},
|
||||
bindings (bindings) {
|
||||
return {
|
||||
process: {
|
||||
pid: bindings.pid
|
||||
},
|
||||
host: {
|
||||
name: bindings.hostname
|
||||
}
|
||||
}
|
||||
},
|
||||
log (obj) {
|
||||
return { hello: 'world', ...obj }
|
||||
}
|
||||
}
|
||||
}, stream)
|
||||
|
||||
const child = logger.child({
|
||||
foo: 'bar',
|
||||
nested: { object: true }
|
||||
}, {
|
||||
formatters: {
|
||||
log (obj) {
|
||||
return { other: 'stuff', ...obj }
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
const o = once(stream, 'data')
|
||||
child.info('hello world')
|
||||
match(await o, {
|
||||
log: {
|
||||
level: 'info'
|
||||
},
|
||||
process: {
|
||||
pid: process.pid
|
||||
},
|
||||
host: {
|
||||
name: hostname()
|
||||
},
|
||||
foo: 'bar',
|
||||
other: 'stuff',
|
||||
nested: { object: true }
|
||||
})
|
||||
})
|
||||
|
||||
test('elastic common schema format', async () => {
|
||||
const stream = sink()
|
||||
const ecs = {
|
||||
formatters: {
|
||||
level (label, number) {
|
||||
return {
|
||||
log: {
|
||||
level: label,
|
||||
logger: 'pino'
|
||||
}
|
||||
}
|
||||
},
|
||||
bindings (bindings) {
|
||||
return {
|
||||
process: {
|
||||
pid: bindings.pid
|
||||
},
|
||||
host: {
|
||||
name: bindings.hostname
|
||||
}
|
||||
}
|
||||
},
|
||||
log (obj) {
|
||||
return { ecs: { version: '1.4.0' }, ...obj }
|
||||
}
|
||||
},
|
||||
messageKey: 'message',
|
||||
timestamp: () => `,"@timestamp":"${new Date(Date.now()).toISOString()}"`
|
||||
}
|
||||
|
||||
const logger = pino({ ...ecs }, stream)
|
||||
|
||||
const o = once(stream, 'data')
|
||||
logger.info({ foo: 'bar' }, 'hello world')
|
||||
const log = await o
|
||||
assert.equal(typeof log['@timestamp'], 'string')
|
||||
match(log, {
|
||||
log: { level: 'info', logger: 'pino' },
|
||||
process: { pid: process.pid },
|
||||
host: { name: hostname() },
|
||||
ecs: { version: '1.4.0' },
|
||||
foo: 'bar',
|
||||
message: 'hello world'
|
||||
})
|
||||
})
|
||||
|
||||
test('formatter with transport', async (t) => {
|
||||
const plan = tspl(t, { plan: 1 })
|
||||
const destination = file()
|
||||
const logger = pino({
|
||||
formatters: {
|
||||
log (obj) {
|
||||
plan.equal(obj.hasOwnProperty('msg'), false)
|
||||
return { hello: 'world', ...obj }
|
||||
}
|
||||
},
|
||||
transport: {
|
||||
targets: [
|
||||
{
|
||||
target: join(__dirname, 'fixtures', 'to-file-transport.js'),
|
||||
options: { destination }
|
||||
}
|
||||
]
|
||||
}
|
||||
})
|
||||
|
||||
logger.info({ foo: 'bar', nested: { object: true } }, 'hello world')
|
||||
await watchFileCreated(destination)
|
||||
const result = JSON.parse(await readFile(destination))
|
||||
delete result.time
|
||||
match(result, {
|
||||
hello: 'world',
|
||||
foo: 'bar',
|
||||
nested: { object: true }
|
||||
})
|
||||
})
|
||||
|
||||
test('throws when custom level formatter is used with transport.targets', async () => {
|
||||
assert.throws(
|
||||
() => {
|
||||
pino({
|
||||
formatters: {
|
||||
level (label) {
|
||||
return label
|
||||
}
|
||||
},
|
||||
transport: {
|
||||
targets: [
|
||||
{
|
||||
target: 'pino/file',
|
||||
options: { destination: 'foo.log' }
|
||||
}
|
||||
]
|
||||
}
|
||||
}
|
||||
)
|
||||
},
|
||||
Error('option.transport.targets do not allow custom level formatters')
|
||||
)
|
||||
})
|
||||
+4
@@ -0,0 +1,4 @@
|
||||
import { PathLike } from 'node:fs'
|
||||
|
||||
export declare function watchFileCreated(filename: PathLike): Promise<void>
|
||||
export declare function watchForWrite(filename: PathLike, testString: string): Promise<void>
|
||||
+155
@@ -0,0 +1,155 @@
|
||||
'use strict'
|
||||
|
||||
const crypto = require('node:crypto')
|
||||
const { join } = require('node:path')
|
||||
const os = require('node:os')
|
||||
const { existsSync, readFileSync, statSync, unlinkSync } = require('node:fs')
|
||||
const writer = require('flush-write-stream')
|
||||
const split = require('split2')
|
||||
|
||||
const pid = process.pid
|
||||
const hostname = os.hostname()
|
||||
const { tmpdir } = os
|
||||
|
||||
const isWin = process.platform === 'win32'
|
||||
const isYarnPnp = process.versions.pnp !== undefined
|
||||
|
||||
function getPathToNull () {
|
||||
return isWin ? '\\\\.\\NUL' : '/dev/null'
|
||||
}
|
||||
|
||||
function once (emitter, name) {
|
||||
return new Promise((resolve, reject) => {
|
||||
if (name !== 'error') emitter.once('error', reject)
|
||||
emitter.once(name, (...args) => {
|
||||
emitter.removeListener('error', reject)
|
||||
resolve(...args)
|
||||
})
|
||||
})
|
||||
}
|
||||
|
||||
function sink (func) {
|
||||
const result = split((data) => {
|
||||
try {
|
||||
return JSON.parse(data)
|
||||
} catch (err) {
|
||||
console.log(err)
|
||||
console.log(data)
|
||||
}
|
||||
})
|
||||
if (func) result.pipe(writer.obj(func))
|
||||
return result
|
||||
}
|
||||
|
||||
function check (is, chunk, level, msg) {
|
||||
is(new Date(chunk.time) <= new Date(), true, 'time is greater than Date.now()')
|
||||
delete chunk.time
|
||||
is(chunk.pid, pid)
|
||||
is(chunk.hostname, hostname)
|
||||
is(chunk.level, level)
|
||||
is(chunk.msg, msg)
|
||||
}
|
||||
|
||||
function sleep (ms) {
|
||||
return new Promise((resolve) => {
|
||||
setTimeout(resolve, ms)
|
||||
})
|
||||
}
|
||||
|
||||
function watchFileCreated (filename) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const TIMEOUT = process.env.PINO_TEST_WAIT_WATCHFILE_TIMEOUT || 10000
|
||||
const INTERVAL = 100
|
||||
const threshold = TIMEOUT / INTERVAL
|
||||
let counter = 0
|
||||
const interval = setInterval(() => {
|
||||
const exists = existsSync(filename)
|
||||
// On some CI runs file is created but not filled
|
||||
if (exists && statSync(filename).size !== 0) {
|
||||
clearInterval(interval)
|
||||
resolve()
|
||||
} else if (counter <= threshold) {
|
||||
counter++
|
||||
} else {
|
||||
clearInterval(interval)
|
||||
reject(new Error(
|
||||
`${filename} hasn't been created within ${TIMEOUT} ms. ` +
|
||||
(exists ? 'File exist, but still empty.' : 'File not yet created.')
|
||||
))
|
||||
}
|
||||
}, INTERVAL)
|
||||
})
|
||||
}
|
||||
|
||||
function watchForWrite (filename, testString) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const TIMEOUT = process.env.PINO_TEST_WAIT_WRITE_TIMEOUT || 10000
|
||||
const INTERVAL = 100
|
||||
const threshold = TIMEOUT / INTERVAL
|
||||
let counter = 0
|
||||
const interval = setInterval(() => {
|
||||
if (readFileSync(filename).includes(testString)) {
|
||||
clearInterval(interval)
|
||||
resolve()
|
||||
} else if (counter <= threshold) {
|
||||
counter++
|
||||
} else {
|
||||
clearInterval(interval)
|
||||
reject(new Error(`'${testString}' hasn't been written to ${filename} within ${TIMEOUT} ms.`))
|
||||
}
|
||||
}, INTERVAL)
|
||||
})
|
||||
}
|
||||
|
||||
let files = []
|
||||
|
||||
function file () {
|
||||
const hash = crypto.randomBytes(12).toString('hex')
|
||||
const file = join(tmpdir(), `pino-${pid}-${hash}`)
|
||||
files.push(file)
|
||||
return file
|
||||
}
|
||||
|
||||
process.on('beforeExit', () => {
|
||||
if (files.length === 0) return
|
||||
for (const file of files) {
|
||||
try {
|
||||
unlinkSync(file)
|
||||
} catch (e) {
|
||||
}
|
||||
}
|
||||
files = []
|
||||
})
|
||||
|
||||
/**
|
||||
* match is a bare-bones object shape matcher. We should be able to replace
|
||||
* this with `assert.partialDeepStrictEqual` when v22 is our minimum.
|
||||
*
|
||||
* @param {object} found
|
||||
* @param {object} expected
|
||||
*/
|
||||
function match (found, expected) {
|
||||
for (const [key, value] of Object.entries(expected)) {
|
||||
if (Object.prototype.toString.call(value) === '[object Object]') {
|
||||
match(found[key], value)
|
||||
continue
|
||||
}
|
||||
if (value !== found[key]) {
|
||||
throw Error(`expected "${value}" but found "${found[key]}"`)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = {
|
||||
check,
|
||||
file,
|
||||
getPathToNull,
|
||||
isWin,
|
||||
isYarnPnp,
|
||||
match,
|
||||
once,
|
||||
sink,
|
||||
sleep,
|
||||
watchFileCreated,
|
||||
watchForWrite
|
||||
}
|
||||
+114
@@ -0,0 +1,114 @@
|
||||
'use strict'
|
||||
|
||||
const { describe, test } = require('node:test')
|
||||
const tspl = require('@matteo.collina/tspl')
|
||||
|
||||
const { sink, match, once } = require('./helper')
|
||||
const pino = require('../')
|
||||
|
||||
describe('log method hook', () => {
|
||||
test('gets invoked', async t => {
|
||||
const plan = tspl(t, { plan: 7 })
|
||||
|
||||
const stream = sink()
|
||||
const logger = pino({
|
||||
hooks: {
|
||||
logMethod (args, method, level) {
|
||||
plan.equal(Array.isArray(args), true)
|
||||
plan.equal(typeof level, 'number')
|
||||
plan.equal(args.length, 3)
|
||||
plan.equal(level, this.levels.values.info)
|
||||
plan.deepEqual(args, ['a', 'b', 'c'])
|
||||
|
||||
plan.equal(typeof method, 'function')
|
||||
plan.equal(method.name, 'LOG')
|
||||
|
||||
method.apply(this, [args.join('-')])
|
||||
}
|
||||
}
|
||||
}, stream)
|
||||
|
||||
const o = once(stream, 'data')
|
||||
logger.info('a', 'b', 'c')
|
||||
match(await o, { msg: 'a-b-c' })
|
||||
})
|
||||
|
||||
test('fatal method invokes hook', async t => {
|
||||
const plan = tspl(t, { plan: 1 })
|
||||
|
||||
const stream = sink()
|
||||
const logger = pino({
|
||||
hooks: {
|
||||
logMethod (args, method) {
|
||||
plan.ok(true)
|
||||
method.apply(this, [args.join('-')])
|
||||
}
|
||||
}
|
||||
}, stream)
|
||||
|
||||
const o = once(stream, 'data')
|
||||
logger.fatal('a')
|
||||
match(await o, { msg: 'a' })
|
||||
})
|
||||
|
||||
test('children get the hook', async t => {
|
||||
const plan = tspl(t, { plan: 2 })
|
||||
|
||||
const stream = sink()
|
||||
const root = pino({
|
||||
hooks: {
|
||||
logMethod (args, method) {
|
||||
plan.ok(true)
|
||||
method.apply(this, [args.join('-')])
|
||||
}
|
||||
}
|
||||
}, stream)
|
||||
const child = root.child({ child: 'one' })
|
||||
const grandchild = child.child({ child: 'two' })
|
||||
|
||||
let o = once(stream, 'data')
|
||||
child.info('a', 'b')
|
||||
match(await o, { msg: 'a-b' })
|
||||
|
||||
o = once(stream, 'data')
|
||||
grandchild.info('c', 'd')
|
||||
match(await o, { msg: 'c-d' })
|
||||
})
|
||||
|
||||
test('get log level', async t => {
|
||||
const plan = tspl(t, { plan: 2 })
|
||||
|
||||
const stream = sink()
|
||||
const logger = pino({
|
||||
hooks: {
|
||||
logMethod (args, method, level) {
|
||||
plan.equal(typeof level, 'number')
|
||||
plan.equal(level, this.levels.values.error)
|
||||
|
||||
method.apply(this, [args.join('-')])
|
||||
}
|
||||
}
|
||||
}, stream)
|
||||
|
||||
const o = once(stream, 'data')
|
||||
logger.error('a')
|
||||
match(await o, { msg: 'a' })
|
||||
})
|
||||
})
|
||||
|
||||
describe('streamWrite hook', () => {
|
||||
test('gets invoked', async () => {
|
||||
const stream = sink()
|
||||
const logger = pino({
|
||||
hooks: {
|
||||
streamWrite (s) {
|
||||
return s.replaceAll('redact-me', 'XXX')
|
||||
}
|
||||
}
|
||||
}, stream)
|
||||
|
||||
const o = once(stream, 'data')
|
||||
logger.info('hide redact-me in this string')
|
||||
match(await o, { msg: 'hide XXX in this string' })
|
||||
})
|
||||
})
|
||||
+214
@@ -0,0 +1,214 @@
|
||||
'use strict'
|
||||
|
||||
const test = require('node:test')
|
||||
const http = require('node:http')
|
||||
const os = require('node:os')
|
||||
const tspl = require('@matteo.collina/tspl')
|
||||
|
||||
const { sink, once } = require('./helper')
|
||||
const pino = require('../')
|
||||
|
||||
const { pid } = process
|
||||
const hostname = os.hostname()
|
||||
|
||||
test('http request support', async (t) => {
|
||||
const plan = tspl(t, { plan: 3 })
|
||||
let originalReq
|
||||
const instance = pino(sink((chunk, enc) => {
|
||||
plan.ok(new Date(chunk.time) <= new Date(), 'time is greater than Date.now()')
|
||||
delete chunk.time
|
||||
plan.deepEqual(chunk, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 30,
|
||||
msg: 'my request',
|
||||
req: {
|
||||
method: originalReq.method,
|
||||
url: originalReq.url,
|
||||
headers: originalReq.headers,
|
||||
remoteAddress: originalReq.socket.remoteAddress,
|
||||
remotePort: originalReq.socket.remotePort
|
||||
}
|
||||
})
|
||||
}))
|
||||
|
||||
const server = http.createServer((req, res) => {
|
||||
originalReq = req
|
||||
instance.info(req, 'my request')
|
||||
res.end('hello')
|
||||
})
|
||||
server.unref()
|
||||
server.listen()
|
||||
const err = await once(server, 'listening')
|
||||
plan.equal(err, undefined)
|
||||
const res = await once(http.get('http://localhost:' + server.address().port), 'response')
|
||||
res.resume()
|
||||
server.close()
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('http request support via serializer', async (t) => {
|
||||
const plan = tspl(t, { plan: 3 })
|
||||
let originalReq
|
||||
const instance = pino({
|
||||
serializers: {
|
||||
req: pino.stdSerializers.req
|
||||
}
|
||||
}, sink((chunk, enc) => {
|
||||
plan.ok(new Date(chunk.time) <= new Date(), 'time is greater than Date.now()')
|
||||
delete chunk.time
|
||||
plan.deepEqual(chunk, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 30,
|
||||
msg: 'my request',
|
||||
req: {
|
||||
method: originalReq.method,
|
||||
url: originalReq.url,
|
||||
headers: originalReq.headers,
|
||||
remoteAddress: originalReq.socket.remoteAddress,
|
||||
remotePort: originalReq.socket.remotePort
|
||||
}
|
||||
})
|
||||
}))
|
||||
|
||||
const server = http.createServer(function (req, res) {
|
||||
originalReq = req
|
||||
instance.info({ req }, 'my request')
|
||||
res.end('hello')
|
||||
})
|
||||
server.unref()
|
||||
server.listen()
|
||||
const err = await once(server, 'listening')
|
||||
plan.equal(err, undefined)
|
||||
|
||||
const res = await once(http.get('http://localhost:' + server.address().port), 'response')
|
||||
res.resume()
|
||||
server.close()
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('http response support', async (t) => {
|
||||
const plan = tspl(t, { plan: 3 })
|
||||
let originalRes
|
||||
const instance = pino(sink((chunk, enc) => {
|
||||
plan.ok(new Date(chunk.time) <= new Date(), 'time is greater than Date.now()')
|
||||
delete chunk.time
|
||||
plan.deepEqual(chunk, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 30,
|
||||
msg: 'my response',
|
||||
res: {
|
||||
statusCode: originalRes.statusCode,
|
||||
headers: originalRes.getHeaders()
|
||||
}
|
||||
})
|
||||
}))
|
||||
|
||||
const server = http.createServer(function (req, res) {
|
||||
originalRes = res
|
||||
res.end('hello')
|
||||
instance.info(res, 'my response')
|
||||
})
|
||||
server.unref()
|
||||
server.listen()
|
||||
const err = await once(server, 'listening')
|
||||
|
||||
plan.equal(err, undefined)
|
||||
|
||||
const res = await once(http.get('http://localhost:' + server.address().port), 'response')
|
||||
res.resume()
|
||||
server.close()
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('http response support via a serializer', async (t) => {
|
||||
const plan = tspl(t, { plan: 3 })
|
||||
const instance = pino({
|
||||
serializers: {
|
||||
res: pino.stdSerializers.res
|
||||
}
|
||||
}, sink((chunk, enc) => {
|
||||
plan.ok(new Date(chunk.time) <= new Date(), 'time is greater than Date.now()')
|
||||
delete chunk.time
|
||||
plan.deepEqual(chunk, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 30,
|
||||
msg: 'my response',
|
||||
res: {
|
||||
statusCode: 200,
|
||||
headers: {
|
||||
'x-single': 'y',
|
||||
'x-multi': [1, 2]
|
||||
}
|
||||
}
|
||||
})
|
||||
}))
|
||||
|
||||
const server = http.createServer(function (req, res) {
|
||||
res.setHeader('x-single', 'y')
|
||||
res.setHeader('x-multi', [1, 2])
|
||||
res.end('hello')
|
||||
instance.info({ res }, 'my response')
|
||||
})
|
||||
|
||||
server.unref()
|
||||
server.listen()
|
||||
const err = await once(server, 'listening')
|
||||
plan.equal(err, undefined)
|
||||
|
||||
const res = await once(http.get('http://localhost:' + server.address().port), 'response')
|
||||
res.resume()
|
||||
server.close()
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('http request support via serializer in a child', async (t) => {
|
||||
const plan = tspl(t, { plan: 3 })
|
||||
let originalReq
|
||||
const instance = pino({
|
||||
serializers: {
|
||||
req: pino.stdSerializers.req
|
||||
}
|
||||
}, sink((chunk, enc) => {
|
||||
plan.ok(new Date(chunk.time) <= new Date(), 'time is greater than Date.now()')
|
||||
delete chunk.time
|
||||
plan.deepEqual(chunk, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 30,
|
||||
msg: 'my request',
|
||||
req: {
|
||||
method: originalReq.method,
|
||||
url: originalReq.url,
|
||||
headers: originalReq.headers,
|
||||
remoteAddress: originalReq.socket.remoteAddress,
|
||||
remotePort: originalReq.socket.remotePort
|
||||
}
|
||||
})
|
||||
}))
|
||||
|
||||
const server = http.createServer(function (req, res) {
|
||||
originalReq = req
|
||||
const child = instance.child({ req })
|
||||
child.info('my request')
|
||||
res.end('hello')
|
||||
})
|
||||
|
||||
server.unref()
|
||||
server.listen()
|
||||
const err = await once(server, 'listening')
|
||||
plan.equal(err, undefined)
|
||||
|
||||
const res = await once(http.get('http://localhost:' + server.address().port), 'response')
|
||||
res.resume()
|
||||
server.close()
|
||||
|
||||
await plan
|
||||
})
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
'use strict'
|
||||
|
||||
const test = require('node:test')
|
||||
const assert = require('node:assert')
|
||||
const fs = require('node:fs')
|
||||
const path = require('node:path')
|
||||
|
||||
const pino = require('../..')()
|
||||
|
||||
test('should be the same as package.json', () => {
|
||||
const json = JSON.parse(
|
||||
fs.readFileSync(path.join(__dirname, '..', '..', 'package.json'))
|
||||
.toString('utf8')
|
||||
)
|
||||
|
||||
assert.equal(pino.version, json.version)
|
||||
})
|
||||
+179
@@ -0,0 +1,179 @@
|
||||
'use strict'
|
||||
|
||||
const { describe, test } = require('node:test')
|
||||
const assert = require('node:assert')
|
||||
|
||||
const pino = require('../')
|
||||
|
||||
const descLevels = {
|
||||
trace: 60,
|
||||
debug: 50,
|
||||
info: 40,
|
||||
warn: 30,
|
||||
error: 20,
|
||||
fatal: 10
|
||||
}
|
||||
|
||||
const ascLevels = {
|
||||
trace: 10,
|
||||
debug: 20,
|
||||
info: 30,
|
||||
warn: 40,
|
||||
error: 50,
|
||||
fatal: 60
|
||||
}
|
||||
|
||||
describe('Default levels suite', () => {
|
||||
test('can check if current level enabled', async () => {
|
||||
const log = pino({ level: 'debug' })
|
||||
assert.equal(true, log.isLevelEnabled('debug'))
|
||||
})
|
||||
|
||||
test('can check if level enabled after level set', async () => {
|
||||
const log = pino()
|
||||
assert.equal(false, log.isLevelEnabled('debug'))
|
||||
log.level = 'debug'
|
||||
assert.equal(true, log.isLevelEnabled('debug'))
|
||||
})
|
||||
|
||||
test('can check if higher level enabled', async () => {
|
||||
const log = pino({ level: 'debug' })
|
||||
assert.equal(true, log.isLevelEnabled('error'))
|
||||
})
|
||||
|
||||
test('can check if lower level is disabled', async () => {
|
||||
const log = pino({ level: 'error' })
|
||||
assert.equal(false, log.isLevelEnabled('trace'))
|
||||
})
|
||||
|
||||
test('ASC: can check if child has current level enabled', async () => {
|
||||
const log = pino().child({}, { level: 'debug' })
|
||||
assert.equal(true, log.isLevelEnabled('debug'))
|
||||
assert.equal(true, log.isLevelEnabled('error'))
|
||||
assert.equal(false, log.isLevelEnabled('trace'))
|
||||
})
|
||||
|
||||
test('can check if custom level is enabled', async () => {
|
||||
const log = pino({
|
||||
customLevels: { foo: 35 },
|
||||
level: 'debug'
|
||||
})
|
||||
assert.equal(true, log.isLevelEnabled('foo'))
|
||||
assert.equal(true, log.isLevelEnabled('error'))
|
||||
assert.equal(false, log.isLevelEnabled('trace'))
|
||||
})
|
||||
})
|
||||
|
||||
describe('Ascending levels suite', () => {
|
||||
const customLevels = ascLevels
|
||||
const levelComparison = 'ASC'
|
||||
|
||||
test('can check if current level enabled', async () => {
|
||||
const log = pino({ level: 'debug', levelComparison, customLevels, useOnlyCustomLevels: true })
|
||||
assert.equal(true, log.isLevelEnabled('debug'))
|
||||
})
|
||||
|
||||
test('can check if level enabled after level set', async () => {
|
||||
const log = pino({ levelComparison, customLevels, useOnlyCustomLevels: true })
|
||||
assert.equal(false, log.isLevelEnabled('debug'))
|
||||
log.level = 'debug'
|
||||
assert.equal(true, log.isLevelEnabled('debug'))
|
||||
})
|
||||
|
||||
test('can check if higher level enabled', async () => {
|
||||
const log = pino({ level: 'debug', levelComparison, customLevels, useOnlyCustomLevels: true })
|
||||
assert.equal(true, log.isLevelEnabled('error'))
|
||||
})
|
||||
|
||||
test('can check if lower level is disabled', async () => {
|
||||
const log = pino({ level: 'error', customLevels, useOnlyCustomLevels: true })
|
||||
assert.equal(false, log.isLevelEnabled('trace'))
|
||||
})
|
||||
|
||||
test('can check if child has current level enabled', async () => {
|
||||
const log = pino().child({ levelComparison, customLevels, useOnlyCustomLevels: true }, { level: 'debug' })
|
||||
assert.equal(true, log.isLevelEnabled('debug'))
|
||||
assert.equal(true, log.isLevelEnabled('error'))
|
||||
assert.equal(false, log.isLevelEnabled('trace'))
|
||||
})
|
||||
|
||||
test('can check if custom level is enabled', async () => {
|
||||
const log = pino({
|
||||
levelComparison,
|
||||
useOnlyCustomLevels: true,
|
||||
customLevels: { foo: 35, ...customLevels },
|
||||
level: 'debug'
|
||||
})
|
||||
assert.equal(true, log.isLevelEnabled('foo'))
|
||||
assert.equal(true, log.isLevelEnabled('error'))
|
||||
assert.equal(false, log.isLevelEnabled('trace'))
|
||||
})
|
||||
})
|
||||
|
||||
describe('Descending levels suite', () => {
|
||||
const customLevels = descLevels
|
||||
const levelComparison = 'DESC'
|
||||
|
||||
test('can check if current level enabled', async () => {
|
||||
const log = pino({ level: 'debug', levelComparison, customLevels, useOnlyCustomLevels: true })
|
||||
assert.equal(true, log.isLevelEnabled('debug'))
|
||||
})
|
||||
|
||||
test('can check if level enabled after level set', async () => {
|
||||
const log = pino({ levelComparison, customLevels, useOnlyCustomLevels: true })
|
||||
assert.equal(false, log.isLevelEnabled('debug'))
|
||||
log.level = 'debug'
|
||||
assert.equal(true, log.isLevelEnabled('debug'))
|
||||
})
|
||||
|
||||
test('can check if higher level enabled', async () => {
|
||||
const log = pino({ level: 'debug', levelComparison, customLevels, useOnlyCustomLevels: true })
|
||||
assert.equal(true, log.isLevelEnabled('error'))
|
||||
})
|
||||
|
||||
test('can check if lower level is disabled', async () => {
|
||||
const log = pino({ level: 'error', levelComparison, customLevels, useOnlyCustomLevels: true })
|
||||
assert.equal(false, log.isLevelEnabled('trace'))
|
||||
})
|
||||
|
||||
test('can check if child has current level enabled', async () => {
|
||||
const log = pino({ levelComparison, customLevels, useOnlyCustomLevels: true }).child({}, { level: 'debug' })
|
||||
assert.equal(true, log.isLevelEnabled('debug'))
|
||||
assert.equal(true, log.isLevelEnabled('error'))
|
||||
assert.equal(false, log.isLevelEnabled('trace'))
|
||||
})
|
||||
|
||||
test('can check if custom level is enabled', async () => {
|
||||
const log = pino({
|
||||
levelComparison,
|
||||
customLevels: { foo: 35, ...customLevels },
|
||||
useOnlyCustomLevels: true,
|
||||
level: 'debug'
|
||||
})
|
||||
assert.equal(true, log.isLevelEnabled('foo'))
|
||||
assert.equal(true, log.isLevelEnabled('error'))
|
||||
assert.equal(false, log.isLevelEnabled('trace'))
|
||||
})
|
||||
})
|
||||
|
||||
describe('Custom levels comparison', () => {
|
||||
test('Custom comparison returns true cause level is enabled', async () => {
|
||||
const log = pino({ level: 'error', levelComparison: () => true })
|
||||
assert.equal(true, log.isLevelEnabled('debug'))
|
||||
})
|
||||
|
||||
test('Custom comparison returns false cause level is disabled', async () => {
|
||||
const log = pino({ level: 'error', levelComparison: () => false })
|
||||
assert.equal(false, log.isLevelEnabled('debug'))
|
||||
})
|
||||
|
||||
test('Custom comparison returns true cause child level is enabled', async () => {
|
||||
const log = pino({ levelComparison: () => true }).child({ level: 'error' })
|
||||
assert.equal(true, log.isLevelEnabled('debug'))
|
||||
})
|
||||
|
||||
test('Custom comparison returns false cause child level is disabled', async () => {
|
||||
const log = pino({ levelComparison: () => false }).child({ level: 'error' })
|
||||
assert.equal(false, log.isLevelEnabled('debug'))
|
||||
})
|
||||
})
|
||||
+10
@@ -0,0 +1,10 @@
|
||||
/* global test */
|
||||
const pino = require('../../pino')
|
||||
|
||||
test('transport should work in jest', function () {
|
||||
pino({
|
||||
transport: {
|
||||
target: 'pino-pretty'
|
||||
}
|
||||
})
|
||||
})
|
||||
+810
@@ -0,0 +1,810 @@
|
||||
'use strict'
|
||||
|
||||
const { describe, test } = require('node:test')
|
||||
const assert = require('node:assert')
|
||||
const tspl = require('@matteo.collina/tspl')
|
||||
|
||||
const { sink, once, check } = require('./helper')
|
||||
const pino = require('../')
|
||||
|
||||
const levelsLib = require('../lib/levels')
|
||||
|
||||
// Silence all warnings for this test
|
||||
process.removeAllListeners('warning')
|
||||
process.on('warning', () => {})
|
||||
|
||||
test('set the level by string', async () => {
|
||||
const expected = [{
|
||||
level: 50,
|
||||
msg: 'this is an error'
|
||||
}, {
|
||||
level: 60,
|
||||
msg: 'this is fatal'
|
||||
}]
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
instance.level = 'error'
|
||||
instance.info('hello world')
|
||||
instance.error('this is an error')
|
||||
instance.fatal('this is fatal')
|
||||
const result = await once(stream, 'data')
|
||||
const current = expected.shift()
|
||||
check(assert.equal, result, current.level, current.msg)
|
||||
})
|
||||
|
||||
test('the wrong level throws', async () => {
|
||||
const instance = pino()
|
||||
assert.throws(() => {
|
||||
instance.level = 'kaboom'
|
||||
})
|
||||
})
|
||||
|
||||
test('set the level by number', async () => {
|
||||
const expected = [{
|
||||
level: 50,
|
||||
msg: 'this is an error'
|
||||
}, {
|
||||
level: 60,
|
||||
msg: 'this is fatal'
|
||||
}]
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
|
||||
instance.level = 50
|
||||
instance.info('hello world')
|
||||
instance.error('this is an error')
|
||||
instance.fatal('this is fatal')
|
||||
const result = await once(stream, 'data')
|
||||
const current = expected.shift()
|
||||
check(assert.equal, result, current.level, current.msg)
|
||||
})
|
||||
|
||||
test('exposes level string mappings', async () => {
|
||||
assert.equal(pino.levels.values.error, 50)
|
||||
})
|
||||
|
||||
test('exposes level number mappings', async () => {
|
||||
assert.equal(pino.levels.labels[50], 'error')
|
||||
})
|
||||
|
||||
test('returns level integer', async () => {
|
||||
const instance = pino({ level: 'error' })
|
||||
assert.equal(instance.levelVal, 50)
|
||||
})
|
||||
|
||||
test('child returns level integer', async () => {
|
||||
const parent = pino({ level: 'error' })
|
||||
const child = parent.child({ foo: 'bar' })
|
||||
assert.equal(child.levelVal, 50)
|
||||
})
|
||||
|
||||
test('set the level via exported pino function', async () => {
|
||||
const expected = [{
|
||||
level: 50,
|
||||
msg: 'this is an error'
|
||||
}, {
|
||||
level: 60,
|
||||
msg: 'this is fatal'
|
||||
}]
|
||||
const stream = sink()
|
||||
const instance = pino({ level: 'error' }, stream)
|
||||
|
||||
instance.info('hello world')
|
||||
instance.error('this is an error')
|
||||
instance.fatal('this is fatal')
|
||||
const result = await once(stream, 'data')
|
||||
const current = expected.shift()
|
||||
check(assert.equal, result, current.level, current.msg)
|
||||
})
|
||||
|
||||
test('level-change event', async (t) => {
|
||||
const plan = tspl(t, { plan: 8 })
|
||||
const instance = pino()
|
||||
function handle (lvl, val, prevLvl, prevVal, logger) {
|
||||
plan.equal(lvl, 'trace')
|
||||
plan.equal(val, 10)
|
||||
plan.equal(prevLvl, 'info')
|
||||
plan.equal(prevVal, 30)
|
||||
plan.equal(logger, instance)
|
||||
}
|
||||
instance.on('level-change', handle)
|
||||
instance.level = 'trace'
|
||||
instance.removeListener('level-change', handle)
|
||||
instance.level = 'info'
|
||||
|
||||
let count = 0
|
||||
|
||||
const l1 = () => count++
|
||||
const l2 = () => count++
|
||||
const l3 = () => count++
|
||||
instance.on('level-change', l1)
|
||||
instance.on('level-change', l2)
|
||||
instance.on('level-change', l3)
|
||||
|
||||
instance.level = 'trace'
|
||||
instance.removeListener('level-change', l3)
|
||||
instance.level = 'fatal'
|
||||
instance.removeListener('level-change', l1)
|
||||
instance.level = 'debug'
|
||||
instance.removeListener('level-change', l2)
|
||||
instance.level = 'info'
|
||||
|
||||
plan.equal(count, 6)
|
||||
|
||||
instance.once('level-change', (lvl, val, prevLvl, prevVal, logger) => plan.equal(logger, instance))
|
||||
instance.level = 'info'
|
||||
const child = instance.child({})
|
||||
instance.once('level-change', (lvl, val, prevLvl, prevVal, logger) => plan.equal(logger, child))
|
||||
child.level = 'trace'
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('enable', async (t) => {
|
||||
const instance = pino({
|
||||
level: 'trace',
|
||||
enabled: false
|
||||
}, sink((result, enc) => {
|
||||
throw Error('no data should be logged')
|
||||
}))
|
||||
|
||||
Object.keys(pino.levels.values).forEach((level) => {
|
||||
instance[level]('hello world')
|
||||
})
|
||||
})
|
||||
|
||||
test('silent level', async () => {
|
||||
const instance = pino({
|
||||
level: 'silent'
|
||||
}, sink((result, enc) => {
|
||||
throw Error('no data should be logged')
|
||||
}))
|
||||
|
||||
Object.keys(pino.levels.values).forEach((level) => {
|
||||
instance[level]('hello world')
|
||||
})
|
||||
})
|
||||
|
||||
test('set silent via Infinity', async () => {
|
||||
const instance = pino({
|
||||
level: Infinity
|
||||
}, sink((result, enc) => {
|
||||
throw Error('no data should be logged')
|
||||
}))
|
||||
|
||||
Object.keys(pino.levels.values).forEach((level) => {
|
||||
instance[level]('hello world')
|
||||
})
|
||||
})
|
||||
|
||||
test('exposed levels', async () => {
|
||||
assert.deepEqual(Object.keys(pino.levels.values), [
|
||||
'trace',
|
||||
'debug',
|
||||
'info',
|
||||
'warn',
|
||||
'error',
|
||||
'fatal'
|
||||
])
|
||||
})
|
||||
|
||||
test('exposed labels', async () => {
|
||||
assert.deepEqual(Object.keys(pino.levels.labels), [
|
||||
'10',
|
||||
'20',
|
||||
'30',
|
||||
'40',
|
||||
'50',
|
||||
'60'
|
||||
])
|
||||
})
|
||||
|
||||
test('setting level in child', async (t) => {
|
||||
const plan = tspl(t, { plan: 10 })
|
||||
const expected = [{
|
||||
level: 50,
|
||||
msg: 'this is an error'
|
||||
}, {
|
||||
level: 60,
|
||||
msg: 'this is fatal'
|
||||
}]
|
||||
const instance = pino(sink((result, enc, cb) => {
|
||||
const current = expected.shift()
|
||||
check(plan.equal, result, current.level, current.msg)
|
||||
cb()
|
||||
})).child({ level: 30 })
|
||||
|
||||
instance.level = 'error'
|
||||
instance.info('hello world')
|
||||
instance.error('this is an error')
|
||||
instance.fatal('this is fatal')
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('setting level by assigning a number to level', async () => {
|
||||
const instance = pino()
|
||||
assert.equal(instance.levelVal, 30)
|
||||
assert.equal(instance.level, 'info')
|
||||
instance.level = 50
|
||||
assert.equal(instance.levelVal, 50)
|
||||
assert.equal(instance.level, 'error')
|
||||
})
|
||||
|
||||
test('setting level by number to unknown value results in a throw', async () => {
|
||||
const instance = pino()
|
||||
assert.throws(() => { instance.level = 973 })
|
||||
})
|
||||
|
||||
test('setting level by assigning a known label to level', async () => {
|
||||
const instance = pino()
|
||||
assert.equal(instance.levelVal, 30)
|
||||
assert.equal(instance.level, 'info')
|
||||
instance.level = 'error'
|
||||
assert.equal(instance.levelVal, 50)
|
||||
assert.equal(instance.level, 'error')
|
||||
})
|
||||
|
||||
test('levelVal is read only', async () => {
|
||||
const instance = pino()
|
||||
assert.throws(() => { instance.levelVal = 20 })
|
||||
})
|
||||
|
||||
test('produces labels when told to', async (t) => {
|
||||
const plan = tspl(t, { plan: 5 })
|
||||
const expected = [{
|
||||
level: 'info',
|
||||
msg: 'hello world'
|
||||
}]
|
||||
const instance = pino({
|
||||
formatters: {
|
||||
level (label, number) {
|
||||
return { level: label }
|
||||
}
|
||||
}
|
||||
}, sink((result, enc, cb) => {
|
||||
const current = expected.shift()
|
||||
check(plan.equal, result, current.level, current.msg)
|
||||
cb()
|
||||
}))
|
||||
|
||||
instance.info('hello world')
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('resets levels from labels to numbers', async (t) => {
|
||||
const plan = tspl(t, { plan: 5 })
|
||||
const expected = [{
|
||||
level: 30,
|
||||
msg: 'hello world'
|
||||
}]
|
||||
pino({ useLevelLabels: true })
|
||||
const instance = pino({ useLevelLabels: false }, sink((result, enc, cb) => {
|
||||
const current = expected.shift()
|
||||
check(plan.equal, result, current.level, current.msg)
|
||||
cb()
|
||||
}))
|
||||
|
||||
instance.info('hello world')
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('changes label naming when told to', async (t) => {
|
||||
const plan = tspl(t, { plan: 2 })
|
||||
const expected = [{
|
||||
priority: 30,
|
||||
msg: 'hello world'
|
||||
}]
|
||||
const instance = pino({
|
||||
formatters: {
|
||||
level (label, number) {
|
||||
return { priority: number }
|
||||
}
|
||||
}
|
||||
}, sink((result, enc, cb) => {
|
||||
const current = expected.shift()
|
||||
plan.equal(result.priority, current.priority)
|
||||
plan.equal(result.msg, current.msg)
|
||||
cb()
|
||||
}))
|
||||
|
||||
instance.info('hello world')
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('children produce labels when told to', async (t) => {
|
||||
const plan = tspl(t, { plan: 10 })
|
||||
const expected = [
|
||||
{
|
||||
level: 'info',
|
||||
msg: 'child 1'
|
||||
},
|
||||
{
|
||||
level: 'info',
|
||||
msg: 'child 2'
|
||||
}
|
||||
]
|
||||
const instance = pino({
|
||||
formatters: {
|
||||
level (label, number) {
|
||||
return { level: label }
|
||||
}
|
||||
}
|
||||
}, sink((result, enc, cb) => {
|
||||
const current = expected.shift()
|
||||
check(plan.equal, result, current.level, current.msg)
|
||||
cb()
|
||||
}))
|
||||
|
||||
const child1 = instance.child({ name: 'child1' })
|
||||
const child2 = child1.child({ name: 'child2' })
|
||||
|
||||
child1.info('child 1')
|
||||
child2.info('child 2')
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('produces labels for custom levels', async (t) => {
|
||||
const plan = tspl(t, { plan: 10 })
|
||||
const expected = [
|
||||
{
|
||||
level: 'info',
|
||||
msg: 'hello world'
|
||||
},
|
||||
{
|
||||
level: 'foo',
|
||||
msg: 'foobar'
|
||||
}
|
||||
]
|
||||
const opts = {
|
||||
formatters: {
|
||||
level (label, number) {
|
||||
return { level: label }
|
||||
}
|
||||
},
|
||||
customLevels: {
|
||||
foo: 35
|
||||
}
|
||||
}
|
||||
const instance = pino(opts, sink((result, enc, cb) => {
|
||||
const current = expected.shift()
|
||||
check(plan.equal, result, current.level, current.msg)
|
||||
cb()
|
||||
}))
|
||||
|
||||
instance.info('hello world')
|
||||
instance.foo('foobar')
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('setting levelKey does not affect labels when told to', async (t) => {
|
||||
const plan = tspl(t, { plan: 1 })
|
||||
const instance = pino(
|
||||
{
|
||||
formatters: {
|
||||
level (label, number) {
|
||||
return { priority: label }
|
||||
}
|
||||
}
|
||||
},
|
||||
sink((result, enc, cb) => {
|
||||
plan.equal(result.priority, 'info')
|
||||
cb()
|
||||
})
|
||||
)
|
||||
|
||||
instance.info('hello world')
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('throws when creating a default label that does not exist in logger levels', async () => {
|
||||
const defaultLevel = 'foo'
|
||||
assert.throws(
|
||||
() => {
|
||||
pino({
|
||||
customLevels: {
|
||||
bar: 5
|
||||
},
|
||||
level: defaultLevel
|
||||
})
|
||||
},
|
||||
Error(`default level:${defaultLevel} must be included in custom levels`)
|
||||
)
|
||||
})
|
||||
|
||||
test('throws when creating a default value that does not exist in logger levels', async () => {
|
||||
const defaultLevel = 15
|
||||
assert.throws(
|
||||
() => {
|
||||
pino({
|
||||
customLevels: {
|
||||
bar: 5
|
||||
},
|
||||
level: defaultLevel
|
||||
})
|
||||
},
|
||||
Error(`default level:${defaultLevel} must be included in custom levels`)
|
||||
)
|
||||
})
|
||||
|
||||
test('throws when creating a default value that does not exist in logger levels', async ({ equal, throws }) => {
|
||||
assert.throws(
|
||||
() => {
|
||||
pino({
|
||||
customLevels: {
|
||||
foo: 5
|
||||
},
|
||||
useOnlyCustomLevels: true
|
||||
})
|
||||
},
|
||||
/default level:info must be included in custom levels/
|
||||
)
|
||||
})
|
||||
|
||||
test('passes when creating a default value that exists in logger levels', async () => {
|
||||
pino({
|
||||
level: 30
|
||||
})
|
||||
})
|
||||
|
||||
test('log null value when message is null', async () => {
|
||||
const expected = {
|
||||
msg: null,
|
||||
level: 30
|
||||
}
|
||||
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
instance.level = 'info'
|
||||
instance.info(null)
|
||||
|
||||
const result = await once(stream, 'data')
|
||||
check(assert.equal, result, expected.level, expected.msg)
|
||||
})
|
||||
|
||||
test('formats when base param is null', async () => {
|
||||
const expected = {
|
||||
msg: 'a string',
|
||||
level: 30
|
||||
}
|
||||
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
instance.level = 'info'
|
||||
instance.info(null, 'a %s', 'string')
|
||||
|
||||
const result = await once(stream, 'data')
|
||||
check(assert.equal, result, expected.level, expected.msg)
|
||||
})
|
||||
|
||||
test('fatal method sync-flushes the destination if sync flushing is available', async (t) => {
|
||||
const plan = tspl(t, { plan: 2 })
|
||||
const stream = sink()
|
||||
stream.flushSync = () => {
|
||||
plan.ok('destination flushed')
|
||||
}
|
||||
const instance = pino(stream)
|
||||
instance.fatal('this is fatal')
|
||||
await once(stream, 'data')
|
||||
plan.doesNotThrow(() => {
|
||||
stream.flushSync = undefined
|
||||
instance.fatal('this is fatal')
|
||||
})
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('fatal method should call async when sync-flushing fails', async (t) => {
|
||||
const plan = tspl(t, { plan: 1 })
|
||||
const messages = [
|
||||
'this is fatal 1'
|
||||
]
|
||||
const stream = sink((result) => assert.equal(result.msg, messages.shift()))
|
||||
stream.flushSync = () => { throw new Error('Error') }
|
||||
stream.flush = () => { throw Error('flush should be called') }
|
||||
|
||||
const instance = pino(stream)
|
||||
plan.doesNotThrow(() => instance.fatal(messages[0]))
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('calling silent method on logger instance', async () => {
|
||||
const instance = pino({ level: 'silent' }, sink((result, enc) => {
|
||||
throw Error('no data should be logged')
|
||||
}))
|
||||
instance.silent('hello world')
|
||||
})
|
||||
|
||||
test('calling silent method on child logger', async () => {
|
||||
const child = pino({ level: 'silent' }, sink((result, enc) => {
|
||||
throw Error('no data should be logged')
|
||||
})).child({})
|
||||
child.silent('hello world')
|
||||
})
|
||||
|
||||
test('changing level from info to silent and back to info', async () => {
|
||||
const expected = {
|
||||
level: 30,
|
||||
msg: 'hello world'
|
||||
}
|
||||
const stream = sink()
|
||||
const instance = pino({ level: 'info' }, stream)
|
||||
|
||||
instance.level = 'silent'
|
||||
instance.info('hello world')
|
||||
let result = stream.read()
|
||||
assert.equal(result, null)
|
||||
|
||||
instance.level = 'info'
|
||||
instance.info('hello world')
|
||||
result = await once(stream, 'data')
|
||||
check(assert.equal, result, expected.level, expected.msg)
|
||||
})
|
||||
|
||||
test('changing level from info to silent and back to info in child logger', async () => {
|
||||
const expected = {
|
||||
level: 30,
|
||||
msg: 'hello world'
|
||||
}
|
||||
const stream = sink()
|
||||
const child = pino({ level: 'info' }, stream).child({})
|
||||
|
||||
child.level = 'silent'
|
||||
child.info('hello world')
|
||||
let result = stream.read()
|
||||
assert.equal(result, null)
|
||||
|
||||
child.level = 'info'
|
||||
child.info('hello world')
|
||||
result = await once(stream, 'data')
|
||||
check(assert.equal, result, expected.level, expected.msg)
|
||||
})
|
||||
|
||||
describe('changing level respects level comparison set to', () => {
|
||||
const ascLevels = {
|
||||
debug: 1,
|
||||
info: 2,
|
||||
warn: 3
|
||||
}
|
||||
|
||||
const descLevels = {
|
||||
debug: 3,
|
||||
info: 2,
|
||||
warn: 1
|
||||
}
|
||||
|
||||
const expected = {
|
||||
level: 2,
|
||||
msg: 'hello world'
|
||||
}
|
||||
|
||||
test('ASC in parent logger', async () => {
|
||||
const customLevels = ascLevels
|
||||
const levelComparison = 'ASC'
|
||||
|
||||
const stream = sink()
|
||||
const logger = pino({ levelComparison, customLevels, useOnlyCustomLevels: true, level: 'info' }, stream)
|
||||
|
||||
logger.level = 'warn'
|
||||
logger.info('hello world')
|
||||
let result = stream.read()
|
||||
assert.equal(result, null)
|
||||
|
||||
logger.level = 'debug'
|
||||
logger.info('hello world')
|
||||
result = await once(stream, 'data')
|
||||
check(assert.equal, result, expected.level, expected.msg)
|
||||
})
|
||||
|
||||
test('DESC in parent logger', async () => {
|
||||
const customLevels = descLevels
|
||||
const levelComparison = 'DESC'
|
||||
|
||||
const stream = sink()
|
||||
const logger = pino({ levelComparison, customLevels, useOnlyCustomLevels: true, level: 'info' }, stream)
|
||||
|
||||
logger.level = 'warn'
|
||||
logger.info('hello world')
|
||||
let result = stream.read()
|
||||
assert.equal(result, null)
|
||||
|
||||
logger.level = 'debug'
|
||||
logger.info('hello world')
|
||||
result = await once(stream, 'data')
|
||||
check(assert.equal, result, expected.level, expected.msg)
|
||||
})
|
||||
|
||||
test('custom function in parent logger', async () => {
|
||||
const customLevels = {
|
||||
info: 2,
|
||||
debug: 345,
|
||||
warn: 789
|
||||
}
|
||||
const levelComparison = (current, expected) => {
|
||||
if (expected === customLevels.warn) return false
|
||||
return true
|
||||
}
|
||||
|
||||
const stream = sink()
|
||||
const logger = pino({ levelComparison, customLevels, useOnlyCustomLevels: true, level: 'info' }, stream)
|
||||
|
||||
logger.level = 'warn'
|
||||
logger.info('hello world')
|
||||
let result = stream.read()
|
||||
assert.equal(result, null)
|
||||
|
||||
logger.level = 'debug'
|
||||
logger.info('hello world')
|
||||
result = await once(stream, 'data')
|
||||
check(assert.equal, result, expected.level, expected.msg)
|
||||
})
|
||||
|
||||
test('ASC in child logger', async () => {
|
||||
const customLevels = ascLevels
|
||||
const levelComparison = 'ASC'
|
||||
|
||||
const stream = sink()
|
||||
const logger = pino({ levelComparison, customLevels, useOnlyCustomLevels: true, level: 'info' }, stream).child({ })
|
||||
|
||||
logger.level = 'warn'
|
||||
logger.info('hello world')
|
||||
let result = stream.read()
|
||||
assert.equal(result, null)
|
||||
|
||||
logger.level = 'debug'
|
||||
logger.info('hello world')
|
||||
result = await once(stream, 'data')
|
||||
check(assert.equal, result, expected.level, expected.msg)
|
||||
})
|
||||
|
||||
test('DESC in parent logger', async () => {
|
||||
const customLevels = descLevels
|
||||
const levelComparison = 'DESC'
|
||||
|
||||
const stream = sink()
|
||||
const logger = pino({ levelComparison, customLevels, useOnlyCustomLevels: true, level: 'info' }, stream).child({ })
|
||||
|
||||
logger.level = 'warn'
|
||||
logger.info('hello world')
|
||||
let result = stream.read()
|
||||
assert.equal(result, null)
|
||||
|
||||
logger.level = 'debug'
|
||||
logger.info('hello world')
|
||||
result = await once(stream, 'data')
|
||||
check(assert.equal, result, expected.level, expected.msg)
|
||||
})
|
||||
|
||||
test('custom function in child logger', async () => {
|
||||
const customLevels = {
|
||||
info: 2,
|
||||
debug: 345,
|
||||
warn: 789
|
||||
}
|
||||
const levelComparison = (current, expected) => {
|
||||
if (expected === customLevels.warn) return false
|
||||
return true
|
||||
}
|
||||
|
||||
const stream = sink()
|
||||
const logger = pino({ levelComparison, customLevels, useOnlyCustomLevels: true, level: 'info' }, stream).child({ })
|
||||
|
||||
logger.level = 'warn'
|
||||
logger.info('hello world')
|
||||
let result = stream.read()
|
||||
assert.equal(result, null)
|
||||
|
||||
logger.level = 'debug'
|
||||
logger.info('hello world')
|
||||
result = await once(stream, 'data')
|
||||
check(assert.equal, result, expected.level, expected.msg)
|
||||
})
|
||||
})
|
||||
|
||||
test('changing level respects level comparison DESC', async () => {
|
||||
const customLevels = {
|
||||
warn: 1,
|
||||
info: 2,
|
||||
debug: 3
|
||||
}
|
||||
|
||||
const levelComparison = 'DESC'
|
||||
|
||||
const expected = {
|
||||
level: 2,
|
||||
msg: 'hello world'
|
||||
}
|
||||
|
||||
const stream = sink()
|
||||
const logger = pino({ levelComparison, customLevels, useOnlyCustomLevels: true, level: 'info' }, stream)
|
||||
|
||||
logger.level = 'warn'
|
||||
logger.info('hello world')
|
||||
let result = stream.read()
|
||||
assert.equal(result, null)
|
||||
|
||||
logger.level = 'debug'
|
||||
logger.info('hello world')
|
||||
result = await once(stream, 'data')
|
||||
check(assert.equal, result, expected.level, expected.msg)
|
||||
})
|
||||
|
||||
// testing for potential loss of Pino constructor scope from serializers - an edge case with circular refs see: https://github.com/pinojs/pino/issues/833
|
||||
test('trying to get levels when `this` is no longer a Pino instance returns an empty string', async () => {
|
||||
const notPinoInstance = { some: 'object', getLevel: levelsLib.getLevel }
|
||||
const blankedLevelValue = notPinoInstance.getLevel()
|
||||
assert.equal(blankedLevelValue, '')
|
||||
})
|
||||
|
||||
test('accepts capital letter for INFO level', async () => {
|
||||
const stream = sink()
|
||||
const logger = pino({
|
||||
level: 'INFO'
|
||||
}, stream)
|
||||
|
||||
logger.info('test')
|
||||
const { level } = await once(stream, 'data')
|
||||
assert.equal(level, 30)
|
||||
})
|
||||
|
||||
test('accepts capital letter for FATAL level', async () => {
|
||||
const stream = sink()
|
||||
const logger = pino({
|
||||
level: 'FATAL'
|
||||
}, stream)
|
||||
|
||||
logger.fatal('test')
|
||||
const { level } = await once(stream, 'data')
|
||||
assert.equal(level, 60)
|
||||
})
|
||||
|
||||
test('accepts capital letter for ERROR level', async () => {
|
||||
const stream = sink()
|
||||
const logger = pino({
|
||||
level: 'ERROR'
|
||||
}, stream)
|
||||
|
||||
logger.error('test')
|
||||
const { level } = await once(stream, 'data')
|
||||
assert.equal(level, 50)
|
||||
})
|
||||
|
||||
test('accepts capital letter for WARN level', async () => {
|
||||
const stream = sink()
|
||||
const logger = pino({
|
||||
level: 'WARN'
|
||||
}, stream)
|
||||
|
||||
logger.warn('test')
|
||||
const { level } = await once(stream, 'data')
|
||||
assert.equal(level, 40)
|
||||
})
|
||||
|
||||
test('accepts capital letter for DEBUG level', async () => {
|
||||
const stream = sink()
|
||||
const logger = pino({
|
||||
level: 'DEBUG'
|
||||
}, stream)
|
||||
|
||||
logger.debug('test')
|
||||
const { level } = await once(stream, 'data')
|
||||
assert.equal(level, 20)
|
||||
})
|
||||
|
||||
test('accepts capital letter for TRACE level', async () => {
|
||||
const stream = sink()
|
||||
const logger = pino({
|
||||
level: 'TRACE'
|
||||
}, stream)
|
||||
|
||||
logger.trace('test')
|
||||
const { level } = await once(stream, 'data')
|
||||
assert.equal(level, 10)
|
||||
})
|
||||
+120
@@ -0,0 +1,120 @@
|
||||
'use strict'
|
||||
|
||||
const test = require('node:test')
|
||||
const os = require('node:os')
|
||||
const tspl = require('@matteo.collina/tspl')
|
||||
|
||||
const pino = require('../')
|
||||
|
||||
const { pid } = process
|
||||
const hostname = os.hostname()
|
||||
|
||||
test('metadata works', async (t) => {
|
||||
const plan = tspl(t, { plan: 7 })
|
||||
const now = Date.now()
|
||||
const instance = pino({}, {
|
||||
[Symbol.for('pino.metadata')]: true,
|
||||
write (chunk) {
|
||||
plan.equal(instance, this.lastLogger)
|
||||
plan.equal(30, this.lastLevel)
|
||||
plan.equal('a msg', this.lastMsg)
|
||||
plan.ok(Number(this.lastTime) >= now)
|
||||
plan.deepEqual(this.lastObj, { hello: 'world' })
|
||||
const result = JSON.parse(chunk)
|
||||
plan.ok(new Date(result.time) <= new Date(), 'time is greater than Date.now()')
|
||||
delete result.time
|
||||
plan.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 30,
|
||||
hello: 'world',
|
||||
msg: 'a msg'
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
instance.info({ hello: 'world' }, 'a msg')
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('child loggers works', async (t) => {
|
||||
const plan = tspl(t, { plan: 6 })
|
||||
const instance = pino({}, {
|
||||
[Symbol.for('pino.metadata')]: true,
|
||||
write (chunk) {
|
||||
plan.equal(child, this.lastLogger)
|
||||
plan.equal(30, this.lastLevel)
|
||||
plan.equal('a msg', this.lastMsg)
|
||||
plan.deepEqual(this.lastObj, { from: 'child' })
|
||||
const result = JSON.parse(chunk)
|
||||
plan.ok(new Date(result.time) <= new Date(), 'time is greater than Date.now()')
|
||||
delete result.time
|
||||
plan.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 30,
|
||||
hello: 'world',
|
||||
from: 'child',
|
||||
msg: 'a msg'
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
const child = instance.child({ hello: 'world' })
|
||||
child.info({ from: 'child' }, 'a msg')
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('without object', async (t) => {
|
||||
const plan = tspl(t, { plan: 6 })
|
||||
const instance = pino({}, {
|
||||
[Symbol.for('pino.metadata')]: true,
|
||||
write (chunk) {
|
||||
plan.equal(instance, this.lastLogger)
|
||||
plan.equal(30, this.lastLevel)
|
||||
plan.equal('a msg', this.lastMsg)
|
||||
plan.deepEqual({ }, this.lastObj)
|
||||
const result = JSON.parse(chunk)
|
||||
plan.ok(new Date(result.time) <= new Date(), 'time is greater than Date.now()')
|
||||
delete result.time
|
||||
plan.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 30,
|
||||
msg: 'a msg'
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
instance.info('a msg')
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('without msg', async (t) => {
|
||||
const plan = tspl(t, { plan: 6 })
|
||||
const instance = pino({}, {
|
||||
[Symbol.for('pino.metadata')]: true,
|
||||
write (chunk) {
|
||||
plan.equal(instance, this.lastLogger)
|
||||
plan.equal(30, this.lastLevel)
|
||||
plan.equal(undefined, this.lastMsg)
|
||||
plan.deepEqual({ hello: 'world' }, this.lastObj)
|
||||
const result = JSON.parse(chunk)
|
||||
plan.ok(new Date(result.time) <= new Date(), 'time is greater than Date.now()')
|
||||
delete result.time
|
||||
plan.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level: 30,
|
||||
hello: 'world'
|
||||
})
|
||||
}
|
||||
})
|
||||
|
||||
instance.info({ hello: 'world' })
|
||||
|
||||
await plan
|
||||
})
|
||||
+57
@@ -0,0 +1,57 @@
|
||||
'use strict'
|
||||
|
||||
const test = require('node:test')
|
||||
const assert = require('node:assert')
|
||||
|
||||
const { sink, once } = require('./helper')
|
||||
const pino = require('../')
|
||||
|
||||
const level = 50
|
||||
const name = 'error'
|
||||
|
||||
test('default merge strategy', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({
|
||||
base: {},
|
||||
mixin () {
|
||||
return { tag: 'k8s' }
|
||||
}
|
||||
}, stream)
|
||||
instance.level = name
|
||||
instance[name]({
|
||||
tag: 'local'
|
||||
}, 'test')
|
||||
const result = await once(stream, 'data')
|
||||
assert.ok(new Date(result.time) <= new Date(), 'time is greater than Date.now()')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
level,
|
||||
msg: 'test',
|
||||
tag: 'local'
|
||||
})
|
||||
})
|
||||
|
||||
test('custom merge strategy with mixin priority', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({
|
||||
base: {},
|
||||
mixin () {
|
||||
return { tag: 'k8s' }
|
||||
},
|
||||
mixinMergeStrategy (mergeObject, mixinObject) {
|
||||
return Object.assign(mergeObject, mixinObject)
|
||||
}
|
||||
}, stream)
|
||||
instance.level = name
|
||||
instance[name]({
|
||||
tag: 'local'
|
||||
}, 'test')
|
||||
const result = await once(stream, 'data')
|
||||
assert.ok(new Date(result.time) <= new Date(), 'time is greater than Date.now()')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
level,
|
||||
msg: 'test',
|
||||
tag: 'k8s'
|
||||
})
|
||||
})
|
||||
+241
@@ -0,0 +1,241 @@
|
||||
'use strict'
|
||||
|
||||
const test = require('node:test')
|
||||
const assert = require('node:assert')
|
||||
const os = require('node:os')
|
||||
const tspl = require('@matteo.collina/tspl')
|
||||
|
||||
const { sink, once } = require('./helper')
|
||||
const pino = require('../')
|
||||
|
||||
const { pid } = process
|
||||
const hostname = os.hostname()
|
||||
const level = 50
|
||||
const name = 'error'
|
||||
|
||||
test('mixin object is included', async () => {
|
||||
let n = 0
|
||||
const stream = sink()
|
||||
const instance = pino({
|
||||
mixin () {
|
||||
return { hello: ++n }
|
||||
}
|
||||
}, stream)
|
||||
instance.level = name
|
||||
instance[name]('test')
|
||||
const result = await once(stream, 'data')
|
||||
assert.ok(new Date(result.time) <= new Date(), 'time is greater than Date.now()')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level,
|
||||
msg: 'test',
|
||||
hello: 1
|
||||
})
|
||||
})
|
||||
|
||||
test('mixin object is new every time', async (t) => {
|
||||
const plan = tspl(t, { plan: 6 })
|
||||
|
||||
let n = 0
|
||||
const stream = sink()
|
||||
const instance = pino({
|
||||
mixin () {
|
||||
return { hello: n }
|
||||
}
|
||||
}, stream)
|
||||
instance.level = name
|
||||
|
||||
while (++n < 4) {
|
||||
const msg = `test #${n}`
|
||||
stream.pause()
|
||||
instance[name](msg)
|
||||
stream.resume()
|
||||
const result = await once(stream, 'data')
|
||||
plan.ok(new Date(result.time) <= new Date(), 'time is greater than Date.now()')
|
||||
delete result.time
|
||||
plan.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level,
|
||||
msg,
|
||||
hello: n
|
||||
})
|
||||
}
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('mixin object is not called if below log level', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({
|
||||
mixin () {
|
||||
throw Error('should not call mixin function')
|
||||
}
|
||||
}, stream)
|
||||
instance.level = 'error'
|
||||
instance.info('test')
|
||||
})
|
||||
|
||||
test('mixin object + logged object', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({
|
||||
mixin () {
|
||||
return { foo: 1, bar: 2 }
|
||||
}
|
||||
}, stream)
|
||||
instance.level = name
|
||||
instance[name]({ bar: 3, baz: 4 })
|
||||
const result = await once(stream, 'data')
|
||||
assert.ok(new Date(result.time) <= new Date(), 'time is greater than Date.now()')
|
||||
delete result.time
|
||||
assert.deepEqual(result, {
|
||||
pid,
|
||||
hostname,
|
||||
level,
|
||||
foo: 1,
|
||||
bar: 3,
|
||||
baz: 4
|
||||
})
|
||||
})
|
||||
|
||||
test('mixin not a function', async () => {
|
||||
const stream = sink()
|
||||
assert.throws(function () {
|
||||
pino({ mixin: 'not a function' }, stream)
|
||||
})
|
||||
})
|
||||
|
||||
test('mixin can use context', async (t) => {
|
||||
const plan = tspl(t, { plan: 3 })
|
||||
const stream = sink()
|
||||
const instance = pino({
|
||||
mixin (context) {
|
||||
plan.ok(context !== null, 'context should be defined')
|
||||
plan.ok(context !== undefined, 'context should be defined')
|
||||
plan.deepEqual(context, {
|
||||
message: '123',
|
||||
stack: 'stack'
|
||||
})
|
||||
return Object.assign({
|
||||
error: context.message,
|
||||
stack: context.stack
|
||||
})
|
||||
}
|
||||
}, stream)
|
||||
instance.level = name
|
||||
instance[name]({
|
||||
message: '123',
|
||||
stack: 'stack'
|
||||
}, 'test')
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('mixin works without context', async (t) => {
|
||||
const plan = tspl(t, { plan: 3 })
|
||||
const stream = sink()
|
||||
const instance = pino({
|
||||
mixin (context) {
|
||||
plan.ok(context !== null, 'context is still defined w/o passing mergeObject')
|
||||
plan.ok(context !== undefined, 'context is still defined w/o passing mergeObject')
|
||||
plan.deepEqual(context, {})
|
||||
return {
|
||||
something: true
|
||||
}
|
||||
}
|
||||
}, stream)
|
||||
instance.level = name
|
||||
instance[name]('test')
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('mixin can use level number', async (t) => {
|
||||
const plan = tspl(t, { plan: 3 })
|
||||
const stream = sink()
|
||||
const instance = pino({
|
||||
mixin (context, num) {
|
||||
plan.ok(num !== null, 'level should be defined')
|
||||
plan.ok(num !== undefined, 'level should be defined')
|
||||
plan.deepEqual(num, level)
|
||||
return Object.assign({
|
||||
error: context.message,
|
||||
stack: context.stack
|
||||
})
|
||||
}
|
||||
}, stream)
|
||||
instance.level = name
|
||||
instance[name]({
|
||||
message: '123',
|
||||
stack: 'stack'
|
||||
}, 'test')
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('mixin receives logger as third parameter', async (t) => {
|
||||
const plan = tspl(t, { plan: 3 })
|
||||
const stream = sink()
|
||||
const instance = pino({
|
||||
mixin (context, num, logger) {
|
||||
plan.ok(logger !== null, 'logger should be defined')
|
||||
plan.ok(logger !== undefined, 'logger should be defined')
|
||||
plan.deepEqual(logger, instance)
|
||||
return { ...context, num }
|
||||
}
|
||||
}, stream)
|
||||
instance.level = name
|
||||
instance[name]({
|
||||
message: '123'
|
||||
}, 'test')
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('mixin receives child logger', async (t) => {
|
||||
const plan = tspl(t, { plan: 3 })
|
||||
const stream = sink()
|
||||
let child = null
|
||||
const instance = pino({
|
||||
mixin (context, num, logger) {
|
||||
plan.ok(logger !== null, 'logger should be defined')
|
||||
plan.ok(logger !== undefined, 'logger should be defined')
|
||||
plan.deepEqual(logger.expected, child.expected)
|
||||
return { ...context, num }
|
||||
}
|
||||
}, stream)
|
||||
instance.level = name
|
||||
instance.expected = false
|
||||
child = instance.child({})
|
||||
child.expected = true
|
||||
child[name]({
|
||||
message: '123'
|
||||
}, 'test')
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('mixin receives logger even if child exists', async (t) => {
|
||||
const plan = tspl(t, { plan: 3 })
|
||||
const stream = sink()
|
||||
let child = null
|
||||
const instance = pino({
|
||||
mixin (context, num, logger) {
|
||||
plan.ok(logger !== null, 'logger should be defined')
|
||||
plan.ok(logger !== undefined, 'logger should be defined')
|
||||
plan.deepEqual(logger.expected, instance.expected)
|
||||
return { ...context, num }
|
||||
}
|
||||
}, stream)
|
||||
instance.level = name
|
||||
instance.expected = false
|
||||
child = instance.child({})
|
||||
child.expected = true
|
||||
instance[name]({
|
||||
message: '123'
|
||||
}, 'test')
|
||||
|
||||
await plan
|
||||
})
|
||||
+729
@@ -0,0 +1,729 @@
|
||||
'use strict'
|
||||
|
||||
const test = require('node:test')
|
||||
const assert = require('node:assert')
|
||||
const { readFileSync } = require('node:fs')
|
||||
const { join } = require('node:path')
|
||||
const proxyquire = require('proxyquire')
|
||||
const strip = require('strip-ansi')
|
||||
const tspl = require('@matteo.collina/tspl')
|
||||
|
||||
const writeStream = require('flush-write-stream')
|
||||
const pino = require('../')
|
||||
const multistream = pino.multistream
|
||||
const { file, sink } = require('./helper')
|
||||
|
||||
test('sends to multiple streams using string levels', async () => {
|
||||
let messageCount = 0
|
||||
const stream = writeStream(function (data, enc, cb) {
|
||||
messageCount += 1
|
||||
cb()
|
||||
})
|
||||
const streams = [
|
||||
{ stream },
|
||||
{ level: 'debug', stream },
|
||||
{ level: 'trace', stream },
|
||||
{ level: 'fatal', stream },
|
||||
{ level: 'silent', stream }
|
||||
]
|
||||
const log = pino({
|
||||
level: 'trace'
|
||||
}, multistream(streams))
|
||||
log.info('info stream')
|
||||
log.debug('debug stream')
|
||||
log.fatal('fatal stream')
|
||||
assert.equal(messageCount, 9)
|
||||
})
|
||||
|
||||
test('sends to multiple streams using custom levels', async () => {
|
||||
let messageCount = 0
|
||||
const stream = writeStream(function (data, enc, cb) {
|
||||
messageCount += 1
|
||||
cb()
|
||||
})
|
||||
const streams = [
|
||||
{ stream },
|
||||
{ level: 'debug', stream },
|
||||
{ level: 'trace', stream },
|
||||
{ level: 'fatal', stream },
|
||||
{ level: 'silent', stream }
|
||||
]
|
||||
const log = pino({
|
||||
level: 'trace'
|
||||
}, multistream(streams))
|
||||
log.info('info stream')
|
||||
log.debug('debug stream')
|
||||
log.fatal('fatal stream')
|
||||
assert.equal(messageCount, 9)
|
||||
})
|
||||
|
||||
test('sends to multiple streams using optionally predefined levels', async () => {
|
||||
let messageCount = 0
|
||||
const stream = writeStream(function (data, enc, cb) {
|
||||
messageCount += 1
|
||||
cb()
|
||||
})
|
||||
const opts = {
|
||||
levels: {
|
||||
silent: Infinity,
|
||||
fatal: 60,
|
||||
error: 50,
|
||||
warn: 50,
|
||||
info: 30,
|
||||
debug: 20,
|
||||
trace: 10
|
||||
}
|
||||
}
|
||||
const streams = [
|
||||
{ stream },
|
||||
{ level: 'trace', stream },
|
||||
{ level: 'debug', stream },
|
||||
{ level: 'info', stream },
|
||||
{ level: 'warn', stream },
|
||||
{ level: 'error', stream },
|
||||
{ level: 'fatal', stream },
|
||||
{ level: 'silent', stream }
|
||||
]
|
||||
const mstream = multistream(streams, opts)
|
||||
const log = pino({
|
||||
level: 'trace'
|
||||
}, mstream)
|
||||
log.trace('trace stream')
|
||||
log.debug('debug stream')
|
||||
log.info('info stream')
|
||||
log.warn('warn stream')
|
||||
log.error('error stream')
|
||||
log.fatal('fatal stream')
|
||||
log.silent('silent stream')
|
||||
assert.equal(messageCount, 24)
|
||||
})
|
||||
|
||||
test('sends to multiple streams using number levels', async () => {
|
||||
let messageCount = 0
|
||||
const stream = writeStream(function (data, enc, cb) {
|
||||
messageCount += 1
|
||||
cb()
|
||||
})
|
||||
const streams = [
|
||||
{ stream },
|
||||
{ level: 20, stream },
|
||||
{ level: 60, stream }
|
||||
]
|
||||
const log = pino({
|
||||
level: 'debug'
|
||||
}, multistream(streams))
|
||||
log.info('info stream')
|
||||
log.debug('debug stream')
|
||||
log.fatal('fatal stream')
|
||||
assert.equal(messageCount, 6)
|
||||
})
|
||||
|
||||
test('level include higher levels', async () => {
|
||||
let messageCount = 0
|
||||
const stream = writeStream(function (data, enc, cb) {
|
||||
messageCount += 1
|
||||
cb()
|
||||
})
|
||||
const log = pino({}, multistream([{ level: 'info', stream }]))
|
||||
log.fatal('message')
|
||||
assert.equal(messageCount, 1)
|
||||
})
|
||||
|
||||
test('supports multiple arguments', async (t) => {
|
||||
const plan = tspl(t, { plan: 2 })
|
||||
const messages = []
|
||||
const stream = writeStream(function (data, enc, cb) {
|
||||
messages.push(JSON.parse(data))
|
||||
if (messages.length === 2) {
|
||||
const msg1 = messages[0]
|
||||
plan.equal(msg1.msg, 'foo bar baz foobar')
|
||||
|
||||
const msg2 = messages[1]
|
||||
plan.equal(msg2.msg, 'foo bar baz foobar barfoo foofoo')
|
||||
}
|
||||
cb()
|
||||
})
|
||||
const log = pino({}, multistream({ stream }))
|
||||
log.info('%s %s %s %s', 'foo', 'bar', 'baz', 'foobar') // apply not invoked
|
||||
log.info('%s %s %s %s %s %s', 'foo', 'bar', 'baz', 'foobar', 'barfoo', 'foofoo') // apply invoked
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('supports children', async (t) => {
|
||||
const plan = tspl(t, { plan: 2 })
|
||||
const stream = writeStream(function (data, enc, cb) {
|
||||
const input = JSON.parse(data)
|
||||
plan.equal(input.msg, 'child stream')
|
||||
plan.equal(input.child, 'one')
|
||||
cb()
|
||||
})
|
||||
const streams = [
|
||||
{ stream }
|
||||
]
|
||||
const log = pino({}, multistream(streams)).child({ child: 'one' })
|
||||
log.info('child stream')
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('supports grandchildren', async (t) => {
|
||||
const plan = tspl(t, { plan: 9 })
|
||||
const messages = []
|
||||
const stream = writeStream(function (data, enc, cb) {
|
||||
messages.push(JSON.parse(data))
|
||||
if (messages.length === 3) {
|
||||
const msg1 = messages[0]
|
||||
plan.equal(msg1.msg, 'grandchild stream')
|
||||
plan.equal(msg1.child, 'one')
|
||||
plan.equal(msg1.grandchild, 'two')
|
||||
|
||||
const msg2 = messages[1]
|
||||
plan.equal(msg2.msg, 'grandchild stream')
|
||||
plan.equal(msg2.child, 'one')
|
||||
plan.equal(msg2.grandchild, 'two')
|
||||
|
||||
const msg3 = messages[2]
|
||||
plan.equal(msg3.msg, 'debug grandchild')
|
||||
plan.equal(msg3.child, 'one')
|
||||
plan.equal(msg3.grandchild, 'two')
|
||||
}
|
||||
cb()
|
||||
})
|
||||
const streams = [
|
||||
{ stream },
|
||||
{ level: 'debug', stream }
|
||||
]
|
||||
const log = pino({
|
||||
level: 'debug'
|
||||
}, multistream(streams)).child({ child: 'one' }).child({ grandchild: 'two' })
|
||||
log.info('grandchild stream')
|
||||
log.debug('debug grandchild')
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('supports custom levels', (t, end) => {
|
||||
const stream = writeStream(function (data, enc, cb) {
|
||||
assert.equal(JSON.parse(data).msg, 'bar')
|
||||
end()
|
||||
})
|
||||
const log = pino({
|
||||
customLevels: {
|
||||
foo: 35
|
||||
}
|
||||
}, multistream([{ level: 35, stream }]))
|
||||
log.foo('bar')
|
||||
})
|
||||
|
||||
test('supports pretty print', async (t) => {
|
||||
const plan = tspl(t, { plan: 2 })
|
||||
const stream = writeStream(function (data, enc, cb) {
|
||||
plan.equal(strip(data.toString()).match(/INFO.*: pretty print/) != null, true)
|
||||
cb()
|
||||
})
|
||||
|
||||
const safeBoom = proxyquire('pino-pretty/lib/utils/build-safe-sonic-boom.js', {
|
||||
'sonic-boom': function () {
|
||||
plan.ok('sonic created')
|
||||
stream.flushSync = () => {}
|
||||
stream.flush = () => {}
|
||||
return stream
|
||||
}
|
||||
})
|
||||
const nested = proxyquire('pino-pretty/lib/utils/index.js', {
|
||||
'./build-safe-sonic-boom.js': safeBoom
|
||||
})
|
||||
const pretty = proxyquire('pino-pretty', {
|
||||
'./lib/utils/index.js': nested
|
||||
})
|
||||
|
||||
const log = pino({
|
||||
level: 'debug',
|
||||
name: 'helloName'
|
||||
}, multistream([
|
||||
{ stream: pretty() }
|
||||
]))
|
||||
|
||||
log.info('pretty print')
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('emit propagates events to each stream', async (t) => {
|
||||
const plan = tspl(t, { plan: 3 })
|
||||
const handler = function (data) {
|
||||
plan.equal(data.msg, 'world')
|
||||
}
|
||||
const streams = [sink(), sink(), sink()]
|
||||
streams.forEach(function (s) {
|
||||
s.once('hello', handler)
|
||||
})
|
||||
const stream = multistream(streams)
|
||||
stream.emit('hello', { msg: 'world' })
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('children support custom levels', async (t) => {
|
||||
const plan = tspl(t, { plan: 1 })
|
||||
const stream = writeStream(function (data, enc, cb) {
|
||||
plan.equal(JSON.parse(data).msg, 'bar')
|
||||
})
|
||||
const parent = pino({
|
||||
customLevels: {
|
||||
foo: 35
|
||||
}
|
||||
}, multistream([{ level: 35, stream }]))
|
||||
const child = parent.child({ child: 'yes' })
|
||||
child.foo('bar')
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('levelVal overrides level', async () => {
|
||||
let messageCount = 0
|
||||
const stream = writeStream(function (data, enc, cb) {
|
||||
messageCount += 1
|
||||
cb()
|
||||
})
|
||||
const streams = [
|
||||
{ stream },
|
||||
{ level: 'blabla', levelVal: 15, stream },
|
||||
{ level: 60, stream }
|
||||
]
|
||||
const log = pino({
|
||||
level: 'debug'
|
||||
}, multistream(streams))
|
||||
log.info('info stream')
|
||||
log.debug('debug stream')
|
||||
log.fatal('fatal stream')
|
||||
assert.equal(messageCount, 6)
|
||||
})
|
||||
|
||||
test('forwards metadata', async (t) => {
|
||||
const plan = tspl(t, { plan: 4 })
|
||||
const streams = [
|
||||
{
|
||||
stream: {
|
||||
[Symbol.for('pino.metadata')]: true,
|
||||
write (chunk) {
|
||||
plan.equal(log, this.lastLogger)
|
||||
plan.equal(30, this.lastLevel)
|
||||
plan.deepEqual({ hello: 'world' }, this.lastObj)
|
||||
plan.deepEqual('a msg', this.lastMsg)
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
const log = pino({
|
||||
level: 'debug'
|
||||
}, multistream(streams))
|
||||
|
||||
log.info({ hello: 'world' }, 'a msg')
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('forward name', async (t) => {
|
||||
const plan = tspl(t, { plan: 2 })
|
||||
const streams = [
|
||||
{
|
||||
stream: {
|
||||
[Symbol.for('pino.metadata')]: true,
|
||||
write (chunk) {
|
||||
const line = JSON.parse(chunk)
|
||||
plan.equal(line.name, 'helloName')
|
||||
plan.equal(line.hello, 'world')
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
const log = pino({
|
||||
level: 'debug',
|
||||
name: 'helloName'
|
||||
}, multistream(streams))
|
||||
|
||||
log.info({ hello: 'world' }, 'a msg')
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('forward name with child', async (t) => {
|
||||
const plan = tspl(t, { plan: 3 })
|
||||
const streams = [
|
||||
{
|
||||
stream: {
|
||||
write (chunk) {
|
||||
const line = JSON.parse(chunk)
|
||||
plan.equal(line.name, 'helloName')
|
||||
plan.equal(line.hello, 'world')
|
||||
plan.equal(line.component, 'aComponent')
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
|
||||
const log = pino({
|
||||
level: 'debug',
|
||||
name: 'helloName'
|
||||
}, multistream(streams)).child({ component: 'aComponent' })
|
||||
|
||||
log.info({ hello: 'world' }, 'a msg')
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('clone generates a new multistream with all stream at the same level', async (t) => {
|
||||
const plan = tspl(t, { plan: 14 })
|
||||
let messageCount = 0
|
||||
const stream = writeStream(function (data, enc, cb) {
|
||||
messageCount += 1
|
||||
cb()
|
||||
})
|
||||
const streams = [
|
||||
{ stream },
|
||||
{ level: 'debug', stream },
|
||||
{ level: 'trace', stream },
|
||||
{ level: 'fatal', stream }
|
||||
]
|
||||
const ms = multistream(streams)
|
||||
const clone = ms.clone(30)
|
||||
|
||||
// eslint-disable-next-line eqeqeq
|
||||
plan.equal(clone != ms, true)
|
||||
|
||||
clone.streams.forEach((s, i) => {
|
||||
// eslint-disable-next-line eqeqeq
|
||||
plan.equal(s != streams[i], true)
|
||||
plan.equal(s.stream, streams[i].stream)
|
||||
plan.equal(s.level, 30)
|
||||
})
|
||||
|
||||
const log = pino({
|
||||
level: 'trace'
|
||||
}, clone)
|
||||
|
||||
log.info('info stream')
|
||||
log.debug('debug message not counted')
|
||||
log.fatal('fatal stream')
|
||||
plan.equal(messageCount, 8)
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('one stream', async () => {
|
||||
let messageCount = 0
|
||||
const stream = writeStream(function (data, enc, cb) {
|
||||
messageCount += 1
|
||||
cb()
|
||||
})
|
||||
const log = pino({
|
||||
level: 'trace'
|
||||
}, multistream({ stream, level: 'fatal' }))
|
||||
log.info('info stream')
|
||||
log.debug('debug stream')
|
||||
log.fatal('fatal stream')
|
||||
assert.equal(messageCount, 1)
|
||||
})
|
||||
|
||||
test('dedupe', async () => {
|
||||
let messageCount = 0
|
||||
const stream1 = writeStream(function (data, enc, cb) {
|
||||
messageCount -= 1
|
||||
cb()
|
||||
})
|
||||
|
||||
const stream2 = writeStream(function (data, enc, cb) {
|
||||
messageCount += 1
|
||||
cb()
|
||||
})
|
||||
|
||||
const streams = [
|
||||
{
|
||||
stream: stream1,
|
||||
level: 'info'
|
||||
},
|
||||
{
|
||||
stream: stream2,
|
||||
level: 'fatal'
|
||||
}
|
||||
]
|
||||
|
||||
const log = pino({
|
||||
level: 'trace'
|
||||
}, multistream(streams, { dedupe: true }))
|
||||
log.info('info stream')
|
||||
log.fatal('fatal stream')
|
||||
log.fatal('fatal stream')
|
||||
assert.equal(messageCount, 1)
|
||||
})
|
||||
|
||||
test('dedupe when logs have different levels', async () => {
|
||||
let messageCount = 0
|
||||
const stream1 = writeStream(function (data, enc, cb) {
|
||||
messageCount += 1
|
||||
cb()
|
||||
})
|
||||
|
||||
const stream2 = writeStream(function (data, enc, cb) {
|
||||
messageCount += 2
|
||||
cb()
|
||||
})
|
||||
|
||||
const streams = [
|
||||
{
|
||||
stream: stream1,
|
||||
level: 'info'
|
||||
},
|
||||
{
|
||||
stream: stream2,
|
||||
level: 'error'
|
||||
}
|
||||
]
|
||||
|
||||
const log = pino({
|
||||
level: 'trace'
|
||||
}, multistream(streams, { dedupe: true }))
|
||||
|
||||
log.info('info stream')
|
||||
log.warn('warn stream')
|
||||
log.error('error streams')
|
||||
log.fatal('fatal streams')
|
||||
assert.equal(messageCount, 6)
|
||||
})
|
||||
|
||||
test('dedupe when some streams has the same level', async () => {
|
||||
let messageCount = 0
|
||||
const stream1 = writeStream(function (data, enc, cb) {
|
||||
messageCount -= 1
|
||||
cb()
|
||||
})
|
||||
|
||||
const stream2 = writeStream(function (data, enc, cb) {
|
||||
messageCount += 1
|
||||
cb()
|
||||
})
|
||||
|
||||
const stream3 = writeStream(function (data, enc, cb) {
|
||||
messageCount += 1
|
||||
cb()
|
||||
})
|
||||
|
||||
const streams = [
|
||||
{
|
||||
stream: stream1,
|
||||
level: 'info'
|
||||
},
|
||||
{
|
||||
stream: stream2,
|
||||
level: 'fatal'
|
||||
},
|
||||
{
|
||||
stream: stream3,
|
||||
level: 'fatal'
|
||||
}
|
||||
]
|
||||
|
||||
const log = pino({
|
||||
level: 'trace'
|
||||
}, multistream(streams, { dedupe: true }))
|
||||
log.info('info stream')
|
||||
log.fatal('fatal streams')
|
||||
log.fatal('fatal streams')
|
||||
assert.equal(messageCount, 3)
|
||||
})
|
||||
|
||||
test('no stream', async () => {
|
||||
const log = pino({
|
||||
level: 'trace'
|
||||
}, multistream())
|
||||
log.info('info stream')
|
||||
log.debug('debug stream')
|
||||
log.fatal('fatal stream')
|
||||
})
|
||||
|
||||
test('one stream', async () => {
|
||||
let messageCount = 0
|
||||
const stream = writeStream(function (data, enc, cb) {
|
||||
messageCount += 1
|
||||
cb()
|
||||
})
|
||||
const log = pino({
|
||||
level: 'trace'
|
||||
}, multistream(stream))
|
||||
log.info('info stream')
|
||||
log.debug('debug stream')
|
||||
log.fatal('fatal stream')
|
||||
assert.equal(messageCount, 2)
|
||||
})
|
||||
|
||||
test('add a stream', async () => {
|
||||
let messageCount = 0
|
||||
const stream = writeStream(function (data, enc, cb) {
|
||||
messageCount += 1
|
||||
cb()
|
||||
})
|
||||
|
||||
const log = pino({
|
||||
level: 'trace'
|
||||
}, multistream().add(stream))
|
||||
log.info('info stream')
|
||||
log.debug('debug stream')
|
||||
log.fatal('fatal stream')
|
||||
assert.equal(messageCount, 2)
|
||||
})
|
||||
|
||||
test('remove a stream', async () => {
|
||||
let messageCount1 = 0
|
||||
let messageCount2 = 0
|
||||
let messageCount3 = 0
|
||||
|
||||
const stream1 = writeStream(function (data, enc, cb) {
|
||||
messageCount1 += 1
|
||||
cb()
|
||||
})
|
||||
|
||||
const stream2 = writeStream(function (data, enc, cb) {
|
||||
messageCount2 += 1
|
||||
cb()
|
||||
})
|
||||
|
||||
const stream3 = writeStream(function (data, enc, cb) {
|
||||
messageCount3 += 1
|
||||
cb()
|
||||
})
|
||||
|
||||
const multi = multistream()
|
||||
const log = pino({ level: 'trace', sync: true }, multi)
|
||||
|
||||
multi.add(stream1)
|
||||
const id1 = multi.lastId
|
||||
|
||||
multi.add(stream2)
|
||||
const id2 = multi.lastId
|
||||
|
||||
multi.add(stream3)
|
||||
const id3 = multi.lastId
|
||||
|
||||
log.info('line')
|
||||
multi.remove(id1)
|
||||
|
||||
log.info('line')
|
||||
multi.remove(id2)
|
||||
|
||||
log.info('line')
|
||||
multi.remove(id3)
|
||||
|
||||
log.info('line')
|
||||
multi.remove(Math.floor(Math.random() * 1000)) // non-existing id
|
||||
|
||||
assert.equal(messageCount1, 1)
|
||||
assert.equal(messageCount2, 2)
|
||||
assert.equal(messageCount3, 3)
|
||||
})
|
||||
|
||||
test('multistream.add throws if not a stream', async () => {
|
||||
try {
|
||||
pino({
|
||||
level: 'trace'
|
||||
}, multistream().add({}))
|
||||
} catch (_) {
|
||||
}
|
||||
})
|
||||
|
||||
test('multistream throws if not a stream', async () => {
|
||||
try {
|
||||
pino({
|
||||
level: 'trace'
|
||||
}, multistream({}))
|
||||
} catch (_) {
|
||||
}
|
||||
})
|
||||
|
||||
test('multistream.write should not throw if one stream fails', async () => {
|
||||
let messageCount = 0
|
||||
const stream = writeStream(function (data, enc, cb) {
|
||||
messageCount += 1
|
||||
cb()
|
||||
})
|
||||
const noopStream = pino.transport({
|
||||
target: join(__dirname, 'fixtures', 'noop-transport.js')
|
||||
})
|
||||
// eslint-disable-next-line
|
||||
noopStream.on('error', function (err) {
|
||||
// something went wrong while writing to noop stream, ignoring!
|
||||
})
|
||||
const log = pino({
|
||||
level: 'trace'
|
||||
},
|
||||
multistream([
|
||||
{
|
||||
level: 'trace',
|
||||
stream
|
||||
},
|
||||
{
|
||||
level: 'debug',
|
||||
stream: noopStream
|
||||
}
|
||||
])
|
||||
)
|
||||
log.debug('0')
|
||||
noopStream.end()
|
||||
// noop stream is ending, should emit an error but not throw
|
||||
log.debug('1')
|
||||
log.debug('2')
|
||||
assert.equal(messageCount, 3)
|
||||
})
|
||||
|
||||
test('flushSync', async (t) => {
|
||||
const plan = tspl(t, { plan: 2 })
|
||||
const tmp = file()
|
||||
const destination = pino.destination({ dest: tmp, sync: false, minLength: 4096 })
|
||||
const stream = multistream([{ level: 'info', stream: destination }])
|
||||
const log = pino({ level: 'info' }, stream)
|
||||
destination.on('ready', () => {
|
||||
log.info('foo')
|
||||
log.info('bar')
|
||||
stream.flushSync()
|
||||
plan.equal(readFileSync(tmp, { encoding: 'utf-8' }).split('\n').length - 1, 2)
|
||||
log.info('biz')
|
||||
stream.flushSync()
|
||||
plan.equal(readFileSync(tmp, { encoding: 'utf-8' }).split('\n').length - 1, 3)
|
||||
})
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('ends all streams', async (t) => {
|
||||
const plan = tspl(t, { plan: 7 })
|
||||
const stream = writeStream(function (data, enc, cb) {
|
||||
plan.ok('message')
|
||||
cb()
|
||||
})
|
||||
stream.flushSync = function () {
|
||||
plan.ok('flushSync')
|
||||
}
|
||||
// stream2 has no flushSync
|
||||
const stream2 = writeStream(function (data, enc, cb) {
|
||||
plan.ok('message2')
|
||||
cb()
|
||||
})
|
||||
const streams = [
|
||||
{ stream },
|
||||
{ level: 'debug', stream },
|
||||
{ level: 'trace', stream: stream2 },
|
||||
{ level: 'fatal', stream },
|
||||
{ level: 'silent', stream }
|
||||
]
|
||||
const multi = multistream(streams)
|
||||
const log = pino({
|
||||
level: 'trace'
|
||||
}, multi)
|
||||
log.info('info stream')
|
||||
multi.end()
|
||||
|
||||
await plan
|
||||
})
|
||||
+893
@@ -0,0 +1,893 @@
|
||||
'use strict'
|
||||
|
||||
const test = require('node:test')
|
||||
const assert = require('node:assert')
|
||||
|
||||
const { sink, once } = require('./helper')
|
||||
const pino = require('../')
|
||||
|
||||
test('redact option – throws if not array', async () => {
|
||||
assert.throws(() => {
|
||||
pino({ redact: 'req.headers.cookie' })
|
||||
})
|
||||
})
|
||||
|
||||
test('redact option – throws if array does not only contain strings', async () => {
|
||||
assert.throws(() => {
|
||||
pino({ redact: ['req.headers.cookie', {}] })
|
||||
})
|
||||
})
|
||||
|
||||
test('redact option – throws if array contains an invalid path', async () => {
|
||||
assert.throws(() => {
|
||||
pino({ redact: ['req,headers.cookie'] })
|
||||
})
|
||||
})
|
||||
|
||||
test('redact.paths option – throws if not array', async () => {
|
||||
assert.throws(() => {
|
||||
pino({ redact: { paths: 'req.headers.cookie' } })
|
||||
})
|
||||
})
|
||||
|
||||
test('redact.paths option – throws if array does not only contain strings', async () => {
|
||||
assert.throws(() => {
|
||||
pino({ redact: { paths: ['req.headers.cookie', {}] } })
|
||||
})
|
||||
})
|
||||
|
||||
test('redact.paths option – throws if array contains an invalid path', async () => {
|
||||
assert.throws(() => {
|
||||
pino({ redact: { paths: ['req,headers.cookie'] } })
|
||||
})
|
||||
})
|
||||
|
||||
test('redact option – top level key', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: ['key'] }, stream)
|
||||
instance.info({
|
||||
key: { redact: 'me' }
|
||||
})
|
||||
const { key } = await once(stream, 'data')
|
||||
assert.equal(key, '[Redacted]')
|
||||
})
|
||||
|
||||
test('redact option – top level key next level key', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: ['key', 'key.foo'] }, stream)
|
||||
instance.info({
|
||||
key: { redact: 'me' }
|
||||
})
|
||||
const { key } = await once(stream, 'data')
|
||||
assert.equal(key, '[Redacted]')
|
||||
})
|
||||
|
||||
test('redact option – next level key then top level key', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: ['key.foo', 'key'] }, stream)
|
||||
instance.info({
|
||||
key: { redact: 'me' }
|
||||
})
|
||||
const { key } = await once(stream, 'data')
|
||||
assert.equal(key, '[Redacted]')
|
||||
})
|
||||
|
||||
test('redact option – object', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: ['req.headers.cookie'] }, stream)
|
||||
instance.info({
|
||||
req: {
|
||||
id: 7915,
|
||||
method: 'GET',
|
||||
url: '/',
|
||||
headers: {
|
||||
host: 'localhost:3000',
|
||||
connection: 'keep-alive',
|
||||
cookie: 'SESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;'
|
||||
},
|
||||
remoteAddress: '::ffff:127.0.0.1',
|
||||
remotePort: 58022
|
||||
}
|
||||
})
|
||||
const { req } = await once(stream, 'data')
|
||||
assert.equal(req.headers.cookie, '[Redacted]')
|
||||
})
|
||||
|
||||
test('redact option – child object', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: ['req.headers.cookie'] }, stream)
|
||||
instance.child({
|
||||
req: {
|
||||
id: 7915,
|
||||
method: 'GET',
|
||||
url: '/',
|
||||
headers: {
|
||||
host: 'localhost:3000',
|
||||
connection: 'keep-alive',
|
||||
cookie: 'SESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;'
|
||||
},
|
||||
remoteAddress: '::ffff:127.0.0.1',
|
||||
remotePort: 58022
|
||||
}
|
||||
}).info('message completed')
|
||||
const { req } = await once(stream, 'data')
|
||||
assert.equal(req.headers.cookie, '[Redacted]')
|
||||
})
|
||||
|
||||
test('redact option – interpolated object', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: ['req.headers.cookie'] }, stream)
|
||||
|
||||
instance.info('test %j', {
|
||||
req: {
|
||||
id: 7915,
|
||||
method: 'GET',
|
||||
url: '/',
|
||||
headers: {
|
||||
host: 'localhost:3000',
|
||||
connection: 'keep-alive',
|
||||
cookie: 'SESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;'
|
||||
},
|
||||
remoteAddress: '::ffff:127.0.0.1',
|
||||
remotePort: 58022
|
||||
}
|
||||
})
|
||||
const { msg } = await once(stream, 'data')
|
||||
assert.equal(JSON.parse(msg.replace(/test /, '')).req.headers.cookie, '[Redacted]')
|
||||
})
|
||||
|
||||
test('redact.paths option – object', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: { paths: ['req.headers.cookie'] } }, stream)
|
||||
instance.info({
|
||||
req: {
|
||||
id: 7915,
|
||||
method: 'GET',
|
||||
url: '/',
|
||||
headers: {
|
||||
host: 'localhost:3000',
|
||||
connection: 'keep-alive',
|
||||
cookie: 'SESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;'
|
||||
},
|
||||
remoteAddress: '::ffff:127.0.0.1',
|
||||
remotePort: 58022
|
||||
}
|
||||
})
|
||||
const { req } = await once(stream, 'data')
|
||||
assert.equal(req.headers.cookie, '[Redacted]')
|
||||
})
|
||||
|
||||
test('redact.paths option – child object', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: { paths: ['req.headers.cookie'] } }, stream)
|
||||
instance.child({
|
||||
req: {
|
||||
id: 7915,
|
||||
method: 'GET',
|
||||
url: '/',
|
||||
headers: {
|
||||
host: 'localhost:3000',
|
||||
connection: 'keep-alive',
|
||||
cookie: 'SESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;'
|
||||
},
|
||||
remoteAddress: '::ffff:127.0.0.1',
|
||||
remotePort: 58022
|
||||
}
|
||||
}).info('message completed')
|
||||
const { req } = await once(stream, 'data')
|
||||
assert.equal(req.headers.cookie, '[Redacted]')
|
||||
})
|
||||
|
||||
test('redact.paths option – interpolated object', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: { paths: ['req.headers.cookie'] } }, stream)
|
||||
|
||||
instance.info('test %j', {
|
||||
req: {
|
||||
id: 7915,
|
||||
method: 'GET',
|
||||
url: '/',
|
||||
headers: {
|
||||
host: 'localhost:3000',
|
||||
connection: 'keep-alive',
|
||||
cookie: 'SESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;'
|
||||
},
|
||||
remoteAddress: '::ffff:127.0.0.1',
|
||||
remotePort: 58022
|
||||
}
|
||||
})
|
||||
const { msg } = await once(stream, 'data')
|
||||
assert.equal(JSON.parse(msg.replace(/test /, '')).req.headers.cookie, '[Redacted]')
|
||||
})
|
||||
|
||||
test('redact.censor option – sets the redact value', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: { paths: ['req.headers.cookie'], censor: 'test' } }, stream)
|
||||
instance.info({
|
||||
req: {
|
||||
id: 7915,
|
||||
method: 'GET',
|
||||
url: '/',
|
||||
headers: {
|
||||
host: 'localhost:3000',
|
||||
connection: 'keep-alive',
|
||||
cookie: 'SESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;'
|
||||
},
|
||||
remoteAddress: '::ffff:127.0.0.1',
|
||||
remotePort: 58022
|
||||
}
|
||||
})
|
||||
const { req } = await once(stream, 'data')
|
||||
assert.equal(req.headers.cookie, 'test')
|
||||
})
|
||||
|
||||
test('redact.censor option – can be a function that accepts value and path arguments', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: { paths: ['topLevel'], censor: (value, path) => value + ' ' + path.join('.') } }, stream)
|
||||
instance.info({
|
||||
topLevel: 'test'
|
||||
})
|
||||
const { topLevel } = await once(stream, 'data')
|
||||
assert.equal(topLevel, 'test topLevel')
|
||||
})
|
||||
|
||||
test('redact.censor option – can be a function that accepts value and path arguments (nested path)', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: { paths: ['req.headers.cookie'], censor: (value, path) => value + ' ' + path.join('.') } }, stream)
|
||||
instance.info({
|
||||
req: {
|
||||
id: 7915,
|
||||
method: 'GET',
|
||||
url: '/',
|
||||
headers: {
|
||||
host: 'localhost:3000',
|
||||
connection: 'keep-alive',
|
||||
cookie: 'SESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;'
|
||||
},
|
||||
remoteAddress: '::ffff:127.0.0.1',
|
||||
remotePort: 58022
|
||||
}
|
||||
})
|
||||
const { req } = await once(stream, 'data')
|
||||
assert.equal(req.headers.cookie, 'SESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1; req.headers.cookie')
|
||||
})
|
||||
|
||||
test('redact.remove option – removes both key and value', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: { paths: ['req.headers.cookie'], remove: true } }, stream)
|
||||
instance.info({
|
||||
req: {
|
||||
id: 7915,
|
||||
method: 'GET',
|
||||
url: '/',
|
||||
headers: {
|
||||
host: 'localhost:3000',
|
||||
connection: 'keep-alive',
|
||||
cookie: 'SESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;'
|
||||
},
|
||||
remoteAddress: '::ffff:127.0.0.1',
|
||||
remotePort: 58022
|
||||
}
|
||||
})
|
||||
const { req } = await once(stream, 'data')
|
||||
assert.equal('cookie' in req.headers, false)
|
||||
})
|
||||
|
||||
test('redact.remove – top level key - object value', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: { paths: ['key'], remove: true } }, stream)
|
||||
instance.info({
|
||||
key: { redact: 'me' }
|
||||
})
|
||||
const o = await once(stream, 'data')
|
||||
assert.equal('key' in o, false)
|
||||
})
|
||||
|
||||
test('redact.remove – top level key - number value', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: { paths: ['key'], remove: true } }, stream)
|
||||
instance.info({
|
||||
key: 1
|
||||
})
|
||||
const o = await once(stream, 'data')
|
||||
assert.equal('key' in o, false)
|
||||
})
|
||||
|
||||
test('redact.remove – top level key - boolean value', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: { paths: ['key'], remove: true } }, stream)
|
||||
instance.info({
|
||||
key: false
|
||||
})
|
||||
const o = await once(stream, 'data')
|
||||
assert.equal('key' in o, false)
|
||||
})
|
||||
|
||||
test('redact.remove – top level key in child logger', async () => {
|
||||
const stream = sink()
|
||||
const opts = { redact: { paths: ['key'], remove: true } }
|
||||
const instance = pino(opts, stream).child({ key: { redact: 'me' } })
|
||||
instance.info('test')
|
||||
const o = await once(stream, 'data')
|
||||
assert.equal('key' in o, false)
|
||||
})
|
||||
|
||||
test('redact.paths preserves original object values after the log write', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: ['req.headers.cookie'] }, stream)
|
||||
const obj = {
|
||||
req: {
|
||||
id: 7915,
|
||||
method: 'GET',
|
||||
url: '/',
|
||||
headers: {
|
||||
host: 'localhost:3000',
|
||||
connection: 'keep-alive',
|
||||
cookie: 'SESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;'
|
||||
},
|
||||
remoteAddress: '::ffff:127.0.0.1',
|
||||
remotePort: 58022
|
||||
}
|
||||
}
|
||||
instance.info(obj)
|
||||
const o = await once(stream, 'data')
|
||||
assert.equal(o.req.headers.cookie, '[Redacted]')
|
||||
assert.equal(obj.req.headers.cookie, 'SESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;')
|
||||
})
|
||||
|
||||
test('redact.paths preserves original object values after the log write', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: { paths: ['req.headers.cookie'] } }, stream)
|
||||
const obj = {
|
||||
req: {
|
||||
id: 7915,
|
||||
method: 'GET',
|
||||
url: '/',
|
||||
headers: {
|
||||
host: 'localhost:3000',
|
||||
connection: 'keep-alive',
|
||||
cookie: 'SESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;'
|
||||
},
|
||||
remoteAddress: '::ffff:127.0.0.1',
|
||||
remotePort: 58022
|
||||
}
|
||||
}
|
||||
instance.info(obj)
|
||||
const o = await once(stream, 'data')
|
||||
assert.equal(o.req.headers.cookie, '[Redacted]')
|
||||
assert.equal(obj.req.headers.cookie, 'SESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;')
|
||||
})
|
||||
|
||||
test('redact.censor preserves original object values after the log write', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: { paths: ['req.headers.cookie'], censor: 'test' } }, stream)
|
||||
const obj = {
|
||||
req: {
|
||||
id: 7915,
|
||||
method: 'GET',
|
||||
url: '/',
|
||||
headers: {
|
||||
host: 'localhost:3000',
|
||||
connection: 'keep-alive',
|
||||
cookie: 'SESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;'
|
||||
},
|
||||
remoteAddress: '::ffff:127.0.0.1',
|
||||
remotePort: 58022
|
||||
}
|
||||
}
|
||||
instance.info(obj)
|
||||
const o = await once(stream, 'data')
|
||||
assert.equal(o.req.headers.cookie, 'test')
|
||||
assert.equal(obj.req.headers.cookie, 'SESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;')
|
||||
})
|
||||
|
||||
test('redact.remove preserves original object values after the log write', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: { paths: ['req.headers.cookie'], remove: true } }, stream)
|
||||
const obj = {
|
||||
req: {
|
||||
id: 7915,
|
||||
method: 'GET',
|
||||
url: '/',
|
||||
headers: {
|
||||
host: 'localhost:3000',
|
||||
connection: 'keep-alive',
|
||||
cookie: 'SESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;'
|
||||
},
|
||||
remoteAddress: '::ffff:127.0.0.1',
|
||||
remotePort: 58022
|
||||
}
|
||||
}
|
||||
instance.info(obj)
|
||||
const o = await once(stream, 'data')
|
||||
assert.equal('cookie' in o.req.headers, false)
|
||||
assert.equal('cookie' in obj.req.headers, true)
|
||||
})
|
||||
|
||||
test('redact – supports last position wildcard paths', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: ['req.headers.*'] }, stream)
|
||||
instance.info({
|
||||
req: {
|
||||
id: 7915,
|
||||
method: 'GET',
|
||||
url: '/',
|
||||
headers: {
|
||||
host: 'localhost:3000',
|
||||
connection: 'keep-alive',
|
||||
cookie: 'SESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;'
|
||||
},
|
||||
remoteAddress: '::ffff:127.0.0.1',
|
||||
remotePort: 58022
|
||||
}
|
||||
})
|
||||
const { req } = await once(stream, 'data')
|
||||
assert.equal(req.headers.cookie, '[Redacted]')
|
||||
assert.equal(req.headers.host, '[Redacted]')
|
||||
assert.equal(req.headers.connection, '[Redacted]')
|
||||
})
|
||||
|
||||
test('redact – supports first position wildcard paths', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: ['*.headers'] }, stream)
|
||||
instance.info({
|
||||
req: {
|
||||
id: 7915,
|
||||
method: 'GET',
|
||||
url: '/',
|
||||
headers: {
|
||||
host: 'localhost:3000',
|
||||
connection: 'keep-alive',
|
||||
cookie: 'SESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;'
|
||||
},
|
||||
remoteAddress: '::ffff:127.0.0.1',
|
||||
remotePort: 58022
|
||||
}
|
||||
})
|
||||
const { req } = await once(stream, 'data')
|
||||
assert.equal(req.headers, '[Redacted]')
|
||||
})
|
||||
|
||||
test('redact – supports first position wildcards before other paths', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: ['*.headers.cookie', 'req.id'] }, stream)
|
||||
instance.info({
|
||||
req: {
|
||||
id: 7915,
|
||||
method: 'GET',
|
||||
url: '/',
|
||||
headers: {
|
||||
host: 'localhost:3000',
|
||||
connection: 'keep-alive',
|
||||
cookie: 'SESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;'
|
||||
},
|
||||
remoteAddress: '::ffff:127.0.0.1',
|
||||
remotePort: 58022
|
||||
}
|
||||
})
|
||||
const { req } = await once(stream, 'data')
|
||||
assert.equal(req.headers.cookie, '[Redacted]')
|
||||
assert.equal(req.id, '[Redacted]')
|
||||
})
|
||||
|
||||
test('redact – supports first position wildcards after other paths', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: ['req.id', '*.headers.cookie'] }, stream)
|
||||
instance.info({
|
||||
req: {
|
||||
id: 7915,
|
||||
method: 'GET',
|
||||
url: '/',
|
||||
headers: {
|
||||
host: 'localhost:3000',
|
||||
connection: 'keep-alive',
|
||||
cookie: 'SESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;'
|
||||
},
|
||||
remoteAddress: '::ffff:127.0.0.1',
|
||||
remotePort: 58022
|
||||
}
|
||||
})
|
||||
const { req } = await once(stream, 'data')
|
||||
assert.equal(req.headers.cookie, '[Redacted]')
|
||||
assert.equal(req.id, '[Redacted]')
|
||||
})
|
||||
|
||||
test('redact – supports first position wildcards after top level keys', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: ['key', '*.headers.cookie'] }, stream)
|
||||
instance.info({
|
||||
req: {
|
||||
id: 7915,
|
||||
method: 'GET',
|
||||
url: '/',
|
||||
headers: {
|
||||
host: 'localhost:3000',
|
||||
connection: 'keep-alive',
|
||||
cookie: 'SESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;'
|
||||
},
|
||||
remoteAddress: '::ffff:127.0.0.1',
|
||||
remotePort: 58022
|
||||
}
|
||||
})
|
||||
const { req } = await once(stream, 'data')
|
||||
assert.equal(req.headers.cookie, '[Redacted]')
|
||||
})
|
||||
|
||||
test('redact – supports top level wildcard', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: ['*'] }, stream)
|
||||
instance.info({
|
||||
req: {
|
||||
id: 7915,
|
||||
method: 'GET',
|
||||
url: '/',
|
||||
headers: {
|
||||
host: 'localhost:3000',
|
||||
connection: 'keep-alive',
|
||||
cookie: 'SESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;'
|
||||
},
|
||||
remoteAddress: '::ffff:127.0.0.1',
|
||||
remotePort: 58022
|
||||
}
|
||||
})
|
||||
const { req } = await once(stream, 'data')
|
||||
assert.equal(req, '[Redacted]')
|
||||
})
|
||||
|
||||
test('redact – supports top level wildcard with a censor function', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({
|
||||
redact: {
|
||||
paths: ['*'],
|
||||
censor: () => '[Redacted]'
|
||||
}
|
||||
}, stream)
|
||||
instance.info({
|
||||
req: {
|
||||
id: 7915,
|
||||
method: 'GET',
|
||||
url: '/',
|
||||
headers: {
|
||||
host: 'localhost:3000',
|
||||
connection: 'keep-alive',
|
||||
cookie: 'SESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;'
|
||||
},
|
||||
remoteAddress: '::ffff:127.0.0.1',
|
||||
remotePort: 58022
|
||||
}
|
||||
})
|
||||
const { req } = await once(stream, 'data')
|
||||
assert.equal(req, '[Redacted]')
|
||||
})
|
||||
|
||||
test('redact – supports top level wildcard and leading wildcard', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: ['*', '*.req'] }, stream)
|
||||
instance.info({
|
||||
req: {
|
||||
id: 7915,
|
||||
method: 'GET',
|
||||
url: '/',
|
||||
headers: {
|
||||
host: 'localhost:3000',
|
||||
connection: 'keep-alive',
|
||||
cookie: 'SESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;'
|
||||
},
|
||||
remoteAddress: '::ffff:127.0.0.1',
|
||||
remotePort: 58022
|
||||
}
|
||||
})
|
||||
const { req } = await once(stream, 'data')
|
||||
assert.equal(req, '[Redacted]')
|
||||
})
|
||||
|
||||
test('redact – supports intermediate wildcard paths', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: ['req.*.cookie'] }, stream)
|
||||
instance.info({
|
||||
req: {
|
||||
id: 7915,
|
||||
method: 'GET',
|
||||
url: '/',
|
||||
headers: {
|
||||
host: 'localhost:3000',
|
||||
connection: 'keep-alive',
|
||||
cookie: 'SESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;'
|
||||
},
|
||||
remoteAddress: '::ffff:127.0.0.1',
|
||||
remotePort: 58022
|
||||
}
|
||||
})
|
||||
const { req } = await once(stream, 'data')
|
||||
assert.equal(req.headers.cookie, '[Redacted]')
|
||||
})
|
||||
|
||||
test('redacts numbers at the top level', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: ['id'] }, stream)
|
||||
const obj = {
|
||||
id: 7915
|
||||
}
|
||||
instance.info(obj)
|
||||
const o = await once(stream, 'data')
|
||||
assert.equal(o.id, '[Redacted]')
|
||||
})
|
||||
|
||||
test('redacts booleans at the top level', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: ['maybe'] }, stream)
|
||||
const obj = {
|
||||
maybe: true
|
||||
}
|
||||
instance.info(obj)
|
||||
const o = await once(stream, 'data')
|
||||
assert.equal(o.maybe, '[Redacted]')
|
||||
})
|
||||
|
||||
test('redacts strings at the top level', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: ['s'] }, stream)
|
||||
const obj = {
|
||||
s: 's'
|
||||
}
|
||||
instance.info(obj)
|
||||
const o = await once(stream, 'data')
|
||||
assert.equal(o.s, '[Redacted]')
|
||||
})
|
||||
|
||||
test('does not redact primitives if not objects', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: ['a.b'] }, stream)
|
||||
const obj = {
|
||||
a: 42
|
||||
}
|
||||
instance.info(obj)
|
||||
const o = await once(stream, 'data')
|
||||
assert.equal(o.a, 42)
|
||||
})
|
||||
|
||||
test('redacts null at the top level', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: ['n'] }, stream)
|
||||
const obj = {
|
||||
n: null
|
||||
}
|
||||
instance.info(obj)
|
||||
const o = await once(stream, 'data')
|
||||
assert.equal(o.n, '[Redacted]')
|
||||
})
|
||||
|
||||
test('supports bracket notation', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: ['a["b.b"]'] }, stream)
|
||||
const obj = {
|
||||
a: { 'b.b': 'c' }
|
||||
}
|
||||
instance.info(obj)
|
||||
const o = await once(stream, 'data')
|
||||
assert.equal(o.a['b.b'], '[Redacted]')
|
||||
})
|
||||
|
||||
test('supports bracket notation with further nesting', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: ['a["b.b"].c'] }, stream)
|
||||
const obj = {
|
||||
a: { 'b.b': { c: 'd' } }
|
||||
}
|
||||
instance.info(obj)
|
||||
const o = await once(stream, 'data')
|
||||
assert.equal(o.a['b.b'].c, '[Redacted]')
|
||||
})
|
||||
|
||||
test('supports bracket notation with empty string as path segment', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: ['a[""].c'] }, stream)
|
||||
const obj = {
|
||||
a: { '': { c: 'd' } }
|
||||
}
|
||||
instance.info(obj)
|
||||
const o = await once(stream, 'data')
|
||||
assert.equal(o.a[''].c, '[Redacted]')
|
||||
})
|
||||
|
||||
test('supports leading bracket notation (single quote)', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: ['[\'a.a\'].b'] }, stream)
|
||||
const obj = {
|
||||
'a.a': { b: 'c' }
|
||||
}
|
||||
instance.info(obj)
|
||||
const o = await once(stream, 'data')
|
||||
assert.equal(o['a.a'].b, '[Redacted]')
|
||||
})
|
||||
|
||||
test('supports leading bracket notation (double quote)', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: ['["a.a"].b'] }, stream)
|
||||
const obj = {
|
||||
'a.a': { b: 'c' }
|
||||
}
|
||||
instance.info(obj)
|
||||
const o = await once(stream, 'data')
|
||||
assert.equal(o['a.a'].b, '[Redacted]')
|
||||
})
|
||||
|
||||
test('supports leading bracket notation (backtick quote)', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: ['[`a.a`].b'] }, stream)
|
||||
const obj = {
|
||||
'a.a': { b: 'c' }
|
||||
}
|
||||
instance.info(obj)
|
||||
const o = await once(stream, 'data')
|
||||
assert.equal(o['a.a'].b, '[Redacted]')
|
||||
})
|
||||
|
||||
test('supports leading bracket notation (single-segment path)', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: ['[`a.a`]'] }, stream)
|
||||
const obj = {
|
||||
'a.a': { b: 'c' }
|
||||
}
|
||||
instance.info(obj)
|
||||
const o = await once(stream, 'data')
|
||||
assert.equal(o['a.a'], '[Redacted]')
|
||||
})
|
||||
|
||||
test('supports leading bracket notation (single-segment path, wildcard)', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: ['[*]'] }, stream)
|
||||
const obj = {
|
||||
'a.a': { b: 'c' }
|
||||
}
|
||||
instance.info(obj)
|
||||
const o = await once(stream, 'data')
|
||||
assert.equal(o['a.a'], '[Redacted]')
|
||||
})
|
||||
|
||||
test('child bindings are redacted using wildcard path', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: ['*.headers.cookie'] }, stream)
|
||||
instance.child({
|
||||
req: {
|
||||
method: 'GET',
|
||||
url: '/',
|
||||
headers: {
|
||||
cookie: 'SESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;'
|
||||
}
|
||||
}
|
||||
}).info('message completed')
|
||||
const { req } = await once(stream, 'data')
|
||||
assert.equal(req.headers.cookie, '[Redacted]')
|
||||
})
|
||||
|
||||
test('child bindings are redacted using wildcard and plain path keys', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: ['req.method', '*.headers.cookie'] }, stream)
|
||||
instance.child({
|
||||
req: {
|
||||
method: 'GET',
|
||||
url: '/',
|
||||
headers: {
|
||||
cookie: 'SESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;'
|
||||
}
|
||||
}
|
||||
}).info('message completed')
|
||||
const { req } = await once(stream, 'data')
|
||||
assert.equal(req.headers.cookie, '[Redacted]')
|
||||
assert.equal(req.method, '[Redacted]')
|
||||
})
|
||||
|
||||
test('redacts boolean at the top level', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: ['msg'] }, stream)
|
||||
const obj = {
|
||||
s: 's'
|
||||
}
|
||||
instance.info(obj, true)
|
||||
const o = await once(stream, 'data')
|
||||
assert.equal(o.s, 's')
|
||||
assert.equal(o.msg, '[Redacted]')
|
||||
})
|
||||
|
||||
test('child can customize redact', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: ['req.method', '*.headers.cookie'] }, stream)
|
||||
instance.child({
|
||||
req: {
|
||||
method: 'GET',
|
||||
url: '/',
|
||||
headers: {
|
||||
cookie: 'SESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;'
|
||||
}
|
||||
}
|
||||
}, {
|
||||
redact: ['req.url']
|
||||
}).info('message completed')
|
||||
const { req } = await once(stream, 'data')
|
||||
assert.equal(req.headers.cookie, 'SESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;')
|
||||
assert.equal(req.method, 'GET')
|
||||
assert.equal(req.url, '[Redacted]')
|
||||
})
|
||||
|
||||
test('child can remove parent redact by array', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: ['req.method', '*.headers.cookie'] }, stream)
|
||||
instance.child({
|
||||
req: {
|
||||
method: 'GET',
|
||||
url: '/',
|
||||
headers: {
|
||||
cookie: 'SESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;'
|
||||
}
|
||||
}
|
||||
}, {
|
||||
redact: []
|
||||
}).info('message completed')
|
||||
const { req } = await once(stream, 'data')
|
||||
assert.equal(req.headers.cookie, 'SESSID=298zf09hf012fh2; csrftoken=u32t4o3tb3gg43; _gat=1;')
|
||||
assert.equal(req.method, 'GET')
|
||||
})
|
||||
|
||||
test('redact safe stringify', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ redact: { paths: ['that.secret'] } }, stream)
|
||||
|
||||
instance.info({
|
||||
that: {
|
||||
secret: 'please hide me',
|
||||
myBigInt: 123n
|
||||
},
|
||||
other: {
|
||||
mySecondBigInt: 222n
|
||||
}
|
||||
})
|
||||
const { that, other } = await once(stream, 'data')
|
||||
assert.equal(that.secret, '[Redacted]')
|
||||
assert.equal(that.myBigInt, 123)
|
||||
assert.equal(other.mySecondBigInt, 222)
|
||||
})
|
||||
|
||||
test('censor function should not be called for non-existent nested paths (issue #2313)', async () => {
|
||||
const stream = sink()
|
||||
const censorCalls = []
|
||||
|
||||
const instance = pino({
|
||||
redact: {
|
||||
paths: ['a.b.c', 'req.authorization', 'url'],
|
||||
censor (value, path) {
|
||||
censorCalls.push({ value, path: path.join('.') })
|
||||
if (typeof value !== 'string') {
|
||||
return '***'
|
||||
}
|
||||
return '***'
|
||||
}
|
||||
}
|
||||
}, stream)
|
||||
|
||||
// Test case 1: parent exists but nested path doesn't
|
||||
censorCalls.length = 0
|
||||
instance.info({ req: { id: 'test' } }, 'test message')
|
||||
await once(stream, 'data')
|
||||
assert.equal(censorCalls.length, 0, 'censor should not be called when req.authorization does not exist')
|
||||
|
||||
// Test case 2: parent exists but deeply nested path doesn't
|
||||
censorCalls.length = 0
|
||||
instance.info({ a: { d: 'test' } }, 'test message')
|
||||
await once(stream, 'data')
|
||||
assert.equal(censorCalls.length, 0, 'censor should not be called when a.b.c does not exist')
|
||||
|
||||
// Test case 3: multiple parent keys exist but nested paths don't
|
||||
censorCalls.length = 0
|
||||
instance.info({ a: { c: 'should-not-show-me' }, req: { id: 'test' } }, 'test message')
|
||||
await once(stream, 'data')
|
||||
assert.equal(censorCalls.length, 0, 'censor should not be called when neither a.b.c nor req.authorization exist')
|
||||
|
||||
// Test case 4: verify censor IS called when path exists
|
||||
censorCalls.length = 0
|
||||
instance.info({ req: { authorization: 'bearer token' } }, 'test message')
|
||||
await once(stream, 'data')
|
||||
assert.equal(censorCalls.length, 1, 'censor should be called when req.authorization exists')
|
||||
assert.equal(censorCalls[0].path, 'req.authorization')
|
||||
assert.equal(censorCalls[0].value, 'bearer token')
|
||||
})
|
||||
+257
@@ -0,0 +1,257 @@
|
||||
'use strict'
|
||||
|
||||
const test = require('node:test')
|
||||
const assert = require('node:assert')
|
||||
const stdSerializers = require('pino-std-serializers')
|
||||
|
||||
const { sink, once } = require('./helper')
|
||||
const pino = require('../')
|
||||
|
||||
const parentSerializers = {
|
||||
test: () => 'parent'
|
||||
}
|
||||
|
||||
const childSerializers = {
|
||||
test: () => 'child'
|
||||
}
|
||||
|
||||
test('default err namespace error serializer', async () => {
|
||||
const stream = sink()
|
||||
const parent = pino(stream)
|
||||
|
||||
parent.info({ err: ReferenceError('test') })
|
||||
const o = await once(stream, 'data')
|
||||
assert.equal(typeof o.err, 'object')
|
||||
assert.equal(o.err.type, 'ReferenceError')
|
||||
assert.equal(o.err.message, 'test')
|
||||
assert.equal(typeof o.err.stack, 'string')
|
||||
})
|
||||
|
||||
test('custom serializer overrides default err namespace error serializer', async () => {
|
||||
const stream = sink()
|
||||
const parent = pino({
|
||||
serializers: {
|
||||
err: (e) => ({
|
||||
t: e.constructor.name,
|
||||
m: e.message,
|
||||
s: e.stack
|
||||
})
|
||||
}
|
||||
}, stream)
|
||||
|
||||
parent.info({ err: ReferenceError('test') })
|
||||
const o = await once(stream, 'data')
|
||||
assert.equal(typeof o.err, 'object')
|
||||
assert.equal(o.err.t, 'ReferenceError')
|
||||
assert.equal(o.err.m, 'test')
|
||||
assert.equal(typeof o.err.s, 'string')
|
||||
})
|
||||
|
||||
test('custom serializer overrides default err namespace error serializer when nestedKey is on', async () => {
|
||||
const stream = sink()
|
||||
const parent = pino({
|
||||
nestedKey: 'obj',
|
||||
serializers: {
|
||||
err: (e) => {
|
||||
return {
|
||||
t: e.constructor.name,
|
||||
m: e.message,
|
||||
s: e.stack
|
||||
}
|
||||
}
|
||||
}
|
||||
}, stream)
|
||||
|
||||
parent.info({ err: ReferenceError('test') })
|
||||
const o = await once(stream, 'data')
|
||||
assert.equal(typeof o.obj.err, 'object')
|
||||
assert.equal(o.obj.err.t, 'ReferenceError')
|
||||
assert.equal(o.obj.err.m, 'test')
|
||||
assert.equal(typeof o.obj.err.s, 'string')
|
||||
})
|
||||
|
||||
test('null overrides default err namespace error serializer', async () => {
|
||||
const stream = sink()
|
||||
const parent = pino({ serializers: { err: null } }, stream)
|
||||
|
||||
parent.info({ err: ReferenceError('test') })
|
||||
const o = await once(stream, 'data')
|
||||
assert.equal(typeof o.err, 'object')
|
||||
assert.equal(typeof o.err.type, 'undefined')
|
||||
assert.equal(typeof o.err.message, 'undefined')
|
||||
assert.equal(typeof o.err.stack, 'undefined')
|
||||
})
|
||||
|
||||
test('undefined overrides default err namespace error serializer', async () => {
|
||||
const stream = sink()
|
||||
const parent = pino({ serializers: { err: undefined } }, stream)
|
||||
|
||||
parent.info({ err: ReferenceError('test') })
|
||||
const o = await once(stream, 'data')
|
||||
assert.equal(typeof o.err, 'object')
|
||||
assert.equal(typeof o.err.type, 'undefined')
|
||||
assert.equal(typeof o.err.message, 'undefined')
|
||||
assert.equal(typeof o.err.stack, 'undefined')
|
||||
})
|
||||
|
||||
test('serializers override values', async () => {
|
||||
const stream = sink()
|
||||
const parent = pino({ serializers: parentSerializers }, stream)
|
||||
parent.child({}, { serializers: childSerializers })
|
||||
|
||||
parent.fatal({ test: 'test' })
|
||||
const o = await once(stream, 'data')
|
||||
assert.equal(o.test, 'parent')
|
||||
})
|
||||
|
||||
test('child does not overwrite parent serializers', async () => {
|
||||
const stream = sink()
|
||||
const parent = pino({ serializers: parentSerializers }, stream)
|
||||
const child = parent.child({}, { serializers: childSerializers })
|
||||
|
||||
parent.fatal({ test: 'test' })
|
||||
|
||||
const o = once(stream, 'data')
|
||||
assert.equal((await o).test, 'parent')
|
||||
const o2 = once(stream, 'data')
|
||||
child.fatal({ test: 'test' })
|
||||
assert.equal((await o2).test, 'child')
|
||||
})
|
||||
|
||||
test('Symbol.for(\'pino.serializers\')', async () => {
|
||||
const stream = sink()
|
||||
const expected = Object.assign({
|
||||
err: stdSerializers.err
|
||||
}, parentSerializers)
|
||||
const parent = pino({ serializers: parentSerializers }, stream)
|
||||
const child = parent.child({ a: 'property' })
|
||||
|
||||
assert.deepEqual(parent[Symbol.for('pino.serializers')], expected)
|
||||
assert.deepEqual(child[Symbol.for('pino.serializers')], expected)
|
||||
assert.equal(parent[Symbol.for('pino.serializers')], child[Symbol.for('pino.serializers')])
|
||||
|
||||
const child2 = parent.child({}, {
|
||||
serializers: {
|
||||
a
|
||||
}
|
||||
})
|
||||
|
||||
function a () {
|
||||
return 'hello'
|
||||
}
|
||||
|
||||
// eslint-disable-next-line eqeqeq
|
||||
assert.equal(child2[Symbol.for('pino.serializers')] != parentSerializers, true)
|
||||
assert.equal(child2[Symbol.for('pino.serializers')].a, a)
|
||||
assert.equal(child2[Symbol.for('pino.serializers')].test, parentSerializers.test)
|
||||
})
|
||||
|
||||
test('children inherit parent serializers', async () => {
|
||||
const stream = sink()
|
||||
const parent = pino({ serializers: parentSerializers }, stream)
|
||||
|
||||
const child = parent.child({ a: 'property' })
|
||||
child.fatal({ test: 'test' })
|
||||
const o = await once(stream, 'data')
|
||||
assert.equal(o.test, 'parent')
|
||||
})
|
||||
|
||||
test('children inherit parent Symbol serializers', async () => {
|
||||
const stream = sink()
|
||||
const symbolSerializers = {
|
||||
[Symbol.for('b')]: b
|
||||
}
|
||||
const expected = Object.assign({
|
||||
err: stdSerializers.err
|
||||
}, symbolSerializers)
|
||||
const parent = pino({ serializers: symbolSerializers }, stream)
|
||||
|
||||
assert.deepEqual(parent[Symbol.for('pino.serializers')], expected)
|
||||
|
||||
const child = parent.child({}, {
|
||||
serializers: {
|
||||
[Symbol.for('a')]: a,
|
||||
a
|
||||
}
|
||||
})
|
||||
|
||||
function a () {
|
||||
return 'hello'
|
||||
}
|
||||
|
||||
function b () {
|
||||
return 'world'
|
||||
}
|
||||
|
||||
assert.deepEqual(child[Symbol.for('pino.serializers')].a, a)
|
||||
assert.deepEqual(child[Symbol.for('pino.serializers')][Symbol.for('b')], b)
|
||||
assert.deepEqual(child[Symbol.for('pino.serializers')][Symbol.for('a')], a)
|
||||
})
|
||||
|
||||
test('children serializers get called', async () => {
|
||||
const stream = sink()
|
||||
const parent = pino({
|
||||
test: 'this'
|
||||
}, stream)
|
||||
|
||||
const child = parent.child({ a: 'property' }, { serializers: childSerializers })
|
||||
|
||||
child.fatal({ test: 'test' })
|
||||
const o = await once(stream, 'data')
|
||||
assert.equal(o.test, 'child')
|
||||
})
|
||||
|
||||
test('children serializers get called when inherited from parent', async () => {
|
||||
const stream = sink()
|
||||
const parent = pino({
|
||||
test: 'this',
|
||||
serializers: parentSerializers
|
||||
}, stream)
|
||||
|
||||
const child = parent.child({}, { serializers: { test: function () { return 'pass' } } })
|
||||
|
||||
child.fatal({ test: 'fail' })
|
||||
const o = await once(stream, 'data')
|
||||
assert.equal(o.test, 'pass')
|
||||
})
|
||||
|
||||
test('non-overridden serializers are available in the children', async () => {
|
||||
const stream = sink()
|
||||
const pSerializers = {
|
||||
onlyParent: function () { return 'parent' },
|
||||
shared: function () { return 'parent' }
|
||||
}
|
||||
|
||||
const cSerializers = {
|
||||
shared: function () { return 'child' },
|
||||
onlyChild: function () { return 'child' }
|
||||
}
|
||||
|
||||
const parent = pino({ serializers: pSerializers }, stream)
|
||||
|
||||
const child = parent.child({}, { serializers: cSerializers })
|
||||
|
||||
const o = once(stream, 'data')
|
||||
child.fatal({ shared: 'test' })
|
||||
assert.equal((await o).shared, 'child')
|
||||
const o2 = once(stream, 'data')
|
||||
child.fatal({ onlyParent: 'test' })
|
||||
assert.equal((await o2).onlyParent, 'parent')
|
||||
const o3 = once(stream, 'data')
|
||||
child.fatal({ onlyChild: 'test' })
|
||||
assert.equal((await o3).onlyChild, 'child')
|
||||
const o4 = once(stream, 'data')
|
||||
parent.fatal({ onlyChild: 'test' })
|
||||
assert.equal((await o4).onlyChild, 'test')
|
||||
})
|
||||
|
||||
test('custom serializer for messageKey', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ serializers: { msg: () => '422' } }, stream)
|
||||
|
||||
const o = { num: NaN }
|
||||
instance.info(o, 42)
|
||||
|
||||
const { msg } = await once(stream, 'data')
|
||||
assert.equal(msg, '422')
|
||||
})
|
||||
+41
@@ -0,0 +1,41 @@
|
||||
'use strict'
|
||||
|
||||
const test = require('node:test')
|
||||
const assert = require('node:assert')
|
||||
const { join } = require('node:path')
|
||||
const { fork } = require('node:child_process')
|
||||
const writer = require('flush-write-stream')
|
||||
|
||||
const { once } = require('./helper')
|
||||
const pino = require('..')
|
||||
|
||||
test('do not use SonicBoom is someone tampered with process.stdout.write', async () => {
|
||||
let actual = ''
|
||||
const child = fork(join(__dirname, 'fixtures', 'stdout-hack-protection.js'), { silent: true })
|
||||
|
||||
child.stdout.pipe(writer((s, enc, cb) => {
|
||||
actual += s
|
||||
cb()
|
||||
}))
|
||||
await once(child, 'close')
|
||||
assert.equal(actual.match(/^hack/) != null, true)
|
||||
})
|
||||
|
||||
test('do not use SonicBoom is someone has passed process.stdout to pino', async () => {
|
||||
const logger = pino(process.stdout)
|
||||
assert.equal(logger[pino.symbols.streamSym], process.stdout)
|
||||
})
|
||||
|
||||
test('do not crash if process.stdout has no fd', async (t) => {
|
||||
const fd = process.stdout.fd
|
||||
delete process.stdout.fd
|
||||
t.after(function () { process.stdout.fd = fd })
|
||||
pino()
|
||||
})
|
||||
|
||||
test('use fd=1 if process.stdout has no fd in pino.destination() (worker case)', async (t) => {
|
||||
const fd = process.stdout.fd
|
||||
delete process.stdout.fd
|
||||
t.after(function () { process.stdout.fd = fd })
|
||||
pino.destination()
|
||||
})
|
||||
+186
@@ -0,0 +1,186 @@
|
||||
'use strict'
|
||||
|
||||
const test = require('node:test')
|
||||
const assert = require('node:assert')
|
||||
const os = require('node:os')
|
||||
const { promises: { readFile }, createWriteStream } = require('node:fs')
|
||||
const { join } = require('node:path')
|
||||
const { fork } = require('node:child_process')
|
||||
const writer = require('flush-write-stream')
|
||||
const {
|
||||
once,
|
||||
getPathToNull,
|
||||
file,
|
||||
watchFileCreated
|
||||
} = require('./helper')
|
||||
const { promisify } = require('node:util')
|
||||
const tspl = require('@matteo.collina/tspl')
|
||||
|
||||
const sleep = promisify(setTimeout)
|
||||
|
||||
test('asynchronous logging', async (t) => {
|
||||
const now = Date.now
|
||||
const hostname = os.hostname
|
||||
const proc = process
|
||||
global.process = {
|
||||
__proto__: process,
|
||||
pid: 123456
|
||||
}
|
||||
Date.now = () => 1459875739796
|
||||
os.hostname = () => 'abcdefghijklmnopqr'
|
||||
delete require.cache[require.resolve('../')]
|
||||
const pino = require('../')
|
||||
let expected = ''
|
||||
let actual = ''
|
||||
const normal = pino(writer((s, enc, cb) => {
|
||||
expected += s
|
||||
cb()
|
||||
}))
|
||||
|
||||
const dest = createWriteStream(getPathToNull())
|
||||
dest.write = (s) => {
|
||||
actual += s
|
||||
}
|
||||
const asyncLogger = pino(dest)
|
||||
|
||||
let i = 44
|
||||
while (i--) {
|
||||
normal.info('h')
|
||||
asyncLogger.info('h')
|
||||
}
|
||||
|
||||
const expected2 = expected.split('\n')[0]
|
||||
let actual2 = ''
|
||||
|
||||
const child = fork(join(__dirname, '/fixtures/syncfalse.js'), { silent: true })
|
||||
child.stdout.pipe(writer((s, enc, cb) => {
|
||||
actual2 += s
|
||||
cb()
|
||||
}))
|
||||
await once(child, 'close')
|
||||
// Wait for the last write to be flushed
|
||||
await sleep(100)
|
||||
assert.equal(actual, expected)
|
||||
assert.equal(actual2.trim(), expected2)
|
||||
|
||||
t.after(() => {
|
||||
os.hostname = hostname
|
||||
Date.now = now
|
||||
global.process = proc
|
||||
})
|
||||
})
|
||||
|
||||
test('sync false with child', async (t) => {
|
||||
const now = Date.now
|
||||
const hostname = os.hostname
|
||||
const proc = process
|
||||
global.process = {
|
||||
__proto__: process,
|
||||
pid: 123456
|
||||
}
|
||||
Date.now = function () {
|
||||
return 1459875739796
|
||||
}
|
||||
os.hostname = function () {
|
||||
return 'abcdefghijklmnopqr'
|
||||
}
|
||||
delete require.cache[require.resolve('../')]
|
||||
const pino = require('../')
|
||||
let expected = ''
|
||||
let actual = ''
|
||||
const normal = pino(writer((s, enc, cb) => {
|
||||
expected += s
|
||||
cb()
|
||||
})).child({ hello: 'world' })
|
||||
|
||||
const dest = createWriteStream(getPathToNull())
|
||||
dest.write = function (s) {
|
||||
actual += s
|
||||
}
|
||||
const asyncLogger = pino(dest).child({ hello: 'world' })
|
||||
|
||||
let i = 500
|
||||
while (i--) {
|
||||
normal.info('h')
|
||||
asyncLogger.info('h')
|
||||
}
|
||||
|
||||
asyncLogger.flush()
|
||||
|
||||
const expected2 = expected.split('\n')[0]
|
||||
let actual2 = ''
|
||||
|
||||
const child = fork(join(__dirname, '/fixtures/syncfalse-child.js'), { silent: true })
|
||||
child.stdout.pipe(writer((s, enc, cb) => {
|
||||
actual2 += s
|
||||
cb()
|
||||
}))
|
||||
await once(child, 'close')
|
||||
assert.equal(actual, expected)
|
||||
assert.equal(actual2.trim(), expected2)
|
||||
|
||||
t.after(() => {
|
||||
os.hostname = hostname
|
||||
Date.now = now
|
||||
global.process = proc
|
||||
})
|
||||
})
|
||||
|
||||
test('flush does nothing with sync true (default)', async () => {
|
||||
const instance = require('..')()
|
||||
assert.equal(instance.flush(), undefined)
|
||||
})
|
||||
|
||||
test('should still call flush callback even when does nothing with sync true (default)', async (t) => {
|
||||
const plan = tspl(t, { plan: 3 })
|
||||
const instance = require('..')()
|
||||
instance.flush((...args) => {
|
||||
plan.ok('flush called')
|
||||
plan.deepEqual(args, [])
|
||||
|
||||
// next tick to make flush not called more than once
|
||||
process.nextTick(() => {
|
||||
plan.ok('flush next tick called')
|
||||
})
|
||||
})
|
||||
|
||||
await plan
|
||||
})
|
||||
|
||||
test('should call the flush callback when flushed the data for async logger', async () => {
|
||||
const outputPath = file()
|
||||
async function getOutputLogLines () {
|
||||
return (await readFile(outputPath)).toString().trim().split('\n').map(JSON.parse)
|
||||
}
|
||||
|
||||
const pino = require('../')
|
||||
|
||||
const instance = pino({}, pino.destination({
|
||||
dest: outputPath,
|
||||
|
||||
// to make sure it does not flush on its own
|
||||
minLength: 4096
|
||||
}))
|
||||
const flushPromise = promisify(instance.flush).bind(instance)
|
||||
|
||||
instance.info('hello')
|
||||
await flushPromise()
|
||||
await watchFileCreated(outputPath)
|
||||
|
||||
const [firstFlushData] = await getOutputLogLines()
|
||||
|
||||
assert.equal(firstFlushData.msg, 'hello')
|
||||
|
||||
// should not flush this as no data accumulated that's bigger than min length
|
||||
instance.info('world')
|
||||
|
||||
// Making sure data is not flushed yet
|
||||
const afterLogData = await getOutputLogLines()
|
||||
assert.equal(afterLogData.length, 1)
|
||||
|
||||
await flushPromise()
|
||||
|
||||
// Making sure data is not flushed yet
|
||||
const afterSecondFlush = (await getOutputLogLines())[1]
|
||||
assert.equal(afterSecondFlush.msg, 'world')
|
||||
})
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
'use strict'
|
||||
|
||||
/* eslint no-prototype-builtins: 0 */
|
||||
|
||||
const test = require('node:test')
|
||||
const assert = require('node:assert')
|
||||
|
||||
const { sink, once } = require('./helper')
|
||||
|
||||
test('pino.stdTimeFunctions.isoTimeNano returns RFC 3339 timestamps', async () => {
|
||||
// Mock Date.now at module initialization time
|
||||
const now = Date.now
|
||||
Date.now = () => new Date('2025-08-01T15:03:45.000000000Z').getTime()
|
||||
|
||||
// Mock process.hrtime.bigint at module initialization time
|
||||
const hrTimeBigint = process.hrtime.bigint
|
||||
process.hrtime.bigint = () => 100000000000000n
|
||||
|
||||
const pino = require('../')
|
||||
|
||||
const opts = {
|
||||
timestamp: pino.stdTimeFunctions.isoTimeNano
|
||||
}
|
||||
const stream = sink()
|
||||
|
||||
// Mock process.hrtime.bigint at invocation time, add 1 day to the timestamp
|
||||
process.hrtime.bigint = () => 100000000000000n + 86400012345678n
|
||||
|
||||
const instance = pino(opts, stream)
|
||||
instance.info('foobar')
|
||||
const result = await once(stream, 'data')
|
||||
assert.equal(result.hasOwnProperty('time'), true)
|
||||
assert.equal(result.time, '2025-08-02T15:03:45.012345678Z')
|
||||
|
||||
Date.now = now
|
||||
process.hrtime.bigint = hrTimeBigint
|
||||
})
|
||||
+124
@@ -0,0 +1,124 @@
|
||||
'use strict'
|
||||
|
||||
/* eslint no-prototype-builtins: 0 */
|
||||
|
||||
const test = require('node:test')
|
||||
const assert = require('node:assert')
|
||||
|
||||
const { sink, once } = require('./helper')
|
||||
const pino = require('../')
|
||||
|
||||
test('pino exposes standard time functions', async () => {
|
||||
assert.ok(pino.stdTimeFunctions)
|
||||
assert.ok(pino.stdTimeFunctions.epochTime)
|
||||
assert.ok(pino.stdTimeFunctions.unixTime)
|
||||
assert.ok(pino.stdTimeFunctions.nullTime)
|
||||
assert.ok(pino.stdTimeFunctions.isoTime)
|
||||
assert.ok(pino.stdTimeFunctions.isoTimeNano)
|
||||
})
|
||||
|
||||
test('pino accepts external time functions', async () => {
|
||||
const opts = {
|
||||
timestamp: () => ',"time":"none"'
|
||||
}
|
||||
const stream = sink()
|
||||
const instance = pino(opts, stream)
|
||||
instance.info('foobar')
|
||||
const result = await once(stream, 'data')
|
||||
assert.equal(result.hasOwnProperty('time'), true)
|
||||
assert.equal(result.time, 'none')
|
||||
})
|
||||
|
||||
test('pino accepts external time functions with custom label', async () => {
|
||||
const opts = {
|
||||
timestamp: () => ',"custom-time-label":"none"'
|
||||
}
|
||||
const stream = sink()
|
||||
const instance = pino(opts, stream)
|
||||
instance.info('foobar')
|
||||
const result = await once(stream, 'data')
|
||||
assert.equal(result.hasOwnProperty('custom-time-label'), true)
|
||||
assert.equal(result['custom-time-label'], 'none')
|
||||
})
|
||||
|
||||
test('inserts timestamp by default', async ({ ok, equal }) => {
|
||||
const stream = sink()
|
||||
const instance = pino(stream)
|
||||
instance.info('foobar')
|
||||
const result = await once(stream, 'data')
|
||||
assert.equal(result.hasOwnProperty('time'), true)
|
||||
assert.ok(new Date(result.time) <= new Date(), 'time is greater than timestamp')
|
||||
assert.equal(result.msg, 'foobar')
|
||||
})
|
||||
|
||||
test('omits timestamp when timestamp option is false', async () => {
|
||||
const stream = sink()
|
||||
const instance = pino({ timestamp: false }, stream)
|
||||
instance.info('foobar')
|
||||
const result = await once(stream, 'data')
|
||||
assert.equal(result.hasOwnProperty('time'), false)
|
||||
assert.equal(result.msg, 'foobar')
|
||||
})
|
||||
|
||||
test('inserts timestamp when timestamp option is true', async ({ ok, equal }) => {
|
||||
const stream = sink()
|
||||
const instance = pino({ timestamp: true }, stream)
|
||||
instance.info('foobar')
|
||||
const result = await once(stream, 'data')
|
||||
assert.equal(result.hasOwnProperty('time'), true)
|
||||
assert.ok(new Date(result.time) <= new Date(), 'time is greater than timestamp')
|
||||
assert.equal(result.msg, 'foobar')
|
||||
})
|
||||
|
||||
test('child inserts timestamp by default', async ({ ok, equal }) => {
|
||||
const stream = sink()
|
||||
const logger = pino(stream)
|
||||
const instance = logger.child({ component: 'child' })
|
||||
instance.info('foobar')
|
||||
const result = await once(stream, 'data')
|
||||
assert.equal(result.hasOwnProperty('time'), true)
|
||||
assert.ok(new Date(result.time) <= new Date(), 'time is greater than timestamp')
|
||||
assert.equal(result.msg, 'foobar')
|
||||
})
|
||||
|
||||
test('child omits timestamp with option', async () => {
|
||||
const stream = sink()
|
||||
const logger = pino({ timestamp: false }, stream)
|
||||
const instance = logger.child({ component: 'child' })
|
||||
instance.info('foobar')
|
||||
const result = await once(stream, 'data')
|
||||
assert.equal(result.hasOwnProperty('time'), false)
|
||||
assert.equal(result.msg, 'foobar')
|
||||
})
|
||||
|
||||
test('pino.stdTimeFunctions.unixTime returns seconds based timestamps', async () => {
|
||||
const opts = {
|
||||
timestamp: pino.stdTimeFunctions.unixTime
|
||||
}
|
||||
const stream = sink()
|
||||
const instance = pino(opts, stream)
|
||||
const now = Date.now
|
||||
Date.now = () => 1531069919686
|
||||
instance.info('foobar')
|
||||
const result = await once(stream, 'data')
|
||||
assert.equal(result.hasOwnProperty('time'), true)
|
||||
assert.equal(result.time, 1531069920)
|
||||
Date.now = now
|
||||
})
|
||||
|
||||
test('pino.stdTimeFunctions.isoTime returns ISO 8601 timestamps', async () => {
|
||||
const opts = {
|
||||
timestamp: pino.stdTimeFunctions.isoTime
|
||||
}
|
||||
const stream = sink()
|
||||
const instance = pino(opts, stream)
|
||||
const ms = 1531069919686
|
||||
const now = Date.now
|
||||
Date.now = () => ms
|
||||
const iso = new Date(ms).toISOString()
|
||||
instance.info('foobar')
|
||||
const result = await once(stream, 'data')
|
||||
assert.equal(result.hasOwnProperty('time'), true)
|
||||
assert.equal(result.time, iso)
|
||||
Date.now = now
|
||||
})
|
||||
+40
@@ -0,0 +1,40 @@
|
||||
'use strict'
|
||||
|
||||
const test = require('node:test')
|
||||
const proxyquire = require('proxyquire')
|
||||
const tspl = require('@matteo.collina/tspl')
|
||||
|
||||
test('should import', async (t) => {
|
||||
const plan = tspl(t, { plan: 2 })
|
||||
const mockRealRequire = (target) => {
|
||||
return {
|
||||
default: {
|
||||
default: () => {
|
||||
plan.equal(target, 'pino-pretty')
|
||||
return Promise.resolve()
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
const mockRealImport = async () => {
|
||||
await Promise.resolve()
|
||||
throw Object.assign(new Error(), { code: 'ERR_MODULE_NOT_FOUND' })
|
||||
}
|
||||
|
||||
const loadTransportStreamBuilder = proxyquire(
|
||||
'../lib/transport-stream.js',
|
||||
{
|
||||
'real-require': {
|
||||
realRequire: mockRealRequire,
|
||||
realImport: mockRealImport
|
||||
}
|
||||
}
|
||||
)
|
||||
|
||||
const fn = await loadTransportStreamBuilder('pino-pretty')
|
||||
|
||||
await fn()
|
||||
plan.ok('returned promise resolved')
|
||||
|
||||
await plan
|
||||
})
|
||||
Loaded 100 of 415 files, more files were not shown because too many files have changed in this diff.
Show more
Reference in new issue
Block a user