Skip to content

Commit

Permalink
debbuger service as logger for request and response
Browse files Browse the repository at this point in the history
  • Loading branch information
andrechristikan committed Feb 14, 2024
1 parent a137b24 commit 8b6d4eb
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 17 deletions.
2 changes: 2 additions & 0 deletions src/common/common.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import { DATABASE_CONNECTION_NAME } from 'src/common/database/constants/database
import { ENUM_APP_ENVIRONMENT } from 'src/app/constants/app.enum.constant';
import { APP_LANGUAGE } from 'src/app/constants/app.constant';
import { PolicyModule } from 'src/common/policy/policy.module';
import { DebuggerLoggerModule } from 'src/common/debugger/debugger.logger.module';

@Module({
controllers: [],
Expand Down Expand Up @@ -149,6 +150,7 @@ import { PolicyModule } from 'src/common/policy/policy.module';
ResponseModule,
RequestModule,
PolicyModule,
DebuggerLoggerModule,
ApiKeyModule,
AuthModule.forRoot(),
],
Expand Down
17 changes: 17 additions & 0 deletions src/common/debugger/debugger.logger.module.ts
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 src/common/debugger/interceptors/debugger.logger.interceptor.ts
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();
}
}
15 changes: 1 addition & 14 deletions src/common/debugger/services/debugger.options.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ export class DebuggerOptionService implements IDebuggerOptionService {
const maxSize = this.configService.get<string>('debugger.maxSize');
const maxFiles = this.configService.get<string>('debugger.maxFiles');

const transports: Transport[] | Transport = [
new winston.transports.Console(),
];
const transports: Transport[] | Transport = [];

if (writeIntoFile) {
transports.push(
Expand All @@ -44,17 +42,6 @@ export class DebuggerOptionService implements IDebuggerOptionService {
level: 'info',
})
);
transports.push(
new DailyRotateFile({
filename: `%DATE%.log`,
dirname: `logs/${DEBUGGER_NAME}/debug`,
datePattern: 'YYYY-MM-DD',
zippedArchive: true,
maxSize: maxSize,
maxFiles: maxFiles,
level: 'debug',
})
);
}

const loggerOptions: LoggerOptions = {
Expand Down
1 change: 0 additions & 1 deletion src/common/request/middleware/request.middleware.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import { RequestIdMiddleware } from 'src/common/request/middleware/id/request.id
import { RequestTimestampMiddleware } from 'src/common/request/middleware/timestamp/request.timestamp.middleware';
import { RequestTimezoneMiddleware } from 'src/common/request/middleware/timezone/request.timezone.middleware';
import { RequestUserAgentMiddleware } from 'src/common/request/middleware/user-agent/request.user-agent.middleware';

import { RequestVersionMiddleware } from 'src/common/request/middleware/version/request.version.middleware';

@Module({})
Expand Down
4 changes: 2 additions & 2 deletions test/debugger/debugger.options.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ describe('DebuggerOptionService', () => {
expect(loggerOptions.format).toBeDefined;

expect(loggerOptions).toHaveProperty('transports');
expect(loggerOptions.transports.length).toEqual(4);
expect(loggerOptions.transports.length).toEqual(2);
});

it('should return an object containing necessary options for winston logger, can into console', () => {
Expand All @@ -70,7 +70,7 @@ describe('DebuggerOptionService', () => {
expect(loggerOptions.format).toBeDefined;

expect(loggerOptions).toHaveProperty('transports');
expect(loggerOptions.transports.length).toEqual(1);
expect(loggerOptions.transports.length).toEqual(0);
});
});
});

0 comments on commit 8b6d4eb

Please sign in to comment.