var helper = {}; exports.helper = helper; var path = require(“path”); var log4js = require(‘log4js’); var fs = require(“fs”);
var logDirectory = __dirname + ‘/log’;
// ensure log directory exists fs.existsSync(logDirectory) || fs.mkdirSync(logDirectory);
/*ar strs = fs.readFileSync(path.join(__dirname, ‘log4js.json’),‘utf8’);
console.log(strs); var objConfig = JSON.parse(fs.readFileSync(path.join(__dirname, ‘log4js.json’), “utf8”)); */
log4js.configure({ “customBaseDir” :path.join(__dirname, ‘log/’), “customDefaultAtt” :{ “type”: “dateFile”, “absolute”: true, “alwaysIncludePattern”: true }, “appenders”: [ {“type”: “console”, “category”: “console”}, {“pattern”: “debug/yyyyMMddhh.txt”, “category”: “logDebug”}, {“pattern”: “info/yyyyMMddhh.txt”, “category”: “logInfo”}, {“pattern”: “warn/yyyyMMddhh.txt”, “category”: “logWarn”}, {“pattern”: “err/yyyyMMddhh.txt”, “category”: “logErr”} ], “replaceConsole”: true, “levels”:{ “logDebug”: “DEBUG”, “logInfo”: “DEBUG”, “logWarn”: “DEBUG”, “logErr”: “DEBUG”} });
var logDebug = log4js.getLogger(‘logDebug’); var logInfo = log4js.getLogger(‘logInfo’); var logWarn = log4js.getLogger(‘logWarn’); var logErr = log4js.getLogger(‘logErr’);
helper.writeDebug = function(msg){ if(msg == null) msg = “”; logDebug.debug(msg); };
helper.writeInfo = function(msg){ if(msg == null) msg = “”; logInfo.info(msg); };
helper.writeWarn = function(msg){ if(msg == null) msg = “”; logWarn.warn(msg); };
helper.writeErr = function(msg, exp){ if(msg == null) msg = “”; if(exp != null) msg += “\r\n” + exp; logErr.error(msg); };
// 配合express用的方法 modules.exports.use = function(app) { //页面请求日志, level用auto时,默认级别是WARN app.use(log4js.connectLogger(logInfo, {level:‘debug’, format:’:method :url’})); }
错误:
/usr/local/bin/node bin/www /Users/wuzongbo/node_modules/log4js/lib/log4js.js:291 throw new Error( ^
Error: Problem reading log4js config { customBaseDir: ‘/Users/wuzongbo/WebstormProjects/untitled/logHelper/log/’, customDefaultAtt: { type: ‘dateFile’, absolute: true, alwaysIncludePattern: true }, appenders: [ { type: ‘console’, category: ‘console’, makers: [Object] }, { pattern: ‘debug/yyyyMMddhh.txt’, category: ‘logDebug’ }, { pattern: ‘info/yyyyMMddhh.txt’, category: ‘logInfo’ }, { pattern: ‘warn/yyyyMMddhh.txt’, category: ‘logWarn’ }, { pattern: ‘err/yyyyMMddhh.txt’, category: ‘logErr’ } ], replaceConsole: true, levels: { logDebug: ‘DEBUG’, logInfo: ‘DEBUG’, logWarn: ‘DEBUG’, logErr: ‘DEBUG’ } }. Error was “missing path” (AssertionError: missing path at Module.require (module.js:364:3) at require (module.js:385:17) at requireAppender (/Users/wuzongbo/node_modules/log4js/lib/log4js.js:389:22) at loadAppender (/Users/wuzongbo/node_modules/log4js/lib/log4js.js:405:38) at /Users/wuzongbo/node_modules/log4js/lib/log4js.js:226:7 at Array.forEach (native) at configureAppenders (/Users/wuzongbo/node_modules/log4js/lib/log4js.js:225:18) at configureOnceOff (/Users/wuzongbo/node_modules/log4js/lib/log4js.js:283:7) at Object.configure (/Users/wuzongbo/node_modules/log4js/lib/log4js.js:346:3) at Object.<anonymous> (/Users/wuzongbo/WebstormProjects/untitled/logHelper/logHelper.js:20:8)) at configureOnceOff (/Users/wuzongbo/node_modules/log4js/lib/log4js.js:291:13) at Object.configure (/Users/wuzongbo/node_modules/log4js/lib/log4js.js:346:3) at Object.<anonymous> (/Users/wuzongbo/WebstormProjects/untitled/logHelper/logHelper.js:20:8) at Module._compile (module.js:435:26) at Object.Module._extensions…js (module.js:442:10) at Module.load (module.js:356:32) at Function.Module._load (module.js:311:12) at Module.require (module.js:366:17) at require (module.js:385:17) at Object.<anonymous> (/Users/wuzongbo/WebstormProjects/untitled/app.js:64:11)
Process finished with exit code 1
{
"appenders": [
{
"type": "clustered",
"appenders": [
{
"type": "dateFile",
"filename": "logs/access.log",
"pattern": "-yyyy-MM-dd",
"category": "http"
},
{
"type": "file",
"filename": "logs/app.log",
"maxLogSize": 10485760,
"numBackups": 3
},
{
"type": "logLevelFilter",
"level": "ERROR",
"appender": {
"type": "file",
"filename": "logs/errors.log"
}
}
]
}
]
}
then in app.js
// replace morgan with the log4js connect-logger
log4js.configure('config/log4js.json', { reloadSecs: 300 });
app.use(log4js.connectLogger(log4js.getLogger("http"), { level: 'auto' }));