-
Notifications
You must be signed in to change notification settings - Fork 197
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
debbuger service as logger for request and response
- Loading branch information
1 parent
a137b24
commit 8b6d4eb
Showing
6 changed files
with
98 additions
and
17 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,17 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { APP_INTERCEPTOR } from '@nestjs/core'; | ||
import { DebuggerModule } from 'src/common/debugger/debugger.module'; | ||
import { DebuggerInterceptor } from 'src/common/debugger/interceptors/debugger.logger.interceptor'; | ||
|
||
@Module({ | ||
providers: [ | ||
{ | ||
provide: APP_INTERCEPTOR, | ||
useClass: DebuggerInterceptor, | ||
}, | ||
], | ||
exports: [], | ||
controllers: [], | ||
imports: [DebuggerModule], | ||
}) | ||
export class DebuggerLoggerModule {} |
76 changes: 76 additions & 0 deletions
76
src/common/debugger/interceptors/debugger.logger.interceptor.ts
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,76 @@ | ||
import { | ||
Injectable, | ||
NestInterceptor, | ||
ExecutionContext, | ||
CallHandler, | ||
} from '@nestjs/common'; | ||
import { Observable } from 'rxjs'; | ||
import { tap } from 'rxjs/operators'; | ||
import { ConfigService } from '@nestjs/config'; | ||
import { DebuggerService } from 'src/common/debugger/services/debugger.service'; | ||
import { IRequestApp } from 'src/common/request/interfaces/request.interface'; | ||
import { Response } from 'express'; | ||
|
||
@Injectable() | ||
export class DebuggerInterceptor implements NestInterceptor<Promise<any>> { | ||
private readonly writeIntoFile: boolean; | ||
|
||
constructor( | ||
private readonly configService: ConfigService, | ||
private readonly debuggerService: DebuggerService | ||
) { | ||
this.writeIntoFile = this.configService.get<boolean>( | ||
'debugger.writeIntoFile' | ||
); | ||
} | ||
|
||
async intercept( | ||
context: ExecutionContext, | ||
next: CallHandler | ||
): Promise<Observable<Promise<any> | string>> { | ||
if (context.getType() === 'http') { | ||
const request: IRequestApp = context | ||
.switchToHttp() | ||
.getRequest<IRequestApp>(); | ||
|
||
console.log('aaa'); | ||
|
||
if (this.writeIntoFile) { | ||
console.log('bbb'); | ||
this.debuggerService.info({ | ||
type: 'request', | ||
method: request.method, | ||
path: request.path, | ||
originalUrl: request.originalUrl, | ||
params: request.params, | ||
body: request.body, | ||
baseUrl: request.baseUrl, | ||
query: request.query, | ||
ip: request.ip, | ||
hostname: request.hostname, | ||
protocol: request.protocol, | ||
}); | ||
} | ||
|
||
return next.handle().pipe( | ||
tap(() => { | ||
console.log('ccc'); | ||
const response: Response = context | ||
.switchToHttp() | ||
.getResponse<Response>(); | ||
|
||
if (this.writeIntoFile) { | ||
console.log('ddd'); | ||
this.debuggerService.info({ | ||
type: 'response', | ||
statusCode: response.statusCode, | ||
message: response.statusMessage, | ||
}); | ||
} | ||
}) | ||
); | ||
} | ||
|
||
return next.handle(); | ||
} | ||
} |
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
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