Log4js implementing the Log function of node

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.