Skip to content

Synchronous logger lib (Winston) can't use headers() function anymore now it's async #74491

Answered by clemp6r
clemp6r asked this question in App Router
Discussion options

You must be logged in to vote

I may have found a solution by wrapping the logger:

export default {
  debug(message: string, ...meta: any[]) {
    logWithHeaders("debug", message, ...meta);
  },
  info(message: string, ...meta: any[]) {
    logWithHeaders("info", message, ...meta);
  },
  warn(message: string, ...meta: any[]) {
    logWithHeaders("warn", message, ...meta);
  },
  error(message: string, ...meta: any[]) {
    logWithHeaders("error", message, ...meta);
  },
};

function logWithHeaders(level: string, message: string, ...meta: any[]) {
  let headersPromise: Promise<Headers | undefined>;
  try {
    headersPromise = headers().catch(() => undefined);
  } catch (e) {
    headersPromise = Promise.resolve(undefined

Replies: 1 comment

Comment options

You must be logged in to vote
0 replies
Answer selected by clemp6r
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
1 participant