Access request headers inside custom logger #5001
-
Hello! I wish to pass a custom logger to Fastify (majorly to customise or have full control over how logs are presented). This is what I currently have: export class Logger {
level : string
constructor(level : string = '30') {
this.level = level
}
child() {
return new Logger()
}
debug(message) {
// here I wish to access request headers
}
error() {}
fatal() {}
info(message) {
console.log('info:', message)
}
silent() (}
trace() {}
warn() {}
} Inside my if (request.headers['x-nf-api-debug-logging']?.toString() === '1') {
console.log(message}
} This way, I could simply pass a debug header to my API whenever I wish to see some extra logging. I know that, in my request, I could possibly pass an extra param like: request.log.debug('debug logging', request.headers['x-nf-api-debug-logging']) and compare that in my method. However, I also need to be able to use debug logging outside of the request (for example, when signing and unsigning the cookie) and I cannot access the request headers there. When initializing Fastify, I've passed the I'll be deploying this as an API for my application on Netlify Functions (using AWS Lambda under-the-hood), in case that makes a
Is there a way to achieve this? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
You can always pass a custom instance of Your use case seems to work perfectly with either |
Beta Was this translation helpful? Give feedback.
-
Finally, found a way to achieve what I wanted! As mentioned, the only reason I wanted to access request headers inside custom logger was to dynamically change log level based on request headers. So here's my new Logger class: const logLevels = [
'debug',
'error',
'fatal',
'info',
'silent',
'trace',
'warn'
] as const
type LogLevel = typeof logLevels[number]
export class Logger {
#_level : LogLevel
#levelNumber : number
#levelToNumber : Record<LogLevel, number> = {
debug: 20,
error: 50,
info: 30,
fatal: 60,
trace: 10,
silent: 0,
warn: 40
}
child() {
return new Logger()
}
constructor(level : LogLevel = 'info') {
this.#_level = level
this.#levelNumber = this.#levelToNumber[this.#_level]
}
debug(msg : string) {
if (this.#levelNumber && this.#levelNumber <= this.#levelToNumber.debug) {
console.debug(msg)
}
}
error(msg : string) {
if (this.#levelNumber && this.#levelNumber <= this.#levelToNumber.error) {
console.error(msg)
}
}
get level() {
return this.#_level
}
fatal(msg : string) {
if (this.#levelNumber && this.#levelNumber <= this.#levelToNumber.fatal) {
console.error(msg)
}
}
info(msg : string) {
if (this.#levelNumber && this.#levelNumber <= this.#levelToNumber.info) {
console.info(msg)
}
}
set level(newLevel) {
this.#_level = newLevel
this.#levelNumber = this.#levelToNumber[this.#_level]
}
silent() {}
trace(msg : string) {
if (this.#levelNumber && this.#levelNumber <= this.#levelToNumber.trace) {
console.trace(msg)
}
}
warn(msg : string) {
if (this.#levelNumber && this.#levelNumber <= this.#levelToNumber.warn) {
console.warn(msg)
}
}
} and in my Fastify-side of things, I have: fastify.addHook('onRequest', (req, _res, done) => {
const logLevelHeader = req.headers['x-log-level']
if (logLevels.includes(logLevelHeader)) {
req.log.level = logLevelHeader
}
done()
}) Now, simply making a request to my API with a header like |
Beta Was this translation helpful? Give feedback.
Finally, found a way to achieve what I wanted! As mentioned, the only reason I wanted to access request headers inside custom logger was to dynamically change log level based on request headers. So here's my new Logger class: