Skip to content

Access request headers inside custom logger #5001

Closed Answered by hrishikesh-k
hrishikesh-k asked this question in Q&A
Discussion options

You must be logged in to vote

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 = l…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
2 replies
@hrishikesh-k
Comment options

@hrishikesh-k
Comment options

Comment options

You must be logged in to vote
0 replies
Answer selected by hrishikesh-k
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants