如何使用 ogma nestjs-module 记录正文
how to log body with ogma nestjs-module
我只是使用这个 doc
设置了 ogma nestjs-module
@Module({
imports: [
OgmaModule.forRoot({
service: {
color: true,
json: false,
application: 'NestJS'
},
interceptor: {
http: ExpressParser,
ws: false,
gql: false,
rpc: false
}
})
],
providers: [
{
provide: APP_INTERCEPTOR,
useClass: OgmaInterceptor
}
]
})
export class AppModule {}
设置为全局
async function bootstrap() {
const app = await NestFactory.create(AppModule, { logger: false });
const logger = app.get<OgmaService>(OgmaService);
app.useLogger(logger);
await app.listen(3000);
}
bootstrap();
它有效,但我想知道如何记录请求和响应正文,因为目前它不记录该信息。
对于您正在寻找的东西,您可以执行以下操作
@Injectable()
export class CustomOgmaInterceptor extends OgmaInterceptor {
intercept(context: ExecutionContext, next: CallHandler) {
return super.intercept(context, next).pipe(tap(() => {
this.service.log(context.switchToHttp().getRequest().body);
}));
}
}
现在你可以绑定自定义拦截器了
{
provide: APP_INTERCEPTOR,
useClass: CustomOgmaInterceptor,
}
这应该就是全部了。
因为我是这个包的作者,所以我最终可能会在请求解析器中添加一个 getMeta()
或 getExtra()
方法,这样就可以通过扩展解析器来自动处理,而不是比扩展拦截器
编辑 2021 年 9 月 7 日
@ogma/nestjs-module@3.1.0
现在可用,无需扩展拦截器即可实现。简单地扩展现有的解析器并覆盖 getMeta
方法,如下所示:
import { ExecutionContext, Injectable } from '@nestjs/common';
import { ExpressParser } from '@ogma/platform-express';
@Injectable()
export class CustomExpressParser extends ExpressParser {
getMeta(context: ExecutionContext) {
const body = context.switchToHttp().getRequest().body;
return Object.keys(body).length ? body : 'no body';
}
}
这将导致拦截器生成两份日志,一份用于普通数据,一份用于额外元数据。 You can read more in the docs here
我只是使用这个 doc
设置了 ogma nestjs-module@Module({
imports: [
OgmaModule.forRoot({
service: {
color: true,
json: false,
application: 'NestJS'
},
interceptor: {
http: ExpressParser,
ws: false,
gql: false,
rpc: false
}
})
],
providers: [
{
provide: APP_INTERCEPTOR,
useClass: OgmaInterceptor
}
]
})
export class AppModule {}
设置为全局
async function bootstrap() {
const app = await NestFactory.create(AppModule, { logger: false });
const logger = app.get<OgmaService>(OgmaService);
app.useLogger(logger);
await app.listen(3000);
}
bootstrap();
它有效,但我想知道如何记录请求和响应正文,因为目前它不记录该信息。
对于您正在寻找的东西,您可以执行以下操作
@Injectable()
export class CustomOgmaInterceptor extends OgmaInterceptor {
intercept(context: ExecutionContext, next: CallHandler) {
return super.intercept(context, next).pipe(tap(() => {
this.service.log(context.switchToHttp().getRequest().body);
}));
}
}
现在你可以绑定自定义拦截器了
{
provide: APP_INTERCEPTOR,
useClass: CustomOgmaInterceptor,
}
这应该就是全部了。
因为我是这个包的作者,所以我最终可能会在请求解析器中添加一个 getMeta()
或 getExtra()
方法,这样就可以通过扩展解析器来自动处理,而不是比扩展拦截器
编辑 2021 年 9 月 7 日
@ogma/nestjs-module@3.1.0
现在可用,无需扩展拦截器即可实现。简单地扩展现有的解析器并覆盖 getMeta
方法,如下所示:
import { ExecutionContext, Injectable } from '@nestjs/common';
import { ExpressParser } from '@ogma/platform-express';
@Injectable()
export class CustomExpressParser extends ExpressParser {
getMeta(context: ExecutionContext) {
const body = context.switchToHttp().getRequest().body;
return Object.keys(body).length ? body : 'no body';
}
}
这将导致拦截器生成两份日志,一份用于普通数据,一份用于额外元数据。 You can read more in the docs here