如何使用 nestjs 配置 winston 服务?

How to configure a winston service with nestjs?

我有一个基本的 nestjs 应用程序,我正在尝试使用 winston 作为记录器服务...这破坏了我的应用程序,我真的不知道 fix/revert 这个。 我已经尝试卸载 winston 软件包,rm node_modules 和 npm install 再次安装,没有任何效果。

node -v: v11.15.
nest -v: 7.1.5
yarn -v: 1.22.4
npm -v: 6.14.5

我得到的错误:

[11:37:19 AM] Found 0 errors. Watching for file changes.

internal/modules/cjs/loader.js:670
    throw err;
    ^

Error: Cannot find module './app.module'
    at Function.Module._resolveFilename (internal/modules/cjs/loader.js:668:15)
    at Function.Module._load (internal/modules/cjs/loader.js:591:27)
    at Module.require (internal/modules/cjs/loader.js:723:19)
    at require (internal/modules/cjs/helpers.js:14:16)
    at Object.<anonymous> (/Users/dtmirror/app-api/dist/src/main.js:4:22)
    at Module._compile (internal/modules/cjs/loader.js:816:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:827:10)
    at Module.load (internal/modules/cjs/loader.js:685:32)
    at Function.Module._load (internal/modules/cjs/loader.js:620:12)
    at Function.Module.runMain (internal/modules/cjs/loader.js:877:12)

安装的包:

npm i winston

日志服务:

import * as winston from 'winston';
import { LoggerOptions } from 'winston';

export class LoggerService {
    private logger;

    public static loggerOptions: LoggerOptions = {
        transports: [
            new winston.transports.File({ filename: 'app.log' })
        ]
    }

    constructor(private context: string, transport?) {
        this.logger = (winston as any).createLogger(LoggerService.loggerOptions);
    }

    log(message: string): void {
        const currentDate = new Date();
        this.logger.info(message, {
            timestamp: currentDate.toISOString(),
            context: this.context,
        })
    }
}

main.ts:

import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
import { LoggerService } from '../logger.service'; // <-- this breaks everything

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  await app.listen(3000);
}
bootstrap();

那一刻我运行yarn start:dev在这个阶段一切都崩溃了...

AppModule 的导入似乎很糟糕。根据评论,loggersrc 目录之外,最终导致 dist 呈现不同的形状