NestJS如何将自定义Logger添加到自定义ExceptionFilter

时间:2018-11-20 14:04:18

标签: node.js nest nestjs

我正在使用NestJS 5.4.0 我有自定义LoggerService,它运行良好。但是,如何将这个LoggerService添加到ExceptionFilter。

// logger.service.ts
import {Injectable, LoggerService} from '@nestjs/common';
@Injectable()
export class Logger implements LoggerService {
    log(message: string) {
        console.log(message);
    }
    error(message: string, trace: string) {
        console.error(message);
    }
    warn(message: string) {
        console.warn(message);
    }
}

//logger.module.ts
import { Module } from '@nestjs/common';
import {Logger} from '../services/logger.service';
@Module({
    providers: [Logger],
    exports: [Logger],
})
export class LoggerModule {}


// user.module.ts
import { Module } from '@nestjs/common';
import {UserService} from '../services/user.service';
import {LoggerModule} from './logger.module';

@Module({
    imports: [LoggerModule],
    providers: [UserService],
    exports: [UserService],
})
export class UserModule {}

运行良好。

import {Logger} from './logger.service';
export class UserService {
    constructor(
        private logger: Logger
    ) {}
    private test = () => {
        this.logger.log("test"); // log success "test" to console
    }
}

但是如何将我的自定义Logger添加到ExceptionFilter

// forbidden.exception.filter.ts
import {HttpException, HttpStatus, Injectable} from '@nestjs/common';

@Injectable()
export class ForbiddenException extends HttpException {
    constructor(message?: string) {
        super(message || 'Forbidden', HttpStatus.FORBIDDEN);
        // I want to add my custom logger here!
    }
}

感谢阅读。

2 个答案:

答案 0 :(得分:0)

首先您的class ForbiddenException extends HttpException不是 它称为ExceptionFilterExceptionFilter

  

exception层,负责处理应用程序中所有未处理的异常

docs

当您尝试将exmaple注入到自定义HttpException中时,您提供了exmaple。但这是错误的。您的异常不必负责日志记录。这就是ExceptionFilter应该负责的事情。

无论如何,到目前为止(2019年10月17日),官方文档中还没有示例如何向ExceptionFilter注入提供者。

您可以在初始化时将其传递给constructor,但是您应该在使用app.get<T>(...)方法之前获取Logger实例。

例如,我从exception-filters docs更改了代码:

// HttpExceptionFilter.ts

import { ExceptionFilter, Catch, ArgumentsHost, HttpException } from '@nestjs/common';
import { Request, Response } from 'express';
import {MyLogger} from '../MyLogger'

@Catch(HttpException)
export class HttpExceptionFilter implements ExceptionFilter {
  constructor(private readonly logger: MyLogger) {}

  catch(exception: HttpException, host: ArgumentsHost) {
    const ctx = host.switchToHttp();
    const response = ctx.getResponse<Response>();
    const request = ctx.getRequest<Request>();
    const status = exception.getStatus();

    if (status >= 500) {
      this.logger.error({ request, response });
    }

    response
      .status(status)
      .json({
        statusCode: status,
        timestamp: new Date().toISOString(),
        path: request.url,
      });
  }
}

bootstrap.ts代码:

// bootstrap.ts

const app = await NestFactory.create(MainModule, {
  logger: false,
});

const logger = app.get<MyLogger>(MyLogger);
app.useLogger(logger);
app.useGlobalFilters(new HttpExceptionFilter(logger));

此技术可用于所有这些INestApplication方法:

  • app.useGlobalFilters
  • app.useGlobalGuards
  • app.useGlobalInterceptors
  • app.useGlobalPipes
  • app.useLogger
  • app.useWebSocketAdapter

答案 1 :(得分:0)

首先,要将依赖项注入与异常过滤器一起使用,您不能使用useGlobalFilters()方法注册它们:

const app = await NestFactory.create(MainModule, {
  logger: false,
});

const logger = app.get<MyLogger>(MyLogger);
app.useLogger(logger);

//Remove this line
//app.useGlobalFilters(new HttpExceptionFilter(logger));

接下来,在您的MainModule中,将您的自定义异常过滤器添加为提供程序(注意:无论将过滤器添加到哪个模块,过滤器都会自动设置为全局过滤器,但最佳做法是将其添加到顶级模块):

import { Module } from '@nestjs/common';
import { APP_FILTER } from '@nestjs/core';
import { LoggerModule } from './logger.module';
import { ForbiddenException } from './forbidden.exception.filter.ts';

@Module({
  imports: [
    LoggerModule //this is your logger module
  ],
  providers: [
    {
      provide: APP_FILTER, //you have to use this custom provider
      useClass: ForbiddenException //this is your custom exception filter
    }
  ]
})
export class MainModule {}

现在您可以将记录器注入到您的自定义异常过滤器中:

import {HttpException, HttpStatus, Injectable} from '@nestjs/common';
import { Logger } from './path/to/logger';

@Injectable()
export class ForbiddenException extends HttpException {

  constructor(private logger: Logger) {}

  catch(exception: HttpException, response) {
    this.logger.log('test');
  }
}

伪代码,但我认为您可以理解。