英文:
Redundancy in Winston Logger
问题
除了文件名之外,下面的代码存在冗余。由于我对Node.js很陌生,请建议我如何在调用应用程序的日志记录器时将文件名作为参数传递。
英文:
As there is redundancy in the below code except the file name... Please suggest me ways to reduce it as I am very new to node js. How can I pass file name as an argument while calling the logger from my application.
const loggerForDWTApps = createLogger({
level: loggerLevel === 'undefined' ? 'debug' : loggerLevel,
format: format.combine(
format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss'
}),
format.printf(info => `${info.timestamp} ${info.level}: ${info.message}`)),
transports: [new transports.File({ filename: path.join(logDir, 'XYZ.log') })
]
});
const loggerForDWTService = createLogger({
level: loggerLevel === 'undefined' ? 'debug' : loggerLevel,
format: format.combine(
format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss'
}),
format.printf(info => `${info.timestamp} ${info.level}: ${info.message}`)),
transports: [new transports.File({ filename: path.join(logDir, 'ABC.log') })
]
});
答案1
得分: 0
function getWinstonConfig(fileName) {
return {
level: loggerLevel === 'undefined' ? 'debug' : loggerLevel,
format: format.combine(
format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss'
}),
format.printf(info => `${info.timestamp} ${info.level}: ${info.message}`)),
transports: [new transports.File({ filename: path.join(logDir, '${fileName}.log')})]
}
}
const loggerForDWTApps = createLogger(getWinstonConfig('ABC'));
const loggerForDWTService = createLogger(getWinstonConfig('XYZ'));
英文:
Write a function and call it as shown below :
function getWinstonConfig(fileName) {
return {
level: loggerLevel === 'undefined' ? 'debug' : loggerLevel,
format: format.combine(
format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss'
}),
format.printf(info => `${info.timestamp} ${info.level}: ${info.message}`)),
transports: [new transports.File({ filename: path.join(logDir, '${fileName}.log')})]
}
}
const loggerForDWTApps = createLogger(getWinstonConfig('ABC'));
const loggerForDWTService = createLogger(getWinstonConfig('XYZ'));
答案2
得分: 0
Here is the translated content without the code:
我建议创建一个函数,比如说createLoggerWrapper,该函数接受用于配置日志记录器的各种参数。
例如:
function createLoggerWrapper(logFile) {
return createLogger({
level: loggerLevel === 'undefined' ? 'debug' : loggerLevel,
format: format.combine(
format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss'
}),
format.printf(info => `${info.timestamp} ${info.level}: ${info.message}`)),
transports: [new transports.File({ filename: path.join(logDir, logFile) })]
});
}
const loggerForDWTApps = createLoggerWrapper('XYZ.log');
const loggerForDWTService = createLoggerWrapper('ABC.log');
I hope this helps!
英文:
I'd suggest creating a function, say createLoggerWrapper that accepts any varying parameters for your logger configuration.
For example:
function createLoggerWrapper(logFile) {
return createLogger({
level: loggerLevel === 'undefined' ? 'debug' : loggerLevel,
format: format.combine(
format.timestamp({
format: 'YYYY-MM-DD HH:mm:ss'
}),
format.printf(info => `${info.timestamp} ${info.level}: ${info.message}`)),
transports: [new transports.File({ filename: path.join(logDir, logFile) })]
});
}
const loggerForDWTApps = createLoggerWrapper('XYZ.log');
const loggerForDWTService = createLoggerWrapper('ABC.log');
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论