创建log.js
var log4js = require('log4js'); log4js.configure({ appenders: [ { type: 'console', category: "console" }, //控制台输出 { type: "dateFile", filename: 'logs/log.log', pattern: "_yyyy-MM-dd", alwaysIncludePattern: false, category: 'dateFileLog' }//日期文件格式 ], replaceConsole: true, //替换console.log levels:{ dateFileLog: 'INFO' } }); var dateFileLog = log4js.getLogger('dateFileLog'); exports.logger = dateFileLog; exports.use = function(app) { //页面请求日志,用auto的话,默认级别是WARN //app.use(log4js.connectLogger(dateFileLog, {level:'auto', format:':method :url'})); app.use(log4js.connectLogger(dateFileLog, {level:'debug', format:':method :url'})); }
var log = require('./log'); log.use(app);
var logger = require('../log').logger; logger.info("this is log");
Then initial logging would create a file called "blah.log".At midnight, the current "blah.log" file would be renamed to "blah.log-2012-09-26" (for example), and a new "blah.log" file created.