Create Log.js
var log4js = require (' Log4js ');
log4js.configure {
appenders: [
{
type: ' Console ',
Category: ' Console '
},//console output
{
Type: "Datefile",
filename: ' logs/log.log ', pattern
: "_yyyy-mm-dd",
Alwaysincludepattern:false,
Category: ' Datefilelog '
} Date file format
],
replaceconsole:true, ///replace Console.log
levels:{
datefilelog: ' INFO '
}
});
var datefilelog = Log4js.getlogger (' Datefilelog ');
Exports.logger = Datefilelog;
Exports.use = function (APP) {
//page request log, with auto, the default level is Warn
//app.use (Log4js.connectlogger (Datefilelog, { Level: ' Auto ', format: ': Method:url '});
App.use (Log4js.connectlogger (Datefilelog, {level: ' Debug ', Format: ': Method:url '});
}
Add the following code to the App.js, which is placed in front of the other app.use due to the order of loading
var log = require ('./log ');
Log.use (APP);
function to log, the Require path is modified according to its own project:
var logger = require ('.. /log '). Logger;
Logger.info ("This is Log");
It should be noted that if the log is using a date (as in this example), one day a file, test words, directly modify the date test, will not produce a new date log, must be adjusted to the night 23:59, and then wait for the whole point, this time can test whether the new file will be generated, official examples are as follows ( Https://github.com/nomiddlename/log4js-node/wiki/Date%20rolling%20file%20appender):
Then Initial logging would create a file called "Blah.log". At midnight, the current "Blah.log" file would is renamed to "Blah.log-2012-09-26" (for example), and a new "blah" . log "file created.