From 811ad927f386bc2f5659c61e82b4248f81dfa322 Mon Sep 17 00:00:00 2001 From: dmy Date: Thu, 15 Jan 2026 21:52:40 +0800 Subject: [PATCH] feat(logging): add logging middleware for HTTP requests and update API timeout - Introduce LoggingMiddleware to log incoming requests and their response times - Update AppModule to apply the new logging middleware globally - Increase API request timeout from 10 seconds to 60 seconds - Modify PromptString to include additional project types of interest --- frontend/src/utils/api.ts | 2 +- src/ai/Prompt.ts | 2 +- src/app.module.ts | 9 ++++++-- src/common/logger/logging.middleware.ts | 29 +++++++++++++++++++++++++ src/main.ts | 1 + 5 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 src/common/logger/logging.middleware.ts diff --git a/frontend/src/utils/api.ts b/frontend/src/utils/api.ts index 86c78e7..ffa8e6d 100644 --- a/frontend/src/utils/api.ts +++ b/frontend/src/utils/api.ts @@ -6,7 +6,7 @@ import axios from 'axios' */ const api = axios.create({ baseURL: 'http://localhost:3000', // 设置后端服务地址 - timeout: 10000, // 请求超时时间 + timeout: 60000, // 请求超时时间(60秒) }) // 请求拦截器 diff --git a/src/ai/Prompt.ts b/src/ai/Prompt.ts index ec88411..af0c3e5 100644 --- a/src/ai/Prompt.ts +++ b/src/ai/Prompt.ts @@ -1 +1 @@ -export const PromptString: string = `先给我说你统计了多少个项目。我只对辽宁、山东、江苏、浙江、福建、广东、广西、海南、河北这些地方的海上风电、海上光伏、漂浮式光伏、滩涂光伏、滩涂风电、渔光互补项目感兴趣。从我提供的这些工程里面找到我感兴趣的工程,无论如何至少推荐10个工程。如果没有推荐的,也要给出思考过程。`; +export const PromptString: string = `先给我说你统计了多少个项目。我只对辽宁、山东、江苏、浙江、福建、广东、广西、海南、河北这些地方的海上风电、海上光伏、漂浮式光伏、滩涂光伏、滩涂风电、渔光互补、风光互补项目感兴趣。从我提供的这些工程里面找到我感兴趣的工程,无论如何至少推荐10个工程。如果没有推荐的,也要给出思考过程。`; diff --git a/src/app.module.ts b/src/app.module.ts index 2fd4c24..7897e8e 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -1,4 +1,4 @@ -import { Module } from '@nestjs/common'; +import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common'; import { ConfigModule } from '@nestjs/config'; import { ScheduleModule } from '@nestjs/schedule'; import { ServeStaticModule } from '@nestjs/serve-static'; @@ -9,6 +9,7 @@ import { KeywordsModule } from './keywords/keywords.module'; import { CrawlerModule } from './crawler/crawler.module'; import { TasksModule } from './schedule/schedule.module'; import { LoggerModule } from './common/logger/logger.module'; +import { LoggingMiddleware } from './common/logger/logging.middleware'; import { AiModule } from './ai/ai.module'; @Module({ @@ -28,4 +29,8 @@ import { AiModule } from './ai/ai.module'; AiModule, ], }) -export class AppModule {} +export class AppModule implements NestModule { + configure(consumer: MiddlewareConsumer) { + consumer.apply(LoggingMiddleware).forRoutes('*'); + } +} diff --git a/src/common/logger/logging.middleware.ts b/src/common/logger/logging.middleware.ts new file mode 100644 index 0000000..d8b7e27 --- /dev/null +++ b/src/common/logger/logging.middleware.ts @@ -0,0 +1,29 @@ +import { Injectable, NestMiddleware } from '@nestjs/common'; +import { Request, Response, NextFunction } from 'express'; +import { CustomLogger } from './logger.service'; + +@Injectable() +export class LoggingMiddleware implements NestMiddleware { + constructor(private readonly logger: CustomLogger) { + this.logger.setContext('HTTP'); + } + + use(req: Request, res: Response, next: NextFunction) { + const { method, originalUrl, ip } = req; + const userAgent = req.get('user-agent') || ''; + const startTime = Date.now(); + + // 收到请求时立即输出 + this.logger.debug(`--> ${method} ${originalUrl} - ${ip} - ${userAgent}`); + + res.on('finish', () => { + const { statusCode } = res; + const duration = Date.now() - startTime; + this.logger.debug( + `<-- ${method} ${originalUrl} ${statusCode} - ${duration}ms`, + ); + }); + + next(); + } +} diff --git a/src/main.ts b/src/main.ts index 3d65095..725bc57 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,3 +1,4 @@ +import 'dotenv/config'; import { NestFactory } from '@nestjs/core'; import { AppModule } from './app.module'; import { CustomLogger } from './common/logger/logger.service';