Как правильно создать регистратор Winston в TypeScript, который будет регистрировать экспресс-регистрацию промежуточного программного обеспечения Morgan? Я нашел несколько примеров JavaScript, но у меня возникли проблемы с преобразованием их в TypeScript, потому что я получаю сообщение об ошибке Type '{ write: (message: string, encoding: any) => {}; logger: any; }' is not assignable to type '(options?: any) => ReadableStream'. Object literal may only specify known properties, and 'write' does not exist in type '(options?: any) => ReadableStream'.
Вот мой код:
import { Logger, transports } from 'winston';
// http://tostring.it/2014/06/23/advanced-logging-with-nodejs/
// https://www.loggly.com/ultimate-guide/node-logging-basics/
const logger = new Logger({
transports: [
new (transports.Console)({
level: process.env.NODE_ENV === 'production' ? 'error' : 'debug',
handleExceptions: true,
json: false,
colorize: true
}),
new (transports.File)({
filename: 'debug.log', level: 'info',
handleExceptions: true,
json: true,
colorize: false
})
],
exitOnError: false,
});
if (process.env.NODE_ENV !== 'production') {
logger.debug('Logging initialized at debug level');
}
// [ts]
// Type '{ write: (message: string, encoding: any) => {}; logger: any; }' is not assignable to type '(options?: any) => ReadableStream'.
// Object literal may only specify known properties, and 'write' does not exist in type '(options?: any) => ReadableStream'.
logger.stream = {
write: function (message: string, encoding: any) {
logger.info(message);
};
}
export default logger;
Я смог обойти эту проблему, настроив свой код на использование const winston = require('winston');
, но хотел бы знать, как вы должны делать это, поддерживая типы?