1. 程式人生 > >winston自定義日誌管理

winston自定義日誌管理

mod 自定義 req pos div debug config nbsp error:

logger.js

const winston = require(winston);
const moment = require(moment);
const config = require(../conf/config);
let customLevels = {
    levels: {
        debug: 0,
        info: 1,
        warn: 2,
        error: 3
    },
    colors: {
        debug: blue,
        info: green,
        warn: 
yellow, error: red } }; // create the main logger 等級高的包含等級低的輸出日誌 module.exports = logger = new (winston.Logger)({ level: debug, levels: customLevels.levels, transports: [ new (winston.transports.Console)({ level: error, levels: customLevels.levels, timestamp: function(){
return moment().format(YYYY-MM-DD HH:mm:ss)}, colorize: true, silent: config.logStdout //true關閉,false打開 }), new (winston.transports.File)({ name: info, filename: ./logs/info.log, //項目根目錄 maxsize: 1024 * 1024 * 50, //50M level:
info, levels: customLevels.levels, timestamp: function(){return moment().format(YYYY-MM-DD HH:mm:ss)}, json: false }) ] }) winston.addColors(customLevels.colors);

使用:

#引入
const logger = require(./util/logger);
#輸出
logger.debug(事件消息: + JSON.stringify(message));
logger.info(事件消息: + JSON.stringify(message));
logger.warn(事件消息: + JSON.stringify(message));
logger.error(事件消息: + JSON.stringify(message));

查看:

技術分享圖片

技術分享圖片

winston自定義日誌管理