使用方法
1 2 3 4 5 6 7 8 9
| var log4js = require('log4js'); var logger = log4js.getLogger(); logger.level = 'debug'; logger.debug("Some debug messages"); log4js.configure('./config/log4js.json');
|
log4js.json配置方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
| { "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'}
}
});
```