Nodejs Log Management Log4js

Source: Internet
Author: User

2 Commonly used configurations:

1. Backing up a number of files by file size Shard

varLOG4JS = require (' Log4js ')); Log4js.configure ({"Appenders" : [        {' Type ': ' Console '},        {            ' type ': ' File ',            "FileName": "Logs/app.log",            "Maxlogsize": 204800,            "Backups": 10,            "category": "App"        }    ],    "Replaceconsole":true,    "Levels" : {        "App": "All"    }});varLogger = Log4js.getlogger (' app '); App.use (Log4js.connectlogger (logger, {format:': Remote-addr:method:url:status:response-time MS '})

2. Shard by date, this shard mode cannot be limited by size and number of backups

var log4js = require (' Log4js '); Log4js.configure ({    appenders: [        ' console '},        {             ' datefile '            , ' Logs/app ',            '-yyyy-mm-dd.log ',            True  ,            ' app '        }    ],    true}); var logger = Log4js.getlogger (' app ': Remote-addr:method:url:status:response-time MS '});

Multi-type multi-application file Shard configuration:

log4js.configure ({"Appenders" : [        {' Type ': ' Console '},        {            ' type ': ' File ',            "FileName": "Logs/app1.log",            "Maxlogsize": 204800,            "Backups": 10,            "category": "App1"        },        {            ' type ': ' File ',            "FileName": "Logs/app2.log",            "Maxlogsize": 204800,            "Backups": 10,            "category": "App2"        },        {            ' type ': ' File ',            "FileName": "Logs/app3.log",            "Maxlogsize": 204800,            "Backups": 10,            "category": "App3"        }    ],    "Replaceconsole":true,    "Levels" : {        "App1": "All",//level can be set All,auto,info,warn,error"APP2": "All",        "APP3": "All"    }});

Modular:

1. Define the configuration file Log4js.json

{    "appenders" : [        {"type": "Console"},        {            "type": "Datefile",            "filename": "Logs/app",            "pattern": "-yyyy-mm-dd.log",            true,             "category": "App"        }    ],    true,    "levels" : {        "app": "All"    }}

2. Core Model Ms.js

/**/var log4js = require ("log4js" = Require ("... /log4js.json "); Log4js.configure (this = Log4js.getlogger (" app ");

3. How to use

var ms = require ("MS"), Ms.logger.info ("info test"), Ms.logger.warn ("Warn Test"); Ms.logger.error ("error TEST");

Nodejs Log Management Log4js

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.