Skip to content

Commit

Permalink
Merge pull request #1 from sitespeedio/set-log-level
Browse files Browse the repository at this point in the history
Make it possible to set log level
  • Loading branch information
soulgalore authored Jan 6, 2025
2 parents 2b05592 + c544f73 commit b8adc38
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 22 deletions.
49 changes: 27 additions & 22 deletions src/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
42 changes: 42 additions & 0 deletions test/logger.loglevel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
);
});

0 comments on commit b8adc38

Please sign in to comment.