fix: 补齐异常阶段请求日志
- src/common/http/all-exceptions.filter.ts: 记录 guard/pipe 阶段的结构化错误日志 - src/main.ts: 写入请求开始时间并向异常过滤器注入结构化 logger - src/common/logging: 避免成功拦截器重复记录异常日志
This commit is contained in:
+12
-1
@@ -5,8 +5,13 @@ import { AppModule } from './app.module';
|
||||
import { ApiEnvelopeInterceptor } from './common/http/api-envelope.interceptor';
|
||||
import { AllExceptionsFilter } from './common/http/all-exceptions.filter';
|
||||
import { HttpRequestLoggingInterceptor } from './common/logging/http-request-logging.interceptor';
|
||||
import { StructuredLoggerService } from './common/logging/structured-logger.service';
|
||||
import { EnvConfig } from './config/env.schema';
|
||||
|
||||
type RequestWithStartedAt = {
|
||||
startedAt?: number;
|
||||
};
|
||||
|
||||
async function bootstrap(): Promise<void> {
|
||||
const app = await NestFactory.create(AppModule, {
|
||||
bufferLogs: true,
|
||||
@@ -14,12 +19,18 @@ async function bootstrap(): Promise<void> {
|
||||
});
|
||||
const config = app.get(ConfigService<EnvConfig, true>);
|
||||
const corsOrigin = config.get('CORS_ORIGIN', { infer: true });
|
||||
const structuredLogger = app.get(StructuredLoggerService);
|
||||
|
||||
app.use((request: RequestWithStartedAt, _response: unknown, next: () => void) => {
|
||||
request.startedAt = Date.now();
|
||||
next();
|
||||
});
|
||||
|
||||
app.enableCors({
|
||||
origin: corsOrigin ? corsOrigin.split(',').map((origin) => origin.trim()) : true,
|
||||
credentials: true,
|
||||
});
|
||||
app.useGlobalFilters(new AllExceptionsFilter());
|
||||
app.useGlobalFilters(new AllExceptionsFilter(structuredLogger));
|
||||
app.useGlobalInterceptors(
|
||||
app.get(HttpRequestLoggingInterceptor),
|
||||
new ApiEnvelopeInterceptor(),
|
||||
|
||||
Reference in New Issue
Block a user