安装 (terminal)
npm install log4js
logger.js 简易内容
var log4js = require('log4js');
log4js.configure( options ) ;
var logger = log4js.getLogger([category]);
module.exports = logger;
logger.js 具体内容梳理
const log4js = require( "log4js" );
const options = {
appenders: {
out: {
type: "stdout",
layout: {
type: "pattern",
pattern: "%[[%x{reqid}] [%d{ISO8601_WITH_TZ_OFFSET}] [%x{ip}] [%p] [%x{user}]%] %m%n",
tokens: {
user( logEvent ) {
if ( logEvent.context.user ) {
return logEvent.context.user;
}
return "";
}
}
}
},
file: {
type: "file",
filename: "logs/access.log",
maxLogSize: 10485760,
layout: {
type: "messagePassThrough"
},
compress: true
}
},
categories: {
"out": {
appenders: [ "out" ],
level: "info"
},
default: {
appenders: [ "file" ],
level: "info"
}
}
}
log4js.configure( options );
var logger = log4js.getLogger( "out" );
module.exports = logger;
options 配置
options:
- levels:
All -> Trace -> Debug -> Info -> Warn -> Error -> Fatal -> Mark -> Off(关闭)- appenders ( appendersObject ):
日志事件序列化为某种形式的输出- categories ( categoriesObject ):
用于分组日志事件的标签- pm2, pm2InstanceVar, disableClustering:
与PM2相关的配置
appendersObject { key : object}
key: {
type: types, (required)
layout: layoutObject
}
types : 输出类型
- categoryFilter
- console (向控制台写入大量输出会使应用程序占用大量内存。如果遇到此问题,请尝试切换到stdout appender。
)- dateFile (filename, pattern('.yyyy-MM-dd-hh'), compress, encoding)
- file (filename, maxLogSize, backups, compress, encoding)
- fileSync ( 不支持compress)
- logLevelFilter
- multiFile (多文件 ,base, extension(后缀), timeout, property: 'categoryName' (用于文件名区分))
- multiprocess (多进程 mode(master | worker), appender(针对master), loggerPort, loggerHost )
- recording
- stderr (标准错误流)
- stdout (标准输出流)
- tcp (port, host)
- tcp-server
layoutObject: 输出格式 【也可自定义layout】
- basic: 时间,级别, 类别, 信息
[2017-03-30 07:57:00.113] [ERROR] cheese - Cheese is too ripe!- colored: 比 basic 多了颜色
TRACE - '蓝色blue' | DEBUG - '青色cyan' | INFO - '绿色green' | WARN - '黄色yellow' | ERROR - '红red' | FATAL - '洋红色magenta'- pattern (pattern(patternString), tokens(tokenObject))
patternString
%r
time in toLocaleTimeString format%p
日志级别%c
日志种类%h
主机名%m
日志信息%d
日志日期 (默认2017-06-01 08:32:56.283)%d{yyyy/MM/dd-hh.mm.ss}
%%
% - for when you want a literal%
in your output%n
换行%z
process id (fromprocess.pid
)%x{<tokenname>}
将tokens中的变量带入%X{<tokenname>}
logger.addContext('user', 'charlie'); 将user带入%[
start a coloured block (colour will be taken from the log level, similar tocolouredLayout
)%]
end a coloured block
tokenObject
tokens: {
reqid( logEvent ) {
if ( logEvent.context.reqid ) {
return logEvent.context.reqid;
}
return "";
}
categoriesObject : 必有一个default
"out" : {
appenders: [ "out" ],
level: "info"
},
default: {
appenders: [ "file" ],
level: "info"
}
自定义layout
log4js.addLayout('json', function(config) {
return function(logEvent) { return JSON.stringify(logEvent); }
});
LogEvent 对象
LoggingEvent {
startTime: 2018-12-03T05:57:50.033Z,
categoryName: '[Logger]',
data: [ '拉取日志失败' ],
level: Level { level: 40000, levelStr: 'ERROR', colour: 'red' },
context: {},
pid: 2262 } {}
}