我创建了一个使用Winston的记录器模块。我正在尝试格式化日志,以便对它们进行颜色编码。但是,它通过以下输出弄乱了我的日志。
{"message":"Server Running On: localhost:3000","level":"\u001b[32minfo\u001b[39m","timestamp":"2018-06-30 19:20:09"}
{"message":"MongoDB Successfully Connected On: mongodb://localhost:27017/project","level":"\u001b[32minfo\u001b[39m","timestamp":"2018-06-30 19:20:09"}
可以看到\ u ....字符串。日志也不进行颜色编码。我的配置怎么了?我正在使用Winston 3,请参见下面的代码。
import { createLogger, transports, format } from "winston";
import * as appRoot from "app-root-path";
class Logger {
public logger: any;
constructor() {
const options = {
file: {
level: "info",
filename: `${appRoot}/logs/app.log`,
handleExceptions: true,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 5,
colorize: true,
},
console: {
level: "debug",
handleExceptions: true,
json: false,
colorize: true,
},
exceptionHandlers: {
filename: `${appRoot}/logs/exceptions.log`,
},
};
this.logger = createLogger({
format: format.combine(
format.simple(),
format.timestamp({
format: "YYYY-MM-DD HH:mm:ss",
}),
format.colorize(),
format.json(),
),
transports: [
new transports.File(options.file),
new transports.Console(options.console),
],
exceptionHandlers: [
new transports.File(options.exceptionHandlers),
],
// do not exit on handled exceptions
exitOnError: false,
});
}
}
export default new Logger().logger;