Skip to content

Commit

Permalink
feat: improve logs
Browse files Browse the repository at this point in the history
  • Loading branch information
isuvorov committed Feb 6, 2024
1 parent 817ed70 commit 5b600e4
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 16 deletions.
4 changes: 2 additions & 2 deletions libs/rabbitmq/src/RmqInterceptor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ import { inc } from './utils';

@Injectable()
export class RmqInterceptor implements NestInterceptor {
log = createLogger(this.constructor.name, { ns: 'rmq', level: 'warn' });
log = createLogger(this.constructor.name, { ns: 'rmq:consume', level: 'warn' });
async intercept(context: ExecutionContext, next: CallHandler): Promise<Observable<any>> {
const type = context.getType() as ContextType | 'rmq';
const isLog = getRmqConfig('isLog') || false;
const isLog = getRmqConfig('isLog') ?? isDev;
const maxAttempts = getRmqConfig('maxAttempts') || 0;
const errDelay = getRmqConfig('errDelay') || 0;

Expand Down
2 changes: 1 addition & 1 deletion libs/rabbitmq/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export const rmqDeliveryAttempts: Record<string, any> = {};
export const defaultRmqConfig = {
errDelay: isDev ? 1000 : 100,
maxAttempts: isDev ? 3 : 20,
isLog: isDev,
isLog: false,
};

const log = createLogger('rmq:config', { level: 'debug' });
Expand Down
31 changes: 18 additions & 13 deletions libs/utils/src/AnyExceptionFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ const isEmpty = (obj = {}) => !Object.keys(obj).length;
@Catch(Err)
export class AnyExceptionFilter implements BaseExceptionFilter {
// AnyExceptionFilter.name
private readonly log = createLogger('webserver:exception');
log = createLogger('webserver:exception');
rmqlog = createLogger('rmq:exception');
catch(err: Err, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const res = ctx.getResponse<Response>();
Expand All @@ -32,14 +33,14 @@ export class AnyExceptionFilter implements BaseExceptionFilter {
status = 500;
}

const logInfo: Record<string, unknown> = {
const logInfo: Record<string, unknown> = omitNull({
url: req.url,
status,
query: req.query,
body: req.body,
headers: req.headers,
};
this.logError(err, logInfo);
});
this.logError(err, logInfo, host);

// @ts-ignore
const debug = isDebug && !isEmpty(err?.debug) ? err?.debug : null;
Expand Down Expand Up @@ -72,22 +73,26 @@ export class AnyExceptionFilter implements BaseExceptionFilter {
return res.status(status).send(stringify(response, undefined, isDebug ? 2 : 0));
}

logError(err: Err, logInfo: any = {}) {
logError(err: Err, logInfo: any, host: ArgumentsHost) {
const contextType = host.getType() as string;
const log = contextType === 'rmq' ? this.rmqlog : this.log;
if (isEmpty(logInfo.query)) delete logInfo.query;
if (isEmpty(logInfo.body)) delete logInfo.body;
if (!isDeepDebug || isEmpty(logInfo.headers)) delete logInfo.headers;
if (logInfo.status >= 500) {
this.log.error(err);
this.log.trace('[req]', logInfo);
log.error(err);
log.trace('[req]', logInfo);
} else if ([401, 403, 404].includes(logInfo.status)) {
this.log.trace(err);
// this.log.trace('[req]', logInfo);
log.trace(err);
// log.trace('[req]', logInfo);
} else if (logInfo.status >= 400) {
this.log.debug(err);
this.log.trace('[req]', logInfo);
log.debug(err);
log.trace('[req]', logInfo);
} else if (logInfo.status === 200) {
log.trace('[req]', { ...logInfo, errCode: Err.getCode(err) });
} else {
this.log.debug(err);
this.log.trace('[req]', logInfo);
log.debug(err);
log.trace('[req]', logInfo);
}
}
}

0 comments on commit 5b600e4

Please sign in to comment.