- 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
30 lines
882 B
TypeScript
30 lines
882 B
TypeScript
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();
|
|
}
|
|
}
|