nodeJs接入log4j日志
# nodeJs接入log4j日志
- 引入log4j
npm i log4js -S
- log4j初始化
// 按日期记录
import log4js from 'log4js';
log4js.configure({
appenders: {
api: {
type: 'dateFile',
filename: 'logs/api',
pattern: 'yyyy-MM-dd.log',
alwaysIncludePattern: true,
category: 'normal',
maxLogSize: 100 * 1024 * 1024,
},
},
categories: { default: { appenders: ['api'], level: 'info' } },
});
export const apiLogger = log4js.getLogger('api');
- 请求中加入日志
apiLogger.info('请求', ctx.request);
apiLogger.info('返回', ctx.response);
- 日志查看 在logs目录下可以查看到日志文件
上次更新: 2022/01/20, 16:31:34