-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #45 from HangHae-plus-2-9/feature-common
Feat: add http logging interceptor
- Loading branch information
Showing
5 changed files
with
63 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / test nestjs
Check warning on line 21 in src/common/interceptors/http-logger.interceptor.ts GitHub Actions / test nestjs
Check warning on line 21 in src/common/interceptors/http-logger.interceptor.ts GitHub Actions / test nestjs
|
||
|
||
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)}`); | ||
}), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export * from './transform.interceptors'; | ||
export * from './http-logger.interceptor'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters