Search code examples
javascriptnode.jstypescriptloggingwinston

Winston not logging debug levels in node.js


I am not running this on VS Code, just a normal terminal.

When I pass an object with different levels to winston they are all output as expected - except debug

import { Logger, format, createLogger, transports } from "winston";

export const exampleLogger = (): Logger => {
  return createLogger({
    transports: [new transports.Console()],
    format: format.combine(
      format((info) => {
        info.level = info.level.toUpperCase();
        return info;
      })(),
      format.json(),
    ),
  });
}
import { exampleLogger } from './example'

const logger = exampleLogger();

const infoExample = {
  timestamp: '2021-06-25T07:06:03.901Z',
  level: 'info',
  message: 'this works fine'
}
logger.log(infoExample)

const debugExample = {
  timestamp: '2021-06-25T07:06:03.901Z',
  level: 'debug',
  message: 'this outputs nothing'
}
logger.log(debugExample)

const errorExample = {
  timestamp: '2021-06-25T07:06:03.901Z',
  level: 'error',
  message: 'this works fine'
}
logger.log(errorExample)

Execution

$ npx ts-node test.ts
{"timestamp":"2021-06-25T07:06:03.901Z","level":"INFO","message":"this works fine"}
{"timestamp":"2021-06-25T07:06:03.901Z","level":"ERROR","message":"this works fine"}

Solution

  • Ok, looks like I misunderstood the documentation about default logging levels. As stated in this issue, I need to tell winston that it should output certain log levels

    import { Logger, format, createLogger, transports } from "winston";
    
    export const exampleLogger = (): Logger => {
      return createLogger({
        transports: [new transports.Console({ level: 'debug' })],
        format: format.combine(
          format((info) => {
            info.level = info.level.toUpperCase();
            return info;
          })(),
          format.json(),
        ),
      });
    }