koa-log4 配置

安裝:npm

npminstall log4jsjson

npminstall log4js –save   // 安裝到工程目錄app

配置與使用:ui

// 基本使用spa

var log4js = require('log4js');debug

var logger = log4js.getLogger();日誌

logger.level = 'debug';server

logger.debug("Some debug messages");對象

// 配置規則utf-8

/**

 * 第一種:

 * configure方法爲配置log4js對象,內部有levels、appenders、categories三個屬性

 * levels:

 *         配置日誌的輸出級別,共ALL<TRACE<DEBUG<INFO<WARN<ERROR<FATAL<MARK<OFF八個級別,default level is OFF

 *         只有大於等於日誌配置級別的信息才能輸出出來,能夠經過category來有效的控制日誌輸出級別

 * appenders:

 *         配置文件的輸出源,通常日誌輸出type共有console、file、dateFile三種

 *         console:普通的控制檯輸出

 *         file:輸出到文件內,以文件名-文件大小-備份文件個數的形式rolling生成文件

 *         dateFile:輸出到文件內,以pattern屬性的時間格式,以時間的生成文件

 * replaceConsole:

 *         是否替換控制檯輸出,當代碼出現console.log,表示以日誌type=console的形式輸出

 *

 */

log4js.configure('./config/log4js.json');

log4js.json

{

  "appenders": {

    "access": {

      "type": "dateFile",

      "filename": "log/access.log",

      "pattern": "-yyyy-MM-dd"

    },

    "rule-console": {

      "type": "console"

    },

    "rule-file": {

      "type": "dateFile",

      "filename": "log/server-",

      "encoding": "utf-8",

      "maxLogSize": 10000000,

      "numBackups": 3,

      "pattern": "yyyy-MM-dd.log",

      "alwaysIncludePattern": true

    },

    "rule-error": {

      "type": "dateFile",

      "filename": "log/error-",

      "encoding": "utf-8",

      "maxLogSize": 1000000,

      "numBackups": 3,

      "pattern": "yyyy-MM-dd.log",

      "alwaysIncludePattern": true

    }

  },

  "categories": {

    "default": {

      "appenders": [

        "rule-console",

        "rule-file",

        "rule-error"

      ],

      "level": "debug"

    },

    "http": {

      "appenders": [

        "access"

      ],

      "level": "info"

    }

  }

}

// 第二種配置方式

log4js.configure({

    appenders: {

        ruleConsole: {type: 'console'},

        ruleFile: {

            type: 'dateFile',

            filename: 'logs/server-',

            pattern: 'yyyy-MM-dd.log',

            maxLogSize: 10 * 1000 * 1000,

            numBackups: 3,

            alwaysIncludePattern: true

        }

    },

    categories: {

        default: {appenders: ['ruleConsole', 'ruleFile'], level: 'info'}

    }

});

相關文章
相關標籤/搜索