Mis*_*Guy 3 node.js winston morgan
我刚刚实施了 Winston Logging,它按预期工作,但我遇到了一些我无法找到答案的问题。
据我所知,winston 的工作方式,设置的日志级别以及使用优先级以下的任何内容,例如出错时,它还将包括信息日志等。 有没有办法创建特定的日志级别,让我们称之为 HTTP或 db,我只将 http 或 db 事件记录到其中,而它们最终不会出现在组合文件或控制台中?
A better solution is to use a single logger with a format function as a "level filter" to specify which transport logs which specific level. Here is the solution (note, levelFilter could easily be extended to take an array of acceptable levels).
The key insight is that if no info object is returned from the formatter chain, nothing gets logged.
const { createLogger, format, transports } = require('winston');
const levelFilter = (level) =>
format((info, opts) => {
if (info.level != level) { return false; }
return info;
})();
const logger = createLogger({
transports: [
new transports.Console({
format: format.combine(
levelFilter("info"),
format.json()
)
}),
new transports.File({
filename: "test.log",
format: format.combine(
levelFilter("error"),
format.json()
)
}),
]
});
// ONLY is logged to test.log
logger.log({
level: 'error',
message: 'abcd'
});
// ONLY is logged to console
logger.log({
level: 'info',
message: '1234'
});
Run Code Online (Sandbox Code Playgroud)