Skip to content

Commit

Permalink
feat(utils): improve createNestLogger
Browse files Browse the repository at this point in the history
  • Loading branch information
isuvorov committed Dec 5, 2023
1 parent dc25412 commit b73d43b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 17 deletions.
4 changes: 2 additions & 2 deletions libs/utils/src/AnyExceptionFilter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { omitNull } from '@lsk4/algos';
import { isDebug } from '@lsk4/env';
import { Err } from '@lsk4/err';
import { getEnvConfig, Logger } from '@lsk4/log';
import { createLogger } from '@lsk4/log';
import {
ArgumentsHost,
Catch,
Expand All @@ -20,7 +20,7 @@ const isEmpty = (obj = {}) => !Object.keys(obj).length;
@Catch(Err)
export class AnyExceptionFilter implements BaseExceptionFilter {
// AnyExceptionFilter.name
private readonly log = new Logger({ ns: 'webserver:exception', ...getEnvConfig() });
private readonly log = createLogger('webserver:exception');
catch(err: Err, host: ArgumentsHost) {
const ctx = host.switchToHttp();
const res = ctx.getResponse<Response>();
Expand Down
42 changes: 27 additions & 15 deletions libs/utils/src/createNestLogger.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
import type { ILogger, LoggerLevelType } from '@lsk4/log';
import { mapValues } from '@lsk4/algos';
// eslint-disable-next-line import/named
import { ILogger, Logger, LoggerLevelType } from '@lsk4/log';
import { createLogger } from '@lsk4/log';
import { LoggerService as NestLoggerService } from '@nestjs/common';

export type LoggerService = NestLoggerService & {
info: (name: string, ...args: any[]) => void;
setLogLevels: () => void;
};
export interface ILoggerOptions {
env?: string;
context?: string;
Expand All @@ -11,28 +16,35 @@ export interface ILoggerOptions {

ns?: string;
}

export const createNestLogger = (props: ILoggerOptions = {}): ILogger => {
export const createNestLogger = (props: ILoggerOptions = {}): LoggerService => {
const { level, ns } = props;
const log = new Logger({ level, ns });
// const log = new Logger({ level, ns });
const mapper = {
info: 'info',
verbose: 'trace',
log: 'trace',
error: 'error',
debug: 'debug',
info: 'info',
warn: 'warn',
verbose: 'trace',
error: 'error',
fatal: 'fatal',
};

const cache: Record<string, ILogger> = {};
const getLogger = (ns: string) => {
if (!cache[ns]) cache[ns] = createLogger(`nest:${ns}`, { level });
return cache[ns];
};

// @ts-ignore
const loggerLike: ILogger = mapValues(mapper, (lskLevel) => (...args: any[]) => {
const loggerService: any = mapValues(mapper, (lskLevel: LoggerLevelType) => (...args: any[]) => {
const name = args.pop();
const baseName = log.name;
log.name = name;
// @ts-ignore
const res = log[lskLevel](...args);
log.name = baseName;
const res = getLogger(name)[lskLevel](...args);
return res;
});

return loggerLike;
loggerService.setLogLevels = () => {
// log.setLevel(levels);
};

return loggerService as LoggerService;
};

0 comments on commit b73d43b

Please sign in to comment.