Skip to content

Commit

Permalink
Merge pull request #79 from Boost-Coder/feature/logging-#77
Browse files Browse the repository at this point in the history
[#77] Logger 설정
  • Loading branch information
koomin1227 authored Apr 18, 2024
2 parents 91b9caf + a9d2dba commit 5cc132f
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ import { UserModule } from './user/user.module';
import { StatModule } from './stat/stat.module';
import { RankModule } from './rank/rank.module';
import * as process from 'process';
import { HttpLoggerInterceptor } from './utils/httpLoggerInterceptor';
import { APP_FILTER, APP_INTERCEPTOR } from '@nestjs/core';
import { HttpExceptionFilter } from './utils/httpExceptionFilter';
import { WinstonModule } from 'nest-winston';
import { transports } from 'winston';

@Module({
imports: [
Expand All @@ -25,6 +30,16 @@ import * as process from 'process';
RankModule,
],
controllers: [AppController],
providers: [AppService],
providers: [
AppService,
{
provide: APP_INTERCEPTOR,
useClass: HttpLoggerInterceptor,
},
{
provide: APP_FILTER,
useClass: HttpExceptionFilter,
},
],
})
export class AppModule {}
31 changes: 31 additions & 0 deletions src/utils/httpExceptionFilter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import {
ArgumentsHost,
Catch,
ExceptionFilter,
Logger,
UnauthorizedException,
} from '@nestjs/common';
import { Request, Response } from 'express';

@Catch(UnauthorizedException)
export class HttpExceptionFilter implements ExceptionFilter {
private readonly logger = new Logger();

catch(exception: any, host: ArgumentsHost): any {
const ctx = host.switchToHttp();
const response = ctx.getResponse<Response>();
const request = ctx.getRequest<Request>();
const status = exception.getStatus();

this.logger.error(
`${request.method} ${request.url} ${new Date()} [Error] ${exception} `,
'HTTP ERROR',
);

response.status(status).json({
statusCode: status,
path: request.url,
message: '유저 인증 실패 , Token 을 확인하세요',
});
}
}
42 changes: 42 additions & 0 deletions src/utils/httpLoggerInterceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import {
CallHandler,
ExecutionContext,
NestInterceptor,
Injectable,
Logger,
} from '@nestjs/common';
import { Observable, throwError } from 'rxjs';
import { tap, catchError } from 'rxjs/operators';
import { Request } from 'express';

@Injectable()
export class HttpLoggerInterceptor implements NestInterceptor {
private readonly logger = new Logger();
intercept(
context: ExecutionContext,
next: CallHandler<any>,
): Observable<any> | Promise<Observable<any>> {
const request: Request = context.switchToHttp().getRequest();
this.logger.debug(request.body, 'HTTP');

return next.handle().pipe(
tap((data) => {
const request = context.switchToHttp().getRequest();
const response = context.switchToHttp().getResponse();
const body = JSON.stringify(request.body);
this.logger.debug(
`<${request.userId}> ${request.method} ${request.url} ${body} ${new Date()} ${response.statusCode} ${response.statusMessage}`,
'HTTP',
);
}),
catchError((err) => {
const body = JSON.stringify(request.body);
this.logger.error(
`<${request['userId']}> ${request.method} ${request.url} ${body} ${new Date()} [Error]${err}`,
'HTTP ERROR',
);
return throwError(err);
}),
);
}
}

0 comments on commit 5cc132f

Please sign in to comment.