Files
immich/server/src/middleware/logging.interceptor.ts
Jason Rasmussen 1f5393d02c WIP
2025-02-11 12:30:21 -05:00

43 lines
1.4 KiB
TypeScript

import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nestjs/common';
import { Observable, finalize } from 'rxjs';
import { LoggingRepository } from 'src/repositories/logging.repository';
import { getReqRes } from 'src/utils/request';
const maxArrayLength = 100;
const replacer = (key: string, value: unknown) => {
if (key.toLowerCase().includes('password')) {
return '********';
}
if (Array.isArray(value) && value.length > maxArrayLength) {
return [...value.slice(0, maxArrayLength), `...and ${value.length - maxArrayLength} more`];
}
return value;
};
@Injectable()
export class LoggingInterceptor implements NestInterceptor {
constructor(private logger: LoggingRepository) {
this.logger.setContext(LoggingInterceptor.name);
}
intercept(context: ExecutionContext, next: CallHandler<any>): Observable<any> {
const { req, res } = getReqRes(context);
const { method, ip, url } = req;
const start = performance.now();
return next.handle().pipe(
finalize(() => {
const finish = performance.now();
const duration = (finish - start).toFixed(2);
this.logger.debug(`${method} ${url} ${res?.statusCode || ''} ${duration}ms ${ip}`);
if (req.body && Object.keys(req.body).length > 0) {
this.logger.verbose(JSON.stringify(req.body, replacer));
}
}),
);
}
}