在节点js的lof文件中打印日志时显示当前日期和时间

你能告诉我如何在日志中打印/写入文件时添加时间戳吗?这意味着it shows current data and time在文件中打印日志时。


我试过这样,但没有显示正确的输出。我加了这个


const consoleLogger = new winston.transports.Console({

  timestamp: function() {

    const today = moment();

    return today.format("DD-MM-YYYY");

  },

  format: winston.format.json(),

  colorize: true,

  level: "debug"

});

还是行不通


https://codesandbox.io/s/beautiful-kapitsa-j1sku


没有时间戳的当前输出


{"message":"running → PORT (src/index.js:10)","level":"info"}


呼啦一阵风
浏览 231回答 2
2回答

潇潇雨雨

希望以下解决方案对您有用。const { createLogger, format, transports } = require('winston');const { combine, timestamp, printf} = format;const transport = new transports.Console({  exitOnError: false,  format: combine(    ...(environment === 'dev' ? [format.colorize()] : []),    timestamp(),    printf(info => `${info.timestamp} [${info.level}]: ${info.message}`)  ),  colorize: true,  level: 'debug',});const myFormat = printf(({ level, message, timestamp }) => {  return `${timestamp} ${level}: ${message}`;});const logger = createLogger({  format: combine(timestamp(), appendTimestamp({ tz: 'Add your timezone'}), myFormat),  transports: [transport],  exceptionHandlers: [    new transports.File({      filename: 'FILE_NAME.log'    }),  ],});
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript