当我使用多个记录器时,谁能指出如何在Winston 3中配置默认日志配置?
我想用Winston创建多个记录器,部分原因是我可以根据功能上下文对它们进行标记,部分原因是可以根据该功能上下文具有不同的传输和日志级别。
目前,我收到以下错误消息:“ [winston]尝试写没有传输的日志”
我当前的代码如下:
import {format, transports, loggers} from 'winston';
const customFormat = printf(({level, message, label, timestamp, stack}) => {
return `${timestamp} [${label}] ${level.toUpperCase()}: ${message}${stack?'\n ' + stack:''}`;
});
function getLoggerCategoryConfig(category) {
return config.log.categories[category];
}
function getLoggerCategories() {
if (config.log && config.log.categories) {
return Object.keys(config.log.categories);
}
return [];
}
function createLogger(category: string, options: any) {
// creates an object with instantiated transports, for
// example:
// {
// format: combine(
// label({ label: category }),
// timestamp()
// ),
// transports: [new transports.Console()]
// }
}
function initLogging() {
// configure default
let categoryConfig = getLoggerCategoryConfig('default');
let logger = createLogger('default', categoryConfig);
// console.log(logger);
winston.configure(logger);
const categories = getLoggerCategories();
categories.forEach((category) => {
console.log('creating logger ' + category);
let categoryConfig = getLoggerCategoryConfig('default');
let logger = createLogger(category, categoryConfig);
loggers.add(category, logger);
});
}
配置文件包含一个块,例如:
"log": {
"categories": {
"default": {
"level": "info",
"outputs": [
"console",
{
"type": "file",
"filename": "logs/combined.log"
},
{
"type": "file",
"filename": "logs/error.log",
"level": "error"
}
]
},
"main": {
"level": "info",
"outputs": [
"console"
]
}
}
},