简体   繁体   中英

Winston : How to get logging with timestamp at the front

How do configure Winston to output log like

2019-06-30 17:39:07 : error : Cheese has rotten

I tried this but it is outputting timestamp at the end

const logger = winston.createLogger({
    format: winston.format.combine(
        winston.format.timestamp({format: 'YYYY-MM-DD HH:mm:ss'}),
        winston.format.simple()
    )
});

error: Cheese has rotten! {"timestamp":"2019-06-30 23:32:02"}

try the below code. I hope this will solve the issue. You can customize in your own required way in myFormat custom function

const myFormat = printf(({ level, message, label, timestamp }) => {
  return `${timestamp} : [${label}] : ${level}: ${message}`;
});

const logger = createLogger({
  format: combine(
    label({ label: 'right meow!' }),
    timestamp(),
    myFormat
  ),
  transports: [new transports.Console()]
});

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