简体   繁体   中英

How can I get a custom output using winston in combination with simple()?

I am trying to get the following format using winston: 2020-01-01 [INFO]: test having the level part colorized, instead I am getting

info: test

How can I get the format I am looking for?

import winston from "winston";

const format = winston.format.combine(
  winston.format.align(),
  winston.format.colorize(),
  winston.format.errors({ stack: true }),
  winston.format.printf(
    ({ level, message }) => `[${level.toUpperCase()}]: ${message}`
  ),
  winston.format.simple(),
  winston.format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" })
);

export const logger = winston.createLogger({
  level: process.env.NODE_ENV === "production" ? "info" : "debug",
  transports: [new winston.transports.Console({ format })],
});

Fixed using the following

import winston from "winston";

const format = winston.format.combine(
  winston.format(info => ({ ...info, level: info.level.toUpperCase() }))(),
  winston.format.align(),
  winston.format.colorize(),
  winston.format.errors({ stack: true }),
  winston.format.prettyPrint(),
  winston.format.simple(),
  winston.format.splat(),
  winston.format.timestamp({ format: "YYYY-MM-DD HH:mm:ss" }),
  winston.format.printf(
    ({ timestamp, level, message }) => `${timestamp} [${level}]: ${message}`
  )
);

export const logger = winston.createLogger({
  level: process.env.NODE_ENV === "production" ? "info" : "debug",
  transports: [new winston.transports.Console({ format })],
});

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