首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >Typescript -摩根和logger.stream导致lint错误

Typescript -摩根和logger.stream导致lint错误
EN

Stack Overflow用户
提问于 2018-06-13 21:07:11
回答 2查看 3.1K关注 0票数 1

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

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

下面是我的Winston初始化代码:

代码语言:javascript
复制
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的代码。

代码语言:javascript
复制
// ... All import
import logger from './logger/index';

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

我不确定我为什么会得到这个错误:/

EN

回答 2

Stack Overflow用户

发布于 2018-06-13 21:26:21

好的,所以我深入研究了代码和typescript文件,以了解它真正需要做的事情。

我将我的logger.stream声明从

代码语言:javascript
复制
// 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());
  }
});

代码语言:javascript
复制
// 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

代码语言:javascript
复制
// My import
import { logger, morganOption } from './logger/index';

// ... Then later, the new code
this.expressApp.use(morgan('combined', morganOption));

希望它能帮助其他人:)

票数 3
EN

Stack Overflow用户

发布于 2019-02-21 04:19:47

使用node包管理器安装Winston node包

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

代码语言:javascript
复制
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下面的一行中可以看到更多信息

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50838074

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档