Skip to content

Commit

Permalink
Merge pull request #174 from boostcampwm2023/BE-setHttpLogger-#173
Browse files Browse the repository at this point in the history
[BE/#173] API 서버 로그 기록
  • Loading branch information
koomin1227 authored Nov 25, 2023
2 parents 37cb0ac + efb0978 commit 87ba6fc
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 2 deletions.
64 changes: 64 additions & 0 deletions BE/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions BE/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"class-transformer": "^0.5.1",
"class-validator": "^0.14.0",
"crypto": "^1.0.1",
"morgan": "^1.10.0",
"multer-s3": "^3.0.1",
"mysql2": "^3.6.3",
"nest-winston": "^1.9.4",
Expand Down
2 changes: 1 addition & 1 deletion BE/src/config/winston.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
} from 'nest-winston';

export const winstonOptions = new winston.transports.Console({
level: process.env.NODE_ENV === 'prod' ? 'info' : 'silly',
level: process.env.NODE_ENV === 'prod' ? 'http' : 'silly',
format: winston.format.combine(
winston.format.timestamp(),
winston.format.colorize({ all: true }),
Expand Down
4 changes: 3 additions & 1 deletion BE/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { WINSTON_MODULE_NEST_PROVIDER, WinstonModule } from 'nest-winston';
import { dailyOption, winstonOptions } from './config/winston.config';
import * as winstonDaily from 'winston-daily-rotate-file';
import { ValidationPipe } from '@nestjs/common';
import { HttpLoggerInterceptor } from './utils/httpLogger.interceptor';

async function bootstrap() {
const app = await NestFactory.create(AppModule, {
Expand All @@ -16,14 +17,15 @@ async function bootstrap() {
],
}),
});

app.useGlobalInterceptors(new HttpLoggerInterceptor());
app.useLogger(app.get(WINSTON_MODULE_NEST_PROVIDER));
app.useGlobalPipes(
new ValidationPipe({
transform: true,
}),
);
setupSwagger(app);

await app.listen(3000);
}
bootstrap();
2 changes: 2 additions & 0 deletions BE/src/post/post.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ import { FilesInterceptor } from '@nestjs/platform-express';
import { PostCreateDto } from './dto/postCreate.dto';
import { MultiPartBody } from '../utils/multiPartBody.decorator';
import { PostListDto } from './dto/postList.dto';
import { http } from 'winston';
import { HttpLoggerInterceptor } from 'src/utils/httpLogger.interceptor';

@Controller('posts')
@ApiTags('posts')
Expand Down
43 changes: 43 additions & 0 deletions BE/src/utils/httpLogger.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import {
CallHandler,
ExecutionContext,
NestInterceptor,
Injectable,
Logger,
HttpException,
BadGatewayException,
} from '@nestjs/common';
import { request } from 'http';
import { Observable, throwError } from 'rxjs';
import { map, tap, catchError } from 'rxjs/operators';

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

return next.handle().pipe(
tap((data) => {
const request = context.switchToHttp().getRequest();
const response = context.switchToHttp().getResponse();
this.logger.log(
`${request.url} ${request.method} ${request.ip}/${response.statusCode} ${response.statusMessage}`,
'HTTP',
);
}),
catchError((err: HttpException) => {
this.logger.log(
`${request.url} ${request.method} ${
request.ip
}/${err.getStatus()} ${err.getResponse()}`,
'HTTP ERROR',
);
return throwError(err);
}),
);
}
}

0 comments on commit 87ba6fc

Please sign in to comment.