NODEJS(14)Platform - log4js

NODEJS(14)Platform - log4js

1. log4js Introduction
>npm info log4js version
npmhttpGEThttps://registry.npmjs.org/log4jsnpmhttp200https://registry.npmjs.org/log4js 0.6.14

>npm info log4js
Get the right official website
https://github.com/nomiddlename/log4js-node

2. Use and Configure 
First of all, add the dependencies in package.json
{
     "name": "buglist",
     "version": "0.0.1",
     "private": true,
     "dependencies": {
          "express": "4.2.0",
          "body-parser": "1.2.0",
          "generic-pool": "2.0.4",
          "mongodb": "1.4.5",
          "log4js": "0.6.14"
     }
}

Make sure to install it
>npm install

Prepare the configuration file in log4js.json
{
    "appenders": [
        {
            "type": "console",
            "layout": {
                "type": "pattern",
                "pattern": "[%d{yyyy-MM-dd hh:mm:ss,SSS}] [%-5p] [%M] %m"
            },
            "category": ["root"]
        },
        {
            "type": "file",
            "filename": "buglist-error.log",
            "maxLogSize": 10485760,
            "backups": 10,
            "layout": {
                "type": "pattern",
                "pattern": "[%d{yyyy-MM-dd hh:mm:ss,SSS}] [%-5p] [%M] %m"
            },
            "category": ["root"]
        },
        {
            "type": "file",
            "absolute": true,
            "filename": "/var/log/nodejs/buglist/buglist-error.log",
            "maxLogSize": 20480,
            "backups": 10,
            "layout": {
                "type": "pattern",
                "pattern": "[%d{yyyy-MM-dd hh:mm:ss,SSS}] [%-5p] [%M] %m"
            },
            "category": "root"         
        }
    ],
    "levels": {
        "root": "DEBUG"
    }
}

Start and use it in app.js.

var log4js = require('log4js’);
log4js.configure(__dirname +'/config/log/log4js.json’);
var logger = log4js.getLogger("root”);

logger.debug("Running nodeJS on port" + app.get("port") + " !”);

Then we can use logger when we want to use it.

3. Integrate with Express.js
Easy to Integrate with Express.js, just use it.
app.use(log4js.connectLogger(logger));


References:
http://blog.fens.me/nodejs-log4js/
https://github.com/nomiddlename/log4js-node

相关推荐