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:
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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user