-
Notifications
You must be signed in to change notification settings - Fork 876
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[question] How to "record" logs for testing purposes #1274
Comments
I noticed that what is returned from But I have no guarantees on this, and this could change from one version of pino to another. |
You can keep using const pino = require('pino')
const pretty = require('pino-pretty')
const logger = pino(pretty())
logger.info('hi') |
@mcollina I believe you misunderstood. I do not want to do pinopretty, but rather something else. In pino@6 (and 7), I could do this: const logs = []
const logger = pino({
prettyPrint: true,
prettifier: log => logs.push(log)
})
foo(logger)
expect(logs).to.eql({event: 'dsfjshdf', level: 30, ...}) We used this for testing purposes to see that the function used the logger and output the correct logs (obviously the code below is a simplified version of what we use). Unfortunately, the So...
|
@jsumners perfect! Thank you! |
Another quite raw snippet that always writes all logs to a capped memory buffer. let stream
if (config.pretty || config.logToMemoryForTests) {
const requestedLevel = pino.levels.values[config.level]
pinoConfig.level = 'debug'
stream = pino.destination()
stream[pino.symbols.needsMetadataGsym] = true
const prettifier = config.pretty && require('./dev_string_formatter')()
const memoryDestination = config.logToMemoryForTests && require('./memory-destination')
const origWrite = stream.write
const parse = function (chunk) { try { return JSON.parse(chunk) } catch (err) { return } }
stream.write = function (chunk) {
if (memoryDestination) memoryDestination.write(parse(chunk))
if (this.lastLevel < requestedLevel) return
if (prettifier) {
const obj = parse(chunk)
return origWrite.call(this, obj ? prettifier(obj) : chunk)
} else {
return origWrite.call(this, chunk)
}
}
}
'use strict'
// Singleton memory destination used for testing
module.exports = {
// Array<Object>
// - each object is a parsed pino log entry
logs: [],
// called by pino
write (chunk) {
// crude safety that so no more than 100 logs are in memory
if (this.logs.length >= 100) this.logs.splice(0, 50)
this.logs.push(chunk)
},
reset () {
const current = this.logs
this.logs = []
return current
}
} |
@jsumners Thanks a lot, it works well! |
This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
We have tests in our app that verify that logs were created and have the correct information.
To do that, we need access to the final logs output by pino. The way we do it now is to use the
prettyPrint
option inpino
to get at these final log JSONs and store them in an array that the test can access later.Unfortunately, the
prettyPrint
option is being deprecated, so we need another solution. I looked at transports, and would love to write a transport to do this, but transports run in a worker thread, and there is no way for that worker thread to communicate with the main thread in order for it to pass the logs back to the main thread.So...
(And thanks for a wonderful library!)
The text was updated successfully, but these errors were encountered: