Skip to content

Commit

Permalink
Merge pull request #45 from HangHae-plus-2-9/feature-common
Browse files Browse the repository at this point in the history
Feat: add http logging interceptor
  • Loading branch information
HC-kang authored Oct 23, 2023
2 parents 721c0ef + 3e913e0 commit 08f2707
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 6 deletions.
10 changes: 9 additions & 1 deletion src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ import { WinstonContextModule } from './winston-context/winston-context.module';
import { HttpExceptionFilter } from './common/filters/http.exception.filter';
import { FavoriteModule } from './modules/favorite/favorite.module';
import { CartModule } from './modules/cart/cart.module';
import { HttpLoggerInterceptor } from './common/interceptors';
import { APP_INTERCEPTOR } from '@nestjs/core';
// import { PaymentsModule } from './modules/payments/payments.module';

@Module({
Expand All @@ -33,7 +35,13 @@ import { CartModule } from './modules/cart/cart.module';
FavoriteModule,
CartModule,
],
providers: [HttpExceptionFilter],
providers: [
HttpExceptionFilter,
{
provide: APP_INTERCEPTOR,
useClass: HttpLoggerInterceptor,
},
],
})
export class AppModule implements NestModule {
constructor(private readonly als: AsyncLocalStorage<any>) {}
Expand Down
46 changes: 46 additions & 0 deletions src/common/interceptors/http-logger.interceptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { WinstonContextLogger } from '@/winston-context/winston-context.logger';
import {
Injectable,
NestInterceptor,
ExecutionContext,
CallHandler,
} from '@nestjs/common';
import { Observable } from 'rxjs';
import { tap } from 'rxjs/operators';
import { formattedString } from '../utils';

@Injectable()
export class HttpLoggerInterceptor implements NestInterceptor {
constructor(private readonly cLogger: WinstonContextLogger) {}

intercept(context: ExecutionContext, next: CallHandler): Observable<any> {
const now = Date.now();
const httpContext = context.switchToHttp();
const req = httpContext.getRequest();
const res = httpContext.getResponse();
const { method, originalUrl, ip, headers: reqHeaders, body: reqBody } = req;

Check warning on line 21 in src/common/interceptors/http-logger.interceptor.ts

View workflow job for this annotation

GitHub Actions / test nestjs

'reqHeaders' is assigned a value but never used

Check warning on line 21 in src/common/interceptors/http-logger.interceptor.ts

View workflow job for this annotation

GitHub Actions / test nestjs

'reqBody' is assigned a value but never used

Check warning on line 21 in src/common/interceptors/http-logger.interceptor.ts

View workflow job for this annotation

GitHub Actions / test nestjs

'reqHeaders' is assigned a value but never used

Check warning on line 21 in src/common/interceptors/http-logger.interceptor.ts

View workflow job for this annotation

GitHub Actions / test nestjs

'reqBody' is assigned a value but never used

return next.handle().pipe(
tap(() => {
const { statusCode } = res;
const contentLength = res.get('content-length') || 0;
const userAgent = req.get('user-agent') || '';
const logMessage = `${method} ${originalUrl} ${statusCode} ${contentLength} ${
Date.now() - now
}ms - ${userAgent} ${ip}`;

if (statusCode >= 500) {
this.cLogger.error(formattedString(logMessage));
} else if (statusCode >= 400) {
this.cLogger.warn(formattedString(logMessage));
} else {
this.cLogger.log(formattedString(logMessage));
}

// 추가적으로 reqHeaders, reqBody를 로깅하고 싶다면 아래 주석을 해제하세요.
// this.cLogger.verbose(`Headers: ${formattedString(reqHeaders)}`);
// this.cLogger.verbose(`Body: ${formattedString(reqBody)}`);
}),
);
}
}
1 change: 1 addition & 0 deletions src/common/interceptors/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from './transform.interceptors';
export * from './http-logger.interceptor';
2 changes: 2 additions & 0 deletions src/common/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,5 @@ export const getEndOfDay = (date: Date) => {
export const createNumericId = () => {
return Math.floor(Math.random() * 2000000000);
};

export const formattedString = (str: string) => JSON.stringify(str, null, 2);
10 changes: 5 additions & 5 deletions src/config/winston.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,13 @@ const customLogFormat = combine(
printf((aLog) => {
const nestReqId = aLog.alsCtx?.nestReqId;
const nestReqIdStr = nestReqId //
? `[${nestReqId}]`
? `${nestReqId}`
: '';
const stackStr =
aLog.stack && aLog.stack[0] !== undefined //
? ` \n ${aLog.stack}`
: '';
return `[${aLog.timestamp}] [${aLog.level}] ${nestReqIdStr}: ${aLog.message}${stackStr}`;
return `[${aLog.timestamp}] [${aLog.level}] [${nestReqIdStr}]: ${aLog.message}${stackStr}`;
}),
);

Expand All @@ -71,9 +71,9 @@ const cloudwatchConfig = {
const { level, message, additionalInfo, alsCtx } = aLog;
const nestReqId = alsCtx?.nestReqId;
const nestReqIdStr = nestReqId //
? `[${nestReqId}]`
: '[]';
return `[${level}] ${nestReqIdStr}: ${message} \nAdditional Info: ${JSON.stringify(
? `${nestReqId}`
: '';
return `[${level}] [${nestReqIdStr}]: ${message} \nAdditional Info: ${JSON.stringify(
additionalInfo,
)}`;
},
Expand Down

0 comments on commit 08f2707

Please sign in to comment.