Skip to content

Commit

Permalink
Merge pull request #203 from peageon/feat/log
Browse files Browse the repository at this point in the history
[최진수] : 서버 로거 구현
  • Loading branch information
yunuo46 authored Dec 6, 2023
2 parents fa097c7 + ae9367e commit 19dfcc2
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
7 changes: 6 additions & 1 deletion back/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import { AuthModule } from './modules/auth/auth.module';
import typeOrmConfig from './config/ormconfig';
import { JwtModule } from '@nestjs/jwt';
import { UserModule } from './modules/user/user.module';
import { APP_INTERCEPTOR } from '@nestjs/core';
import { ControllerLoggerInterceptor } from './common/interceptors/log.interceptor';

@Module({
imports: [
Expand All @@ -17,6 +19,9 @@ import { UserModule } from './modules/user/user.module';
})
],
controllers: [AppController],
providers: [AppService]
providers: [
AppService,
{ provide: APP_INTERCEPTOR, useClass: ControllerLoggerInterceptor }
]
})
export class AppModule {}
28 changes: 28 additions & 0 deletions back/src/common/interceptors/log.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import {
Injectable,
NestInterceptor,
ExecutionContext,
CallHandler,
Logger
} from '@nestjs/common';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';

@Injectable()
export class ControllerLoggerInterceptor implements NestInterceptor {
intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
const controllerName = context.getClass().name;
const logger = new Logger(controllerName);
const request = context.switchToHttp().getRequest();
const method = request.method;
const url = request.originalUrl;
const start = Date.now();
logger.verbose(`>>> IN: ${method} ${url}`);
return next.handle().pipe(
tap(() => {
const end = Date.now();
logger.verbose(`<<< OUT: ${method} ${url} ${end - start}ms`);
})
);
}
}
5 changes: 4 additions & 1 deletion back/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,10 @@ import { NestFactory } from '@nestjs/core';
import { setupSwagger } from 'src/common/util/swagger';
import { AppModule } from './app.module';
import { ValidationPipe } from '@nestjs/common/pipes';
import { Logger } from '@nestjs/common';

async function bootstrap() {
const logger = new Logger('Main');
const app = await NestFactory.create(AppModule);
app.setGlobalPrefix('api');
app.useGlobalPipes(
Expand All @@ -15,6 +17,7 @@ async function bootstrap() {
})
);
setupSwagger(app);
await app.listen(3000);
logger.log(`Server is running on port ${process.env.PORT}`);
await app.listen(process.env.PORT);
}
bootstrap();

0 comments on commit 19dfcc2

Please sign in to comment.