-
Notifications
You must be signed in to change notification settings - Fork 878
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: send config to transports on init (#1930)
* feat: send config to transports on init * test: integration for transports using pino config
- Loading branch information
1 parent
d9911b0
commit 5ceb596
Showing
10 changed files
with
297 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
'use strict' | ||
|
||
const build = require('pino-abstract-transport') | ||
const { pipeline, Transform } = require('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 | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
'use strict' | ||
|
||
const { parentPort, workerData } = require('worker_threads') | ||
const { Writable } = require('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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,167 @@ | ||
'use strict' | ||
|
||
const os = require('os') | ||
const { join } = require('path') | ||
const { readFile } = require('fs').promises | ||
const writeStream = require('flush-write-stream') | ||
const { watchFileCreated, file } = require('../helper') | ||
const { test } = require('tap') | ||
const pino = require('../../') | ||
|
||
const { pid } = process | ||
const hostname = os.hostname() | ||
|
||
function serializeError (error) { | ||
return { | ||
type: error.name, | ||
message: error.message, | ||
stack: error.stack | ||
} | ||
} | ||
|
||
function parseLogs (buffer) { | ||
return JSON.parse(`[${buffer.toString().replace(/}{/g, '},{')}]`) | ||
} | ||
|
||
test('transport uses pino config', async ({ same, teardown, plan }) => { | ||
plan(1) | ||
const destination = file() | ||
const transport = pino.transport({ | ||
pipeline: [{ | ||
target: join(__dirname, '..', 'fixtures', 'transport-uses-pino-config.js') | ||
}, { | ||
target: 'pino/file', | ||
options: { destination } | ||
}] | ||
}) | ||
teardown(transport.end.bind(transport)) | ||
const instance = pino({ | ||
messageKey: 'customMessageKey', | ||
errorKey: 'customErrorKey', | ||
customLevels: { custom: 35 } | ||
}, transport) | ||
|
||
const error = new Error('bar') | ||
instance.custom('foo') | ||
instance.error(error) | ||
await watchFileCreated(destination) | ||
const result = parseLogs(await readFile(destination)) | ||
|
||
same(result, [{ | ||
severityText: 'custom', | ||
body: 'foo', | ||
attributes: { | ||
pid, | ||
hostname | ||
} | ||
}, { | ||
severityText: 'error', | ||
body: 'bar', | ||
attributes: { | ||
pid, | ||
hostname | ||
}, | ||
error: serializeError(error) | ||
}]) | ||
}) | ||
|
||
test('transport uses pino config without customizations', async ({ same, teardown, plan }) => { | ||
plan(1) | ||
const destination = file() | ||
const transport = pino.transport({ | ||
pipeline: [{ | ||
target: join(__dirname, '..', 'fixtures', 'transport-uses-pino-config.js') | ||
}, { | ||
target: 'pino/file', | ||
options: { destination } | ||
}] | ||
}) | ||
teardown(transport.end.bind(transport)) | ||
const instance = pino(transport) | ||
|
||
const error = new Error('qux') | ||
instance.info('baz') | ||
instance.error(error) | ||
await watchFileCreated(destination) | ||
const result = parseLogs(await readFile(destination)) | ||
|
||
same(result, [{ | ||
severityText: 'info', | ||
body: 'baz', | ||
attributes: { | ||
pid, | ||
hostname | ||
} | ||
}, { | ||
severityText: 'error', | ||
body: 'qux', | ||
attributes: { | ||
pid, | ||
hostname | ||
}, | ||
error: serializeError(error) | ||
}]) | ||
}) | ||
|
||
test('transport uses pino config with multistream', async ({ same, teardown, plan }) => { | ||
plan(2) | ||
const destination = file() | ||
const messages = [] | ||
const stream = writeStream(function (data, enc, cb) { | ||
const message = JSON.parse(data) | ||
delete message.time | ||
messages.push(message) | ||
cb() | ||
}) | ||
const transport = pino.transport({ | ||
pipeline: [{ | ||
target: join(__dirname, '..', 'fixtures', 'transport-uses-pino-config.js') | ||
}, { | ||
target: 'pino/file', | ||
options: { destination } | ||
}] | ||
}) | ||
teardown(transport.end.bind(transport)) | ||
const instance = pino({ | ||
messageKey: 'customMessageKey', | ||
errorKey: 'customErrorKey', | ||
customLevels: { custom: 35 } | ||
}, pino.multistream([transport, { stream }])) | ||
|
||
const error = new Error('buzz') | ||
const serializedError = serializeError(error) | ||
instance.custom('fizz') | ||
instance.error(error) | ||
await watchFileCreated(destination) | ||
const result = parseLogs(await readFile(destination)) | ||
|
||
same(result, [{ | ||
severityText: 'custom', | ||
body: 'fizz', | ||
attributes: { | ||
pid, | ||
hostname | ||
} | ||
}, { | ||
severityText: 'error', | ||
body: 'buzz', | ||
attributes: { | ||
pid, | ||
hostname | ||
}, | ||
error: serializedError | ||
}]) | ||
|
||
same(messages, [{ | ||
level: 35, | ||
pid, | ||
hostname, | ||
customMessageKey: 'fizz' | ||
}, { | ||
level: 50, | ||
pid, | ||
hostname, | ||
customErrorKey: serializedError, | ||
customMessageKey: 'buzz' | ||
}]) | ||
}) |