简体   繁体   中英

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"}

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(),
    ),
  });
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM