Add project scaffolding and development infrastructure: - Add environment configuration files (.env.development, .env.example) with database, JWT, security, AI integration, logging, and CORS settings - Add .gitignore to exclude build artifacts, logs, IDE files, and environment variables - Add .prettierrc with single quotes and trailing commas configuration - Add multi-stage Dockerfile with development, build, and production stages - Ad
46 lines
1.3 KiB
TypeScript
46 lines
1.3 KiB
TypeScript
import {
|
|
Injectable,
|
|
NestInterceptor,
|
|
ExecutionContext,
|
|
CallHandler,
|
|
Logger,
|
|
} from '@nestjs/common';
|
|
import { Observable } from 'rxjs';
|
|
import { tap } from 'rxjs/operators';
|
|
import { Request } from 'express';
|
|
|
|
@Injectable()
|
|
export class LoggingInterceptor implements NestInterceptor {
|
|
private readonly logger = new Logger('HTTP');
|
|
|
|
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
|
|
const request = context.switchToHttp().getRequest<Request>();
|
|
const { method, url, ip } = request;
|
|
const userAgent = request.get('user-agent') || '';
|
|
const userId = (request as any).user?.sub || 'anonymous';
|
|
|
|
const now = Date.now();
|
|
|
|
return next.handle().pipe(
|
|
tap({
|
|
next: () => {
|
|
const response = context.switchToHttp().getResponse();
|
|
const { statusCode } = response;
|
|
const contentLength = response.get('content-length') || 0;
|
|
const duration = Date.now() - now;
|
|
|
|
this.logger.log(
|
|
`${method} ${url} ${statusCode} ${contentLength} - ${duration}ms - ${userId} - ${ip} - ${userAgent}`,
|
|
);
|
|
},
|
|
error: (error) => {
|
|
const duration = Date.now() - now;
|
|
this.logger.error(
|
|
`${method} ${url} ERROR - ${duration}ms - ${userId} - ${ip} - ${error.message}`,
|
|
);
|
|
},
|
|
}),
|
|
);
|
|
}
|
|
}
|