Skip to content

Commit 18cb39d

Browse files
test: integration for transports using pino config
1 parent db7eb02 commit 18cb39d

File tree

3 files changed

+202
-2
lines changed

3 files changed

+202
-2
lines changed

package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -104,14 +104,14 @@
104104
"atomic-sleep": "^1.0.0",
105105
"fast-redact": "^3.1.1",
106106
"on-exit-leak-free": "^2.1.0",
107-
"pino-abstract-transport": "^1.1.0",
107+
"pino-abstract-transport": "^1.2.0",
108108
"pino-std-serializers": "^6.0.0",
109109
"process-warning": "^3.0.0",
110110
"quick-format-unescaped": "^4.0.3",
111111
"real-require": "^0.2.0",
112112
"safe-stable-stringify": "^2.3.1",
113113
"sonic-boom": "^3.7.0",
114-
"thread-stream": "^2.0.0"
114+
"thread-stream": "^2.6.0"
115115
},
116116
"tsd": {
117117
"directory": "test/types"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
'use strict'
2+
3+
const build = require('pino-abstract-transport')
4+
const { pipeline, Transform } = require('stream')
5+
module.exports = () => {
6+
return build(function (source) {
7+
const myTransportStream = new Transform({
8+
autoDestroy: true,
9+
objectMode: true,
10+
transform (chunk, enc, cb) {
11+
const {
12+
time,
13+
level,
14+
[source.messageKey]: body,
15+
[source.errorKey]: error,
16+
...attributes
17+
} = chunk
18+
this.push(JSON.stringify({
19+
severityText: source.levels.labels[level],
20+
body,
21+
attributes,
22+
...(error && { error })
23+
}))
24+
cb()
25+
}
26+
})
27+
pipeline(source, myTransportStream, () => {})
28+
return myTransportStream
29+
}, {
30+
enablePipelining: true,
31+
expectPinoConfig: true
32+
})
33+
}
+167
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,167 @@
1+
'use strict'
2+
3+
const os = require('os')
4+
const { join } = require('path')
5+
const { readFile } = require('fs').promises
6+
const writeStream = require('flush-write-stream')
7+
const { watchFileCreated, file } = require('../helper')
8+
const { test } = require('tap')
9+
const pino = require('../../')
10+
11+
const { pid } = process
12+
const hostname = os.hostname()
13+
14+
function serializeError (error) {
15+
return {
16+
type: error.name,
17+
message: error.message,
18+
stack: error.stack
19+
}
20+
}
21+
22+
function parseLogs (buffer) {
23+
return JSON.parse(`[${buffer.toString().replace(/}{/g, '},{')}]`)
24+
}
25+
26+
test('transport uses pino config', async ({ same, teardown, plan }) => {
27+
plan(1)
28+
const destination = file()
29+
const transport = pino.transport({
30+
pipeline: [{
31+
target: join(__dirname, '..', 'fixtures', 'transport-uses-pino-config.js')
32+
}, {
33+
target: 'pino/file',
34+
options: { destination }
35+
}]
36+
})
37+
teardown(transport.end.bind(transport))
38+
const instance = pino({
39+
messageKey: 'customMessageKey',
40+
errorKey: 'customErrorKey',
41+
customLevels: { custom: 35 }
42+
}, transport)
43+
44+
const error = new Error('bar')
45+
instance.custom('foo')
46+
instance.error(error)
47+
await watchFileCreated(destination)
48+
const result = parseLogs(await readFile(destination))
49+
50+
same(result, [{
51+
severityText: 'custom',
52+
body: 'foo',
53+
attributes: {
54+
pid,
55+
hostname
56+
}
57+
}, {
58+
severityText: 'error',
59+
body: 'bar',
60+
attributes: {
61+
pid,
62+
hostname
63+
},
64+
error: serializeError(error)
65+
}])
66+
})
67+
68+
test('transport uses pino config without customizations', async ({ same, teardown, plan }) => {
69+
plan(1)
70+
const destination = file()
71+
const transport = pino.transport({
72+
pipeline: [{
73+
target: join(__dirname, '..', 'fixtures', 'transport-uses-pino-config.js')
74+
}, {
75+
target: 'pino/file',
76+
options: { destination }
77+
}]
78+
})
79+
teardown(transport.end.bind(transport))
80+
const instance = pino(transport)
81+
82+
const error = new Error('qux')
83+
instance.info('baz')
84+
instance.error(error)
85+
await watchFileCreated(destination)
86+
const result = parseLogs(await readFile(destination))
87+
88+
same(result, [{
89+
severityText: 'info',
90+
body: 'baz',
91+
attributes: {
92+
pid,
93+
hostname
94+
}
95+
}, {
96+
severityText: 'error',
97+
body: 'qux',
98+
attributes: {
99+
pid,
100+
hostname
101+
},
102+
error: serializeError(error)
103+
}])
104+
})
105+
106+
test('transport uses pino config with multistream', async ({ same, teardown, plan }) => {
107+
plan(2)
108+
const destination = file()
109+
const messages = []
110+
const stream = writeStream(function (data, enc, cb) {
111+
const message = JSON.parse(data)
112+
delete message.time
113+
messages.push(message)
114+
cb()
115+
})
116+
const transport = pino.transport({
117+
pipeline: [{
118+
target: join(__dirname, '..', 'fixtures', 'transport-uses-pino-config.js')
119+
}, {
120+
target: 'pino/file',
121+
options: { destination }
122+
}]
123+
})
124+
teardown(transport.end.bind(transport))
125+
const instance = pino({
126+
messageKey: 'customMessageKey',
127+
errorKey: 'customErrorKey',
128+
customLevels: { custom: 35 }
129+
}, pino.multistream([transport, { stream }]))
130+
131+
const error = new Error('buzz')
132+
const serializedError = serializeError(error)
133+
instance.custom('fizz')
134+
instance.error(error)
135+
await watchFileCreated(destination)
136+
const result = parseLogs(await readFile(destination))
137+
138+
same(result, [{
139+
severityText: 'custom',
140+
body: 'fizz',
141+
attributes: {
142+
pid,
143+
hostname
144+
}
145+
}, {
146+
severityText: 'error',
147+
body: 'buzz',
148+
attributes: {
149+
pid,
150+
hostname
151+
},
152+
error: serializedError
153+
}])
154+
155+
same(messages, [{
156+
level: 35,
157+
pid,
158+
hostname,
159+
customMessageKey: 'fizz'
160+
}, {
161+
level: 50,
162+
pid,
163+
hostname,
164+
customErrorKey: serializedError,
165+
customMessageKey: 'buzz'
166+
}])
167+
})

0 commit comments

Comments
 (0)