Node.js Express uses LOG4JS log __js

Source: Internet
Author: User

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.

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.