Skip to content

Commit

Permalink
perf(logger): faster logger
Browse files Browse the repository at this point in the history
  • Loading branch information
H4ad committed Jun 7, 2023
1 parent 36950b3 commit 103817c
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 47 deletions.
95 changes: 49 additions & 46 deletions src/core/logger.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { NO_OP } from './no-op';

/**
* The type representing the possible log levels to choose from.
*
Expand Down Expand Up @@ -51,35 +53,47 @@ export type ILogger = Record<Exclude<LogLevels, 'none'>, LoggerFN>;
*/
const InternalLoggerSymbol = Symbol('InternalLogger');

const errorLogLevel = new Map([
['debug', true],
['verbose', true],
['info', true],
['warn', true],
['error', true],
]);
const warnLogLevel = new Map([
['debug', true],
['verbose', true],
['info', true],
['warn', true],
]);
const infoLogLevel = new Map([
['debug', true],
['verbose', true],
['info', true],
]);
const verboseLogLevel = new Map([
['debug', true],
['verbose', true],
]);
const logLevels: Record<
LogLevels,
[level: LogLevels, consoleMethod: keyof Console][]
> = {
debug: [
['debug', 'debug'],
['verbose', 'debug'],
['info', 'info'],
['error', 'error'],
['warn', 'warn'],
],
verbose: [
['verbose', 'debug'],
['info', 'info'],
['error', 'error'],
['warn', 'warn'],
],
info: [
['info', 'info'],
['error', 'error'],
['warn', 'warn'],
],
warn: [
['warn', 'warn'],
['error', 'error'],
],
error: [['error', 'error']],
none: [],
};

const lazyPrint = value => {
if (typeof value === 'function') return value();

return value;
};

const print =
(fn: string) =>
(message, ...additional) =>
console[fn](message, ...additional.map(lazyPrint));

/**
* The method used to create a simple logger instance to use in this library.
*
Expand All @@ -101,34 +115,23 @@ const lazyPrint = value => {
export function createDefaultLogger(
{ level }: LoggerOptions = { level: 'error' },
): ILogger {
return {
[InternalLoggerSymbol]: true,
error: (message, ...additional) => {
if (!errorLogLevel.has(level)) return;
const levels = logLevels[level];

console.error(message, ...additional.map(lazyPrint));
},
warn: (message, ...additional) => {
if (!warnLogLevel.has(level)) return;
if (!levels) throw new Error('Invalid log level');

console.warn(message, ...additional.map(lazyPrint));
},
info: (message, ...additional) => {
if (!infoLogLevel.has(level)) return;

console.info(message, ...additional.map(lazyPrint));
},
verbose: (message, ...additional) => {
if (!verboseLogLevel.has(level)) return;
const logger = {
[InternalLoggerSymbol]: true,
error: NO_OP,
debug: NO_OP,
info: NO_OP,
verbose: NO_OP,
warn: NO_OP,
} as ILogger;

console.debug(message, ...additional.map(lazyPrint));
},
debug: (message, ...additional) => {
if (level !== 'debug') return;
for (const [level, consoleMethod] of levels)
logger[level] = print(consoleMethod);

console.debug(message, ...additional.map(lazyPrint));
},
} as ILogger;
return logger;
}

/**
Expand Down
8 changes: 7 additions & 1 deletion test/core/logger.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ describe('createDefaultLogger', () => {
expect(global.console.debug).toHaveBeenNthCalledWith(1, 'verbose');
expect(global.console.debug).toHaveBeenNthCalledWith(2, 'debug');
});

it('should throw error with invalid log level', () => {
expect(() =>
createDefaultLogger({ level: 'random' as unknown as LogLevels }),
).toThrowError('Invalid');
});
});

describe('isInternalLogger', () => {
Expand All @@ -172,7 +178,7 @@ describe('isInternalLogger', () => {
});
}

it('random isntance of ILogger should not return true', () => {
it('random instance of ILogger should not return true', () => {
expect(
isInternalLogger({
debug: NO_OP,
Expand Down

0 comments on commit 103817c

Please sign in to comment.