npm install log4js

npm install magic-globals // show line number

Create Config log4js.js

const log4js = require('log4js');
var log4js_config = require("./log4js.json");
log4js.configure(log4js_config);

// var logger = log4js.getLogger();
// module.exports = logger;

module.exports = log4js;

Create Config json file (log4js.json)

{
    "appenders": {
        "out": {
            "type": "stdout"
        },
        "console": {
            "type": "console",
            "category": "console"
        },
        "debug": {
            "type": "dateFile",
            "filename": "logs/debug",
            "pattern": "-yyyy-MM-dd.log",
            "alwaysIncludePattern": true,
            "keepFileExt": true
        },
        "debug-filter": {
            "type": "logLevelFilter",
            "appender": "debug",
            "level": "debug",
            "maxLevel": "debug"
        },
        "result": {
            "type": "dateFile",
            "filename": "logs/result",
            "pattern": "-yyyy-MM-dd.log",
            "alwaysIncludePattern": true,
            "keepFileExt": true
        },
        "result-filter": {
            "type": "logLevelFilter",
            "appender": "result",
            "level": "info",
            "maxLevel": "info"
        },
        "error": {
            "type": "dateFile",
            "filename": "logs/error",
            "pattern": "-yyyy-MM-dd.log",
            "alwaysIncludePattern": true,
            "keepFileExt": true
        },
        "error-filter": {
            "type": "logLevelFilter",
            "appender": "error",
            "level": "error",
            "maxLevel": "error"
        },
        "default": {
            "type": "dateFile",
            "filename": "logs/default",
            "pattern": "-yyyy-MM-dd.log",
            "alwaysIncludePattern": true,
            "keepFileExt": true
        },
        "warn": {
            "type": "dateFile",
            "filename": "logs/warn",
            "pattern": "-yyyy-MM-dd.log",
            "alwaysIncludePattern": true,
            "keepFileExt": true
        },
        "warn-filter": {
            "type": "logLevelFilter",
            "appender": "warn",
            "level": "warn",
            "maxLevel": "warn"
        }
    },
    "categories": {
        "default": {
            "appenders": [
                "out",
                "console",
                "default",
                "debug-filter",
                "result-filter",
                "debug-filter",
                "error-filter",
                "warn-filter"
            ],
            "level": "trace"
        },
        "debug": {
            "appenders": [
                "debug",
                "debug-filter"
            ],
            "level": "debug"
        },
        "result": {
            "appenders": [
                "result-filter",
                "debug-filter",
                "error-filter",
                "warn-filter"
            ],
            "level": "debug"
        },
        "error": {
            "appenders": [
                "error",
                "error-filter"
            ],
            "level": "error"
        },
        "warn": {
            "appenders": [
                "warn",
                "warn-filter"
            ],
            "level": "warn"
        }
    }
}

開始使用log4js

// you may now use additional global objects, 
// in addition to built-ins: __filename and __dirname 
// console.log(__filename) // /home/node/myapp/server/server.js 
// console.log(__file) // server 
// console.log(__line) // 6 
// console.log(__fili) // /home/node/myapp/server/server.js:6 
// console.log(__ext) // js 
// console.log(__base) // /home/node/myapp 
// console.log(__func) // someFunction or (anonymous)  
// console.log(__dirname) // /home/node/myapp/server 

// inti log4js
var log4js = require("./log4js");
// setup logger name
var log = log4js.getLogger("app.js");

log.debug("[" + __line + "] " + 'We Write Logs with log4js');
log.info("[" + __line + "] " + 'You can find logs-files in the log-dir');
log.warn("[" + __line + "] " + 'log-dir is a configuration-item in the log4js.json');
log.error("[" + __line + "] " + 'In This Test log-dir is : \'./logs/log_test/\'');

results matching ""

    No results matching ""