1:先安裝log4js模塊 html
npm install log4js npm
目前安裝的版本信息:"log4js": "^3.0.5"app
2:引用及配置ui
var log4js = require('log4js'); // log4js的輸出級別6個: trace, debug, info, warn, error, fatal log4js.configure({ //輸出位置的基本信息設置 appenders: { //設置控制檯輸出 (默認日誌級別是關閉的(即不會輸出日誌)) out: { type: 'console' }, //設置天天:以日期爲單位,數據文件類型,dataFiel 注意設置pattern,alwaysIncludePattern屬性 //allLog: { type: 'dateFile', filename: './log/all', pattern: '-yyyy-MM-dd.log', alwaysIncludePattern: true }, //全部日誌記錄,文件類型file 文件最大值maxLogSize 單位byte (B->KB->M) backups:備份的文件個數最大值,最新數據覆蓋舊數據 allLog: { type: 'file', filename: './log/all.log', keepFileExt: true, maxLogSize: 10485760, backups: 3 }, //http請求日誌 http請求日誌須要app.use引用一下, 這樣纔會自動記錄每次的請求信息 httpLog: { type: "dateFile", filename: "log/httpAccess.log", pattern: ".yyyy-MM-dd", keepFileExt: true }, //錯誤日誌 type:過濾類型logLevelFilter,將過濾error日誌寫進指定文件 errorLog: { type: 'file', filename: './log/error.log' }, error: { type: "logLevelFilter", level: "error", appender: 'errorLog' } }, //不一樣等級的日誌追加到不一樣的輸出位置:appenders: ['out', 'allLog'] categories 做爲getLogger方法的鍵名對應 categories: { //appenders:採用的appender,取上面appenders項,level:設置級別 http: { appenders: ['out', 'httpLog'], level: "debug" }, default: { appenders: ['out', 'allLog', 'error'], level: 'debug' }, //error寫入時是通過篩選後留下的 } }); //getLogger參數取categories項,爲空或者其餘值取default默認項 // exports.getLogger = function (name) { // return log4js.getLogger(name || 'default') // } const logger = log4js.getLogger("liveMeeting"); const httpLog = log4js.getLogger('http'); const httpLogger = log4js.connectLogger(httpLog, { level: 'WARN' }); exports = module.exports = { logger, httpLogger };
http請求日誌 http請求日誌須要app.use引用一下, 這樣纔會自動記錄每次的請求信息 :
global.logger = require('./config/log4js').logger; httpLogger = require('./config/log4js').httpLogger; //這樣會自動記錄每次請求信息,放在其餘use上面 app.use(httpLogger);
logger用來記錄全部手動輸入的日誌,如:spa