Created
November 26, 2019 02:05
-
-
Save miguelmota/4df504cff4bfebcff982dd06bde7a34a to your computer and use it in GitHub Desktop.
Node.js pino logger show caller filename and line number
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
import path from 'path' | |
import pino from 'pino' | |
const STACKTRACE_OFFSET = 2 | |
const LINE_OFFSET = 7 | |
const { symbols : { asJsonSym} } = pino | |
function traceCaller (pinoInstance) { | |
const get = (target, name) => name === asJsonSym ? asJson : target[name] | |
function asJson (...args) { | |
args[0] = args[0] || Object.create(null) | |
args[0].caller = Error().stack.split('\n').filter(s => !s.includes('node_modules/pino') && !s.includes('node_modules\\pino'))[STACKTRACE_OFFSET].substr(LINE_OFFSET).replace(path.resolve(__dirname, '..'), '') | |
return pinoInstance[asJsonSym].apply(this, args) | |
} | |
return new Proxy(pinoInstance, { get }) | |
} | |
const logger = traceCaller(pino({ | |
level: 'debug', | |
prettyPrint: { | |
colorize: true, | |
} | |
})) |
Love this! Thanks - thanks - thanks! 👍
As a node ESM version, I had issues with __dirname, also substr() should be replaced by substring()
I'm using dirname(import.meta.url)
as a workaround, if in a subfolder, you can call dirname twice
Here's my take on traceCaller, I also modernized the pino instance creation since "prettyPrint" is deprecated.
import { pino } from 'pino'
import { dirname } from 'path'
const STACKTRACE_OFFSET = 2
const LINE_OFFSET = 7
const { symbols: { asJsonSym } } = pino
function traceCaller (pinoInstance) {
const get = (target, name) => name === asJsonSym ? asJson : target[name]
function asJson (...args) {
args[0] = args[0] || Object.create(null)
args[0].caller = Error().stack.split('\n')
.filter(s => !s.includes('node_modules/pino') && !s.includes('node_modules\\pino'))[STACKTRACE_OFFSET]
.substring(LINE_OFFSET).replace(dirname(import.meta.url) + '/', '')
return pinoInstance[asJsonSym].apply(this, args)
}
return new Proxy(pinoInstance, { get })
}
const logger = traceCaller(pino({
level: 'debug',
transport: {
target: 'pino-pretty',
options: {
ignore: 'time,pid,hostname'
}
}
}))
I wrote a version for Node which provides more information: https://gist.github.com/Caellian/53c91f78f9f54146edc73b4200cd4775
I needed the whole trace and have it be structured. Only works for Node (didn't test in browsers).
But in general, pino allows mixins (now?), so it's better to use those than wrapping the logger:
pino({
// rest
mixin: (base, level, logger) => {
return { /* additional data; added from each log statement */ };
},
}),
This makes it much more convenient to add trace information.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Not so scalable solution, this doesnt work if pino actual methods(info, child etc) are inside some wrappers, sure in such cases it logs line and file of an actual wrapper