Skip to content

Conversation

@2013xile
Copy link

@2013xile 2013xile commented May 18, 2024

Add error.cause when cloning error info in logger.child()

@2013xile 2013xile marked this pull request as ready for review May 18, 2024 04:46
@HenriqueSilverio
Copy link

Just tried to run it here and got "cause":{}.

Runtime

  • Node.js: 22.12.0
  • npm: 11.0.0

Minimum Working Example

const winston = require('winston')

const logger = winston.createLogger({
  format: winston.format.combine(
    winston.format.json(),
  ),
  transports: [
    new winston.transports.Console(),
  ],
})

const childLogger = logger.child()

const error = new Error('Something went wrong', { cause: new Error('The devil is in the details') })

childLogger.error(error)

Output

{"cause":{},"level":"error","message":"Something went wrong","stack":"Error: Something went wrong\n    at Object.<anonymous> (/home/Code/winston-debug/index.js:14:15)\n    at Module._compile (node:internal/modules/cjs/loader:1546:14)\n    at Object..js (node:internal/modules/cjs/loader:1689:10)\n    at Module.load (node:internal/modules/cjs/loader:1318:32)\n    at Function._load (node:internal/modules/cjs/loader:1128:12)\n    at TracingChannel.traceSync (node:diagnostics_channel:315:14)\n    at wrapModuleLoad (node:internal/modules/cjs/loader:218:24)\n    at Function.executeUserEntryPoint [as runMain] (node:internal/modules/run_main:170:5)\n    at node:internal/main/run_main_module:36:49"}

@2013xile
Copy link
Author

Hi @HenriqueSilverio ,
This is because the default console transport doesn’t output the cause information. I recommend to customize a transport. For example:

const winston = require('./lib/winston')

class CustomTransport extends winston.Transport {
  log(info, callback) {
    const { level, message, stack, cause } = info;
    console.error({
      level,
      message,
      stack,
    });
    if (cause) {
      console.error({
        level,
        message: cause.message,
        stack: cause.stack
      })
    }
    callback(null, true);
  }
}

const logger = winston.createLogger({
  format: winston.format.combine(
    winston.format.json(),
  ),
  transports: [
    new CustomTransport(),
  ],
})

const childLogger = logger.child()

const error = new Error('Something went wrong', { cause: new Error('The devil is in the details') })

childLogger.error(error)

@HenriqueSilverio
Copy link

Hi @2013xile!
Maybe I'm missing something, but if we need to create a custom transport for the error to show, what exactly is the PR fixing?

@2013xile
Copy link
Author

2013xile commented Dec 25, 2024

Hi @2013xile! Maybe I'm missing something, but if we need to create a custom transport for the error to show, what exactly is the PR fixing?

Without the changes in this PR, even a custom transport cannot retrieve the cause object. The console transport cannot output the cause information because it does not parse the cause object. I prefer not to modify this, as everyone may have different requirements for how the cause information is output.

@HenriqueSilverio
Copy link

Without the changes in this PR, even a custom transport cannot retrieve the cause object.

Got it! I've tried here and it worked as you said. Thanks for clarifying that!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants