我在环回中使用Winston(3.0.0rc5)。我有以下Winston.js文件:
/* eslint-disable max-len */
'use strict';
var appRoot = require('app-root-path');
var winston = require('winston');
const {colorize, combine, timestamp, printf} = winston.format;
// Define your custom format with printf.
const timeStampFormat = printf(info => {
return `[${info.timestamp}] ${info.level}: ${info.message}`;
});
// define the custom settings for each transport (file, console)
var options = {
info: {
format: combine(
timestamp(),
timeStampFormat
),
level: 'info',
filename: `${appRoot}/logs/access.log`,
handleExceptions: false,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 5,
colorize: false,
},
error: {
format: combine(
timestamp(),
timeStampFormat
),
level: 'error',
filename: `${appRoot}/logs/error.log`,
handleExceptions: true,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 5,
colorize: false,
},
console: {
format: combine(
timestamp(),
colorize(),
timeStampFormat
),
level: 'debug',
handleExceptions: true,
json: false,
colorize: true,
},
};
// instantiate a new Winston Logger with the settings defined above
var logger = new winston.createLogger({
transports: [
new winston.transports.File(options.info),
new winston.transports.File(options.error),
new winston.transports.Console(options.console),
],
exitOnError: false, // do not exit on handled exceptions
});
module.exports = logger;
error.log和access.log文件创建得很好。 error.log未按预期显示任何信息消息。但是,即使我已将handleExceptions设置为false,access.log文件也会显示错误消息。
我该如何解决这个问题?
答案 0 :(得分:0)
看起来handleExceptions用于未捕获的异常,而不是记录的错误: https://github.com/winstonjs/winston#handling-uncaught-exceptions-with-winston
也许这个问题可以通过创建除错误之外的标准级别的自定义记录器级别来解决,如下所示: https://github.com/winstonjs/winston#using-custom-logging-levels
仅供参考 - 有类似的问题: https://github.com/winstonjs/winston/issues/614