亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在節點js的lof文件中打印日志時顯示當前日期和時間

在節點js的lof文件中打印日志時顯示當前日期和時間

呼啦一陣風 2021-10-21 16:52:22
你能告訴我如何在日志中打印/寫入文件時添加時間戳嗎?這意味著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"}
查看完整描述

2 回答

?
瀟瀟雨雨

TA貢獻1833條經驗 獲得超4個贊

希望以下解決方案對您有用。


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'

    }),

  ],

});


查看完整回答
反對 回復 2021-10-21
  • 2 回答
  • 0 關注
  • 261 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號