From c544f735813421c7cbc1e102b9c8de5acea77a91 Mon Sep 17 00:00:00 2001 From: Peter Hedenskog Date: Mon, 6 Jan 2025 17:55:14 +0100 Subject: [PATCH] Make it possible to set log level --- src/logger.js | 49 ++++++++++++++++++++---------------- test/logger.loglevel.test.js | 42 +++++++++++++++++++++++++++++++ 2 files changed, 69 insertions(+), 22 deletions(-) diff --git a/src/logger.js b/src/logger.js index 02fe7cf..0ec73b2 100644 --- a/src/logger.js +++ b/src/logger.js @@ -4,42 +4,47 @@ import { LOG_LEVELS } from './logLevels.js'; /** * Configures the global logger settings. * @param {Object} options - Configuration options. + * @param {string} [options.level] - Directly set a log level by name (e.g., 'error', 'panic', 'info'). * @param {number} [options.verbose=0] - Verbosity level (0: INFO, 1: DEBUG, 2: VERBOSE, 3: TRACE). * @param {boolean} [options.silent=false] - If true, disables all logging. */ export function configureLog(options = {}) { - const { verbose = 0, silent = false } = options; + const { + level: customLevel, + verbose = 0, + silent = false + } = options; + + let finalLevel = 'info'; - // Map the 'verbose' setting to a final level name - let level = 'info'; // default is 'info' if (silent) { - level = 'none'; - } else { + finalLevel = 'none'; + } + + else if (customLevel && LOG_LEVELS[customLevel] !== undefined) { + finalLevel = customLevel; + } + else { switch (verbose) { - case 1: { - level = 'debug'; + case 1: + finalLevel = 'debug'; break; - } - - case 2: { - level = 'verbose'; + case 2: + finalLevel = 'verbose'; break; - } - - case 3: { - level = 'trace'; + case 3: + finalLevel = 'trace'; + break; + default: + finalLevel = 'info'; break; - } - - default: { - level = 'info'; - } } } - loggerConfig.level = level; + loggerConfig.level = finalLevel; - const numericLevel = LOG_LEVELS[level]; + // Adjust the format depending on how detailed logs are + const numericLevel = LOG_LEVELS[finalLevel]; loggerConfig.format = numericLevel <= LOG_LEVELS.info ? '[%(date)s] %(levelname)s: %(message)s' diff --git a/test/logger.loglevel.test.js b/test/logger.loglevel.test.js index d485ed0..fbc343b 100644 --- a/test/logger.loglevel.test.js +++ b/test/logger.loglevel.test.js @@ -89,3 +89,45 @@ test('Verbose=3 allows trace logs', t => { 'Should see trace logs at verbose=3' ); }); + +test('Setting log level to "error" only logs error (and above)', t => { + configureLog({ level: 'error' }); + + const logger = getLogger('test-error-level'); + + const warnOutput = captureConsole('warn', () => { + logger.warn('Should not log at error level'); + }); + + t.false( + warnOutput.includes('Should not log at error level'), + 'Warn messages should not appear at error level' + ); + + const infoOutput = captureConsole('info', () => { + logger.info('Should not log info at error level'); + }); + + t.false( + infoOutput.includes('Should not log info at error level'), + 'Info messages should not appear at error level' + ); + + const debugOutput = captureConsole('debug', () => { + logger.debug('Should not log debug at error level'); + }); + + t.false( + debugOutput.includes('Should not log debug at error level'), + 'Debug messages should not appear at error level' + ); + + const errorOutput = captureConsole('error', () => { + logger.error('We are now at error level'); + }); + + t.true( + errorOutput.includes('We are now at error level'), + 'Error messages should be logged at error level' + ); +});