diff --git a/src/core/logger.ts b/src/core/logger.ts index b046af83..caa017c8 100644 --- a/src/core/logger.ts +++ b/src/core/logger.ts @@ -1,3 +1,5 @@ +import { NO_OP } from './no-op'; + /** * The type representing the possible log levels to choose from. * @@ -51,28 +53,35 @@ export type ILogger = Record, 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(); @@ -80,6 +89,11 @@ const lazyPrint = 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. * @@ -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; } /** diff --git a/test/core/logger.spec.ts b/test/core/logger.spec.ts index 82d7309b..eaa29da0 100644 --- a/test/core/logger.spec.ts +++ b/test/core/logger.spec.ts @@ -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', () => { @@ -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,