【问题标题】:Typescript - Morgan and logger.stream cause lint errorTypescript - Morgan 和 logger.stream 导致 lint 错误
【发布时间】:2018-11-23 02:03:05
【问题描述】:

我尝试使用记录器流功能将Morgan 日志附加到Winston。 但是,一旦我在使用 morgan 中间件时尝试附加 logger.stream,它就会失败并显示以下消息:

Argument of type '"combined"' is not assignable to parameter of type 'FormatFn'.

这是我的 Winston 初始化代码:

import * as appRoot from 'app-root-path';
import * as winston from 'winston';
import { Logger } from 'winston';
import * as fs from 'fs';
import * as stream from 'stream';

const dirLogs = `${appRoot}/logs`;

// It's call during initialization, we can block the thread
if (!fs.existsSync(dirLogs)) {
  fs.mkdirSync(dirLogs);
}
// define the custom settings for each transport (file, console)
const options = {
  file: {
    level: 'info',
    filename: `${dirLogs}/app.log`,
    handleExceptions: true,
    json: true,
    maxsize: 5242880, // 5MB
    maxFiles: 5,
    colorize: false,
  },
  console: {
    level: 'debug',
    handleExceptions: true,
    json: false,
    colorize: true,
  },
};

// Keep it simple to focus on the need first
// I think Logger should send logs to a logger service
const logger = new Logger({
  level: 'info',
  transports: [
    new winston.transports.File(options.file),
    new winston.transports.Console(options.console),
  ],
  exitOnError: false, // do not exit on handled exceptions
});

// If I don't use the stream.Duplex, it cause another lint error.
logger.stream = (options?: any) => new stream.Duplex({
  write: function (message: string, encoding: any) {
      logger.info(message.trim());
  }
});

export default logger;

然后,我尝试使用 Morgan 的代码。

// ... All import
import logger from './logger/index';

// ... Then later the code
this.expressApp.use(morgan('combined', { stream: logger.stream }));

我不确定为什么会出现此错误:/

【问题讨论】:

    标签: typescript express winston compiled morgan


    【解决方案1】:

    好的,所以我深入研究了代码和打字稿文件,以了解它真正需要做什么。

    我将 logger.stream 声明从

    // If I don't use the stream.Duplex, it cause another lint error.
    logger.stream = (options?: any) => new stream.Duplex({
      write: function (message: string, encoding: any) {
          logger.info(message.trim());
      }
    });
    

    // Don't forget this import
    import { Options } from 'morgan';
    
    // And the code
    export const morganOption: Options = {
      stream: {
        write: function (message: string) {
            logger.info(message.trim());
        },
      },
    };
    

    然后我导入morganOptions 并将其设置为morgan

    // My import
    import { logger, morganOption } from './logger/index';
    
    // ... Then later, the new code
    this.expressApp.use(morgan('combined', morganOption));
    

    希望它可以帮助其他人:)

    【讨论】:

      【解决方案2】:

      使用节点包管理器安装 Winston 节点包

      创建一个logger.ts,添加以下代码

      import { createLogger, format, transports } from 'winston';
      const { label, combine, timestamp , prettyPrint } = format;
      const logger = createLogger({
       format: combine(
       timestamp(),
       prettyPrint(),
       ),
       transports: [
       new transports.Console(),
       new transports.File({ filename: './error.log' , level: 'error' }),
       new transports.File({ filename: './info.log' , level: 'info' }),
       ],
       exitOnError: false,
      });
      export default loggerStep 
      

      在下面的行中查看更多信息 enter link description here

      【讨论】:

        猜你喜欢
        • 2017-12-23
        • 2019-03-26
        • 2020-06-07
        • 2023-01-05
        • 2021-06-12
        • 2023-03-13
        • 2015-10-03
        • 2022-12-14
        • 2018-08-24
        相关资源
        最近更新 更多