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
This commit is contained in:
@@ -6,7 +6,7 @@ import axios from 'axios'
|
|||||||
*/
|
*/
|
||||||
const api = axios.create({
|
const api = axios.create({
|
||||||
baseURL: 'http://localhost:3000', // 设置后端服务地址
|
baseURL: 'http://localhost:3000', // 设置后端服务地址
|
||||||
timeout: 10000, // 请求超时时间
|
timeout: 60000, // 请求超时时间(60秒)
|
||||||
})
|
})
|
||||||
|
|
||||||
// 请求拦截器
|
// 请求拦截器
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
export const PromptString: string = `先给我说你统计了多少个项目。我只对辽宁、山东、江苏、浙江、福建、广东、广西、海南、河北这些地方的海上风电、海上光伏、漂浮式光伏、滩涂光伏、滩涂风电、渔光互补项目感兴趣。从我提供的这些工程里面找到我感兴趣的工程,无论如何至少推荐10个工程。如果没有推荐的,也要给出思考过程。`;
|
export const PromptString: string = `先给我说你统计了多少个项目。我只对辽宁、山东、江苏、浙江、福建、广东、广西、海南、河北这些地方的海上风电、海上光伏、漂浮式光伏、滩涂光伏、滩涂风电、渔光互补、风光互补项目感兴趣。从我提供的这些工程里面找到我感兴趣的工程,无论如何至少推荐10个工程。如果没有推荐的,也要给出思考过程。`;
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { Module } from '@nestjs/common';
|
import { MiddlewareConsumer, Module, NestModule } from '@nestjs/common';
|
||||||
import { ConfigModule } from '@nestjs/config';
|
import { ConfigModule } from '@nestjs/config';
|
||||||
import { ScheduleModule } from '@nestjs/schedule';
|
import { ScheduleModule } from '@nestjs/schedule';
|
||||||
import { ServeStaticModule } from '@nestjs/serve-static';
|
import { ServeStaticModule } from '@nestjs/serve-static';
|
||||||
@@ -9,6 +9,7 @@ import { KeywordsModule } from './keywords/keywords.module';
|
|||||||
import { CrawlerModule } from './crawler/crawler.module';
|
import { CrawlerModule } from './crawler/crawler.module';
|
||||||
import { TasksModule } from './schedule/schedule.module';
|
import { TasksModule } from './schedule/schedule.module';
|
||||||
import { LoggerModule } from './common/logger/logger.module';
|
import { LoggerModule } from './common/logger/logger.module';
|
||||||
|
import { LoggingMiddleware } from './common/logger/logging.middleware';
|
||||||
import { AiModule } from './ai/ai.module';
|
import { AiModule } from './ai/ai.module';
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
@@ -28,4 +29,8 @@ import { AiModule } from './ai/ai.module';
|
|||||||
AiModule,
|
AiModule,
|
||||||
],
|
],
|
||||||
})
|
})
|
||||||
export class AppModule {}
|
export class AppModule implements NestModule {
|
||||||
|
configure(consumer: MiddlewareConsumer) {
|
||||||
|
consumer.apply(LoggingMiddleware).forRoutes('*');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
29
src/common/logger/logging.middleware.ts
Normal file
29
src/common/logger/logging.middleware.ts
Normal file
@@ -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
|
import 'dotenv/config';
|
||||||
import { NestFactory } from '@nestjs/core';
|
import { NestFactory } from '@nestjs/core';
|
||||||
import { AppModule } from './app.module';
|
import { AppModule } from './app.module';
|
||||||
import { CustomLogger } from './common/logger/logger.service';
|
import { CustomLogger } from './common/logger/logger.service';
|
||||||
|
|||||||
Reference in New Issue
Block a user