question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

format.errors not working on transport level

See original GitHub issue

Please tell us about your environment:

  • winston version?
    • winston@2
    • winston@3
  • node -v outputs: v12.16.1
  • Operating System? macOS
  • Language? TypeScript ^3.9.7

What is the problem?

When I use format.errors({stack: true}) on logger configuration level, it works as expected. The logger parses JavaScript Error object and prints correctly.

import { createLogger, format, transports } from 'winston';

const logFormatter = format.printf((info) => {
  let { timestamp, level, stack, message } = info;
  message = stack || message;
  return `${timestamp} ${level}: ${message}`;
});

const logToConsole = createLogger({
  level: 'info',
  format: format.errors({ stack: true }),
  transports: [
    new transports.Console({
      format: format.combine(format.colorize(), format.simple(), format.timestamp(), logFormatter),
    }),
  ],
});

const err = new Error('network');
logToConsole.error(err);

Output:

2021-01-22T06:55:25.500Z error: Error: network
    at Object.<anonymous> (/Users/ldu020/workspace/github.com/mrdulin/expressjs-research/src/stackoverflow/65822479/index.ts:19:13)
    at Module._compile (internal/modules/cjs/loader.js:1158:30)
    at Module.m._compile (/Users/ldu020/workspace/github.com/mrdulin/expressjs-research/node_modules/ts-node/src/index.ts:858:23)
    at Module._extensions..js (internal/modules/cjs/loader.js:1178:10)
    at Object.require.extensions.<computed> [as .ts] (/Users/ldu020/workspace/github.com/mrdulin/expressjs-research/node_modules/ts-node/src/index.ts:861:12)
    at Module.load (internal/modules/cjs/loader.js:1002:32)
    at Function.Module._load (internal/modules/cjs/loader.js:901:14)
    at Function.executeUserEntryPoint [as runMain] (internal/modules/run_main.js:74:12)
    at main (/Users/ldu020/workspace/github.com/mrdulin/expressjs-research/node_modules/ts-node/src/bin.ts:227:14)
    at Object.<anonymous> (/Users/ldu020/workspace/github.com/mrdulin/expressjs-research/node_modules/ts-node/src/bin.ts:513:3)

I think the format.errors({ stack: true }) can be used on transport level too. But it doesn’t.

import { createLogger, format, transports } from 'winston';

const logFormatter = format.printf((info) => {
  let { timestamp, level, stack, message } = info;
  message = stack || message;
  return `${timestamp} ${level}: ${message}`;
});

const logToConsole = createLogger({
  level: 'info',
  // format: format.errors({ stack: true }),
  transports: [
    new transports.Console({
      format: format.combine(
        format.errors({ stack: true }),
        format.colorize(),
        format.simple(),
        format.timestamp(),
        logFormatter,
      ),
    }),
  ],
});

const err = new Error('network');
logToConsole.error(err);

Output:

2021-01-22T06:58:00.582Z error: undefined

What do you expect to happen instead?

I think the configuration of the logger level is a global configuration, and the configuration of the transport level is the configuration for each transport, and has the highest priority, covering the global configuration.

They are should have the same output.

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:3
  • Comments:5

github_iconTop GitHub Comments

1reaction
MinusFourcommented, Mar 10, 2022

The winston-transport module “clones” the info object being formatted:

https://github.com/winstonjs/winston-transport/blob/e4a6ce1a0b788d4fe56b4be94c30a3bebe96c2e2/index.js#L90-L94

This leads to only copying enumerable properties, which on Error objects not even message is. So the object itself is pretty much blank at that point (at the very least devoid of any Error properties).

Since the error formatter is expecting an Error (or at least an error object inside a message property) it does nothing.

An alternative that could work as it is right now:

logToConsole.error({
   message: err
});

Which I guess you can monkey patch on the class instance.

const logError = logToConsole.error.bind(logToConsole);
logToConsole.error = (message) => logError({ message });

I’ll just point out that the log formatter (that is, the one that’s on the logger itself) does not receive a copy of an object but the object itself.

1reaction
OktarinTentakelcommented, Sep 15, 2021

Any news on this?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Winston 3 + Loopback: Error messages are shown in access.log
The error.log and access.log files are created just fine. The error.log is not showing any info messages as expected. However, the access.log ...
Read more >
A Guide Into Logging in Node.js using Winston - Bird Eats Bug
Learn the concept of logging in the context of Node.js apps using Winston and how to send application logs to Amazon's CloudWatch.
Read more >
How to use the winston.format.printf function in winston - Snyk
To help you get started, we've selected a few winston.format.printf examples, based on popular ways it is used in public projects.
Read more >
logging in JavaScript with Winston.js - ZetCode
Logging is not limited to identifying errors in software development. ... providing information in case of problems, finding application ...
Read more >
winstonjs/winston - Gitter
Console( { level: 'error', format: winston.format.combine( ... <3.0.0 version or just doesn't work, including these issues on GitHub: winstonjs/winston#315 ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found