为了得到日志
. 我创建了一个自定义格式化程序作为 winston config.js
const { createLogger, format, transports } = require('winston');
const { splat, combine, timestamp, label, printf, simple } = format;
const path = require('path');
const myFormat = printf(({ level, message, timestamp, meta }) => {
return `${timestamp} -> ${level}:\t${JSON.stringify(message)}`;
});
// define the custom settings for each transport (file, console)
const options = {
file: {
level: 'info',
filename: `${path.join(__dirname, '../logs/app.log')}`,
handleExceptions: true,
humanReadableUnhandledException: true,
json: true,
maxsize: 5242880, // 5MB
maxFiles: 5,
timestamp: true,
colorize: false,
},
console: {
level: 'debug',
handleExceptions: true,
json: true,
colorize: true,
},
};
module.exports = (moduleName) => {
let logger;
if (process.env.logging === 'off') {
logger = createLogger({
format: combine(
timestamp(),
label({ label: `${moduleName}`, message: true }),
myFormat
),
transports: [
new winston.transports.File(options.file),
],
exitOnError: false, // do not exit on handled exceptions
});
} else {
logger = createLogger({
format: combine(
timestamp(),
label({ label: `${moduleName}`, message: true }),
myFormat
),
transports: [
new transports.File(options.file),
new transports.Console(options.console),
],
exitOnError: false, // do not exit on handled exceptions
});
}
// create a stream object with a 'write' function that will be used by `morgan`
logger.stream = {
write(message) {
logger.info(message);
},
};
return logger;
};
我得到这样的
登录为,
其中 s 是
元未附加到讯息。我在这里缺少什么
uj5u.com热心网友回复:
#1编辑这一行:
const myFormat = printf(({ level, message, timestamp, ...meta }) => {
因为meta
是一个阵列,所以你需要先传播它才能使用它。
#2 温斯顿的档案说得很清楚:
someta
没有附加到message
,要使用它,请尝试:
`${JSON.stringify(meta)}`
0 评论