mirror of
https://github.com/immich-app/immich.git
synced 2025-01-13 15:35:15 +02:00
feat(server): logging interceptor (#8859)
This commit is contained in:
parent
c70d9f9055
commit
14b1425e98
@ -15,6 +15,7 @@ import { entities } from 'src/entities';
|
|||||||
import { AuthGuard } from 'src/middleware/auth.guard';
|
import { AuthGuard } from 'src/middleware/auth.guard';
|
||||||
import { ErrorInterceptor } from 'src/middleware/error.interceptor';
|
import { ErrorInterceptor } from 'src/middleware/error.interceptor';
|
||||||
import { FileUploadInterceptor } from 'src/middleware/file-upload.interceptor';
|
import { FileUploadInterceptor } from 'src/middleware/file-upload.interceptor';
|
||||||
|
import { LoggingInterceptor } from 'src/middleware/logging.interceptor';
|
||||||
import { repositories } from 'src/repositories';
|
import { repositories } from 'src/repositories';
|
||||||
import { services } from 'src/services';
|
import { services } from 'src/services';
|
||||||
import { ApiService } from 'src/services/api.service';
|
import { ApiService } from 'src/services/api.service';
|
||||||
@ -26,6 +27,7 @@ const common = [...services, ...repositories];
|
|||||||
const middleware = [
|
const middleware = [
|
||||||
FileUploadInterceptor,
|
FileUploadInterceptor,
|
||||||
{ provide: APP_PIPE, useValue: new ValidationPipe({ transform: true, whitelist: true }) },
|
{ provide: APP_PIPE, useValue: new ValidationPipe({ transform: true, whitelist: true }) },
|
||||||
|
{ provide: APP_INTERCEPTOR, useClass: LoggingInterceptor },
|
||||||
{ provide: APP_INTERCEPTOR, useClass: ErrorInterceptor },
|
{ provide: APP_INTERCEPTOR, useClass: ErrorInterceptor },
|
||||||
{ provide: APP_GUARD, useClass: AuthGuard },
|
{ provide: APP_GUARD, useClass: AuthGuard },
|
||||||
];
|
];
|
||||||
|
28
server/src/middleware/logging.interceptor.ts
Normal file
28
server/src/middleware/logging.interceptor.ts
Normal file
@ -0,0 +1,28 @@
|
|||||||
|
import { CallHandler, ExecutionContext, Inject, Injectable, NestInterceptor } from '@nestjs/common';
|
||||||
|
import { Observable, finalize } from 'rxjs';
|
||||||
|
import { ILoggerRepository } from 'src/interfaces/logger.interface';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class LoggingInterceptor implements NestInterceptor {
|
||||||
|
constructor(@Inject(ILoggerRepository) private logger: ILoggerRepository) {
|
||||||
|
this.logger.setContext(LoggingInterceptor.name);
|
||||||
|
}
|
||||||
|
|
||||||
|
intercept(context: ExecutionContext, next: CallHandler<any>): Observable<any> {
|
||||||
|
const handler = context.switchToHttp();
|
||||||
|
const req = handler.getRequest();
|
||||||
|
const res = handler.getResponse();
|
||||||
|
|
||||||
|
const { method, ip, path } = req;
|
||||||
|
|
||||||
|
const start = performance.now();
|
||||||
|
return next.handle().pipe(
|
||||||
|
finalize(() => {
|
||||||
|
const finish = performance.now();
|
||||||
|
const duration = (finish - start).toFixed(2);
|
||||||
|
const { statusCode } = res;
|
||||||
|
this.logger.verbose(`${method} ${path} ${statusCode} ${duration}ms ${ip}`);
|
||||||
|
}),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user