Skip to content

Commit

Permalink
logger
Browse files Browse the repository at this point in the history
  • Loading branch information
eldpswp99 committed Dec 2, 2023
1 parent d86a41e commit 6a8ff6f
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
9 changes: 7 additions & 2 deletions backend/src/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Module } from '@nestjs/common';
import { MiddlewareConsumer, Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { CoreModule } from './core/core.module';
Expand All @@ -9,6 +9,7 @@ import * as ormConfig from './core/ormconfig';
import { getEnvFilePath } from './core/getEnvFilePath';
import { ReviewModule } from './review/review.module';
import { AuthModule } from './auth/auth.module';
import { LoggerMiddleware } from './core/logger.middleware';

@Module({
imports: [
Expand All @@ -25,4 +26,8 @@ import { AuthModule } from './auth/auth.module';
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}
export class AppModule {
configure(consumer: MiddlewareConsumer): void {
consumer.apply(LoggerMiddleware).forRoutes('*');
}
}
30 changes: 30 additions & 0 deletions backend/src/core/logger.middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { Request, Response, NextFunction } from 'express';
import { Injectable, NestMiddleware, Logger } from '@nestjs/common';

@Injectable()
export class LoggerMiddleware implements NestMiddleware {
private logger = new Logger('HTTP');

use(request: Request, response: Response, next: NextFunction): void {
const startAt = process.hrtime();
const { method, originalUrl } = request;
const xForwardedFor = request.headers['x-forwarded-for'] ?? '';
const realIp = xForwardedFor.slice(0, xForwardedFor.indexOf(','));
const userAgent = request.get('user-agent') || '';

response.on('finish', () => {
const { statusCode } = response;
const diff = process.hrtime(startAt);
const responseTime = diff[0] * 1e3 + diff[1] * 1e-6;

this.logger.log(
`${method} ${originalUrl} ${statusCode} ${responseTime.toFixed(
3,
)}ms - ${userAgent} ${realIp}`,
);
this.logger.log(request.body);
});

next();
}
}
4 changes: 3 additions & 1 deletion backend/src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@ async function bootstrap() {
return CommandFactory.run(AppModule);
}

const app = await NestFactory.create(AppModule);
const app = await NestFactory.create(AppModule, {
logger: ['log'],
});
await setUpSwagger(app);
await appSetting(app);
if (process.env.NODE_ENV !== 'test') {
Expand Down
4 changes: 2 additions & 2 deletions backend/src/test/review/create-review.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -161,13 +161,13 @@ describe('Create Review test', () => {
expect(reviewEntity.menu).toEqual(['낙지탕탕이']);
});

it('영수증 에러나면 400', async () => {
it('긍부정 에러나면 400', async () => {
const image = await ImageFixture.create({});

const receiptImageId = await ImageFixture.create({});

jest.clearAllMocks();
(getReceiptOcr as jest.Mock).mockRejectedValue(new Error('error'));
(getReviewIsPositive as jest.Mock).mockRejectedValue(new Error('error'));

await supertest(testServer.getHttpServer())
.post('/reviews')
Expand Down

0 comments on commit 6a8ff6f

Please sign in to comment.