Nodejs Implementation log function can use LOG4JS, but its github is just a brief introduction, I use the time also encountered some problems, summed up, hoping to give beginners some help.
Log4 in Java and net have, I believe everyone has used, so do not introduce the basic concept, directly to the node code.
First, install NPM installation Log4js
After that, we set up a log.js file to configure the LOG4JS.
var log4js = require (' Log4js ');
Log4js.configure ({
Appenders: [
{
Type: ' Console ',
Category: "Console"
},
{
Type: "Datefile",//input type of log, file named by date
FileName: ' logs/',//directory where the log is stored, the folder needs to be created manually first
Pattern: ' Yyyy-mm-dd.log ',//output filename format is 2014-11-13.log
Alwaysincludepattern:true
/* Category: ' normal '///This does not have to be set, once set, must be consistent after use
}
],
replaceconsole:true//also writes the contents of the console output to the log file
});
Output method for external invocation, name is the category injected in the above configuration
Exports.logger = function (name) {
var logger = Log4js.getlogger (name);
Logger.setlevel (' INFO ');
return logger;
};
Finally, call Log.js in the other modules to use the
var logger = require ('.. /log '). Logger (' index ');
Logger.info (' Hello Log4js ');
Detailed LOG4JS recommended http://blog.fens.me/nodejs-log4js/
Log4js implementing the Log function of node