Skip to content

Commit

Permalink
refactor(logger): refactor logg message when external API call
Browse files Browse the repository at this point in the history
  • Loading branch information
mikemajesty committed Dec 7, 2023
1 parent 5eb5b42 commit ac6697a
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 16 deletions.
17 changes: 12 additions & 5 deletions src/infra/logger/service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,14 @@ export class LoggerService implements ILoggerAdapter {

const response =
error instanceof BaseException
? { statusCode: error['statusCode'], message: error?.message, ...error?.parameters }
? {
statusCode: error['statusCode'],
message: [
(error?.parameters as { responseData?: { error?: { message?: string } } })?.responseData.error.message,
error?.message
].find(Boolean),
...error?.parameters
}
: errorResponse?.value();

const type = {
Expand Down Expand Up @@ -121,8 +128,9 @@ export class LoggerService implements ILoggerAdapter {
levelFirst: true,
ignore: 'pid,hostname',
quietReqLogger: true,
messageFormat: (log: unknown, messageKey: string) => {
const message = log[String(messageKey)];
// eslint-disable-next-line @typescript-eslint/no-explicit-any
messageFormat: (log: any, messageKey: string) => {
const message = [log?.responseData?.error?.message, log[String(messageKey)]].find(Boolean);
if (this.app) {
return `[${blue(this.app)}] ${message}`;
}
Expand Down Expand Up @@ -164,8 +172,7 @@ export class LoggerService implements ILoggerAdapter {
},
res: pino.stdSerializers.res
},
// eslint-disable-next-line @typescript-eslint/no-explicit-any
customProps: (req: any): any => {
customProps: (req: IncomingMessage & { context: string; protocol: string }): object => {
const context = req.context;

const traceid = [req?.headers?.traceid, req.id].find(Boolean);
Expand Down
4 changes: 2 additions & 2 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ async function bootstrap() {
app.useGlobalFilters(new AppExceptionFilter(loggerService));

app.useGlobalInterceptors(
new ExceptionInterceptor(loggerService),
new HttpLoggerInterceptor(),
new ExceptionInterceptor(),
new HttpLoggerInterceptor(loggerService),
new TracingInterceptor(loggerService),
new MetricsInterceptor()
);
Expand Down
1 change: 1 addition & 0 deletions src/utils/axios.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ export const interceptAxiosResponseError = (error, logger: ILoggerAdapter) => {
].find(Boolean);

const curl = AxiosConverter.getCurl(error);

logger.error(
new BaseException(typeof error['code'] === 'string' ? error['code'] : message, status, {
curl,
Expand Down
3 changes: 2 additions & 1 deletion src/utils/filters/http-exception.filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ export class AppExceptionFilter implements ExceptionFilter {

if (exception instanceof AxiosError) {
if ((exception as AxiosError).response?.data) {
return (exception as AxiosError).message;
// eslint-disable-next-line @typescript-eslint/no-explicit-any
return [(exception as any)?.response?.data?.error?.message, (exception as AxiosError).message].find(Boolean);
}
}

Expand Down
9 changes: 1 addition & 8 deletions src/utils/interceptors/http-exception.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,8 @@ import { Observable } from 'rxjs';
import { catchError } from 'rxjs/operators';
import { ZodError } from 'zod';

import { ILoggerAdapter } from '@/infra/logger';

@Injectable()
export class ExceptionInterceptor implements NestInterceptor {
constructor(private readonly logger: ILoggerAdapter) {}

intercept(executionContext: ExecutionContext, next: CallHandler): Observable<unknown> {
return next.handle().pipe(
catchError((error) => {
Expand All @@ -19,9 +15,6 @@ export class ExceptionInterceptor implements NestInterceptor {
const headers = executionContext.getArgs()[0]?.headers;

const request = executionContext.switchToHttp().getRequest();
const res = executionContext.switchToHttp().getResponse();

this.logger.logger(request, res);

this.sanitizeExternalError(error);

Expand Down Expand Up @@ -51,9 +44,9 @@ export class ExceptionInterceptor implements NestInterceptor {

return [
error.status,
error?.response?.status,
error?.response?.data?.code,
error?.response?.data?.error?.code,
error?.response?.status,
500
].find(Boolean);
}
Expand Down
7 changes: 7 additions & 0 deletions src/utils/interceptors/http-logger.interceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@ import { CallHandler, ExecutionContext, Injectable, NestInterceptor } from '@nes
import { Observable } from 'rxjs';
import { v4 as uuidv4 } from 'uuid';

import { ILoggerAdapter } from '@/infra/logger';

@Injectable()
export class HttpLoggerInterceptor implements NestInterceptor {
constructor(private readonly logger: ILoggerAdapter) {}

intercept(executionContext: ExecutionContext, next: CallHandler): Observable<unknown> {
const context = `${executionContext.getClass().name}/${executionContext.getHandler().name}`;

const request = executionContext.switchToHttp().getRequest();
const res = executionContext.switchToHttp().getResponse();

request['context'] = context;

Expand All @@ -16,6 +21,8 @@ export class HttpLoggerInterceptor implements NestInterceptor {
request.id = request.headers.traceid;
}

this.logger.logger(request, res);

return next.handle();
}
}

0 comments on commit ac6697a

Please sign in to comment.