Node.js 如何将expressWinston放入特定的类/服务中?
因此,我有以下代码:Node.js 如何将expressWinston放入特定的类/服务中?,node.js,typescript,express,Node.js,Typescript,Express,因此,我有以下代码: const loggingWinston = new LoggingWinston({ projectId: CLOUD_PROJECT_ID, keyFilename: CLOUD_KEY_FILE, }); // winston configuration for dev const alignColorsAndTime = winston.format.combine( winston.format.colorize({ all:
const loggingWinston = new LoggingWinston({
projectId: CLOUD_PROJECT_ID,
keyFilename: CLOUD_KEY_FILE,
});
// winston configuration for dev
const alignColorsAndTime = winston.format.combine(
winston.format.colorize({ all: true }),
winston.format.label({ label: '[LOG]' }),
winston.format.timestamp({ format: 'YY-MM-DD HH:MM:SS' }),
winston.format.printf(
(info) => ` ${info.label} ${info.timestamp} ${info.level} : ${info.message}`,
),
);
app.use(expressWinston.logger({
level: 'info',
transports: [
DEV
? new winston.transports.Console({ format: winston.format.combine(winston.format.colorize(), alignColorsAndTime) })
: loggingWinston,
],
meta: true,
expressFormat: true, // Use the default Express/morgan request formatting.
colorize: true, // Color the text and status code, using the Express/morgan color palette.
}));
这很好用
现在,我想从index.ts中删除此逻辑,并将其放入名为WinstonService的新类中
我很挣扎,因为无论我从那门课上得到什么,我都会这样使用它:
let winstonService = new WinstonService(DEV);
app.use(winstonService.getExpressConfiguration());
export class WinstonService {
private dev: boolean;
private loggingWinston: LoggingWinston;
private alignColorsAndTime;
constructor(dev: boolean){
if (!dev){
this.loggingWinston = new LoggingWinston({
projectId: CLOUD_PROJECT_ID,
keyFilename: CLOUD_KEY_FILE,
});
}
// winston configuration for dev
this.alignColorsAndTime = winston.format.combine(
winston.format.colorize({ all: true }),
winston.format.label({ label: '[LOG]' }),
winston.format.timestamp({ format: 'YY-MM-DD HH:MM:SS' }),
winston.format.printf(
(info) => ` ${info.label} ${info.timestamp} ${info.level} : ${info.message}`,
),
);
}
public getExpressConfiguration(){
return expressWinston.logger({
level: 'info',
transports: [
this.dev
? new winston.transports.Console({ format: winston.format.combine(winston.format.colorize(), this.alignColorsAndTime) })
: this.loggingWinston,
],
meta: true,
expressFormat: true, // Use the default Express/morgan request formatting.
colorize: this.dev, // Color the text and status code, using the Express/morgan color palette.
});
}
}
export default new WinstonService(this.dev);
我得到:
"UnhandledPromiseRejectionWarning: Error: Invalid transport, must be an object with a log method."
我现在的班级是这样的:
let winstonService = new WinstonService(DEV);
app.use(winstonService.getExpressConfiguration());
export class WinstonService {
private dev: boolean;
private loggingWinston: LoggingWinston;
private alignColorsAndTime;
constructor(dev: boolean){
if (!dev){
this.loggingWinston = new LoggingWinston({
projectId: CLOUD_PROJECT_ID,
keyFilename: CLOUD_KEY_FILE,
});
}
// winston configuration for dev
this.alignColorsAndTime = winston.format.combine(
winston.format.colorize({ all: true }),
winston.format.label({ label: '[LOG]' }),
winston.format.timestamp({ format: 'YY-MM-DD HH:MM:SS' }),
winston.format.printf(
(info) => ` ${info.label} ${info.timestamp} ${info.level} : ${info.message}`,
),
);
}
public getExpressConfiguration(){
return expressWinston.logger({
level: 'info',
transports: [
this.dev
? new winston.transports.Console({ format: winston.format.combine(winston.format.colorize(), this.alignColorsAndTime) })
: this.loggingWinston,
],
meta: true,
expressFormat: true, // Use the default Express/morgan request formatting.
colorize: this.dev, // Color the text and status code, using the Express/morgan color palette.
});
}
}
export default new WinstonService(this.dev);
有什么建议吗?非常感谢。现有代码对DEV===true和false都有效吗?唯一让我感到不合适的是
导出默认的新WinstonService(this.dev)
,它在模块作用域中是错误的构造函数中也没有设置this.dev
。除非ts有魔法?