您应该如何在TypeScript中为Morgan创建Winston记录器流 [英] How are you supposed to create Winston logger stream for Morgan in TypeScript

查看:205
本文介绍了您应该如何在TypeScript中为Morgan创建Winston记录器流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在TypeScript中创建Winston记录器以记录快速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'.

What is the correct way to create a winston logger in TypeScript that will log the express Morgan middleware logging? I found a number of JavaScript samples but have had trouble converting them over to TypeScript, because I get an error 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');来解决此问题,但想知道您应该如何进行这种维护类型?

I have been able to work around this by adjusting my code to use const winston = require('winston'); but would like to know how you are supposed to do this maintaining types?

推荐答案

最终,我最终以此作为解决方案.我用一个名为write的方法创建了一个类

Ultimately I ended up with this as a solution. I created a class with one method called write

export class LoggerStream {
    write(message: string) {
        logger.info(message.substring(0, message.lastIndexOf('\n')));
    }
}

然后在添加表达时,我创建了该类的实例:

then when adding to express I created an instance of the class:

 app.use(morgan('combined', { stream: new LoggerStream() }));

这很适合我的情况

这篇关于您应该如何在TypeScript中为Morgan创建Winston记录器流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆