新来的.js,

:method :url :status :res[content-length] - :response-time ms

据我所知,最好的地方是interceptors个.但我也使用了Guards,如前所述,警卫是由after个中间软件触发的,但有before个拦截器.

也就是说,我的forrbiden访问没有被记录.我可以在两个不同的地方写日志(log)部分,但不是这样.知道吗?

谢谢

我的拦截代码:

import { Injectable, NestInterceptor, ExecutionContext, HttpException, HttpStatus } from '@nestjs/common';
import { Observable, throwError } from 'rxjs';
import { catchError, tap } from 'rxjs/operators';

@Injectable()
export class HTTPLoggingInterceptor implements NestInterceptor {

  intercept(context: ExecutionContext, call$: Observable<any>): Observable<any> {
    const now = Date.now();
    const request = context.switchToHttp().getRequest();

    const method = request.method;
    const url = request.originalUrl;

    return call$.pipe(
      tap(() => {
        const response = context.switchToHttp().getResponse();
        const delay = Date.now() - now;
        console.log(`${response.statusCode} | [${method}] ${url} - ${delay}ms`);
      }),
      catchError((error) => {
        const response = context.switchToHttp().getResponse();
        const delay = Date.now() - now;
        console.error(`${response.statusCode} | [${method}] ${url} - ${delay}ms`);
        return throwError(error);
      }),
    );
  }
}

推荐答案

我最终在原始应用程序上注入了一个classic 的记录器.

import { NestFactory } from '@nestjs/core';
import { FastifyAdapter, NestFastifyApplication } from '@nestjs/platform-fastify';
import { ApplicationModule } from './app.module';
import * as morgan from 'morgan';

async function bootstrap() {
    const app = await NestFactory.create<NestFastifyApplication>(ApplicationModule, new FastifyAdapter());
    app.use(morgan('tiny'));

    await app.listen(process.env.PORT, '0.0.0.0');
}

if (isNaN(parseInt(process.env.PORT))) {
    console.error('No port provided. ?');
    process.exit(666);
}

bootstrap().then(() => console.log('Service listening ?: ', process.env.PORT));

Typescript相关问答推荐

具有条件通用props 的react 类型脚本组件错误地推断类型

如何将单元测试中的私有订阅变量设置为空(或未定义)?

Angular -使用Phone Directive将值粘贴到控件以格式化值时验证器不工作

typescribe警告,explate—deps,useEffect依赖项列表

如何在TypeScrip面向对象程序设计中用方法/属性有条件地扩展类

不推荐使用Marker类-Google map API警告

如何在已知基类的任何子类上使用`extends`?

使用Redux Saga操作通道对操作进行排序不起作用

为什么S struct 类型化(即鸭子类型化)需要非严格类型联合?

缩小对象具有某一类型的任意字段的范围

具有匹配功能的TS通用事件处理程序

是否将Angular *ngFor和*ngIf迁移到新的v17语法?

是否可以定义引用另一行中的类型参数的类型?

递归类型别名的Typescript 使用

如何处理可能为空的变量...我知道这不是Null?

在Vite中将SVG导入为ReactComponent-不明确的间接导出:ReactComponent

如何将对象的字符串文字属性用作同一对象中的键类型

组件使用forwardRef类型脚本时传递props

如何提取具有索引签名的类型中定义的键

Next 13 + React 18 createContext 类型的构建问题