Skip to content

Commit

Permalink
logger: change how verbosity is being set internally
Browse files Browse the repository at this point in the history
  • Loading branch information
atoppi committed Jan 22, 2024
1 parent 1b69ed6 commit 1f61f19
Showing 1 changed file with 16 additions and 7 deletions.
23 changes: 16 additions & 7 deletions src/utils/logger.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,14 @@

import { getCliArgument } from './utils.js';

const LEVELS = ['none', 'error', 'warn', 'info', 'verb', 'debug'];
const LEVELS = ['none', 'error', 'warning', 'info', 'verbose', 'debug'];
const LEVELS_IDX = LEVELS.reduce((obj, lvl, idx) => {
obj[lvl] = idx;
return obj;
}, {});

const DEFAULT_LEVEL = 'info';
let log_verbosity = getCliArgument('janode-log', 'string', DEFAULT_LEVEL);
if (LEVELS.indexOf(log_verbosity) < 0) log_verbosity = DEFAULT_LEVEL;

const printout = (msg_verbosity, console_fn, ...args) => {
if (LEVELS_IDX[msg_verbosity] > LEVELS_IDX[log_verbosity]) return;
Expand Down Expand Up @@ -49,7 +48,7 @@ const Logger = {
* @function
* @param {...any} args
*/
verbose: (...args) => printout('verb', console.debug, ...args),
verbose: (...args) => printout('verbose', console.debug, ...args),

/**
* Info logging (default).
Expand All @@ -67,7 +66,7 @@ const Logger = {
* @function
* @param {...any} args
*/
warn: (...args) => printout('warn', console.warn, ...args),
warning: (...args) => printout('warning', console.warn, ...args),

/**
* Error logging.
Expand All @@ -87,11 +86,21 @@ const Logger = {
*/
setLevel: (lvl = '') => {
lvl = lvl.toLowerCase();
if (lvl === 'verbose') lvl = 'verb';
if (lvl === 'warning') lvl = 'warn';
if (typeof LEVELS_IDX[lvl] === 'number') log_verbosity = lvl;
if (lvl === 'verb') lvl = 'verbose';
if (lvl === 'warn') lvl = 'warning';
if (typeof LEVELS_IDX[lvl] === 'number') {
log_verbosity = lvl;
}
else {
log_verbosity = DEFAULT_LEVEL;
}
return log_verbosity;
}
};
/* set aliases */
Logger.verb = Logger.verbose;
Logger.warn = Logger.warning;

Logger.setLevel(log_verbosity);

export default Logger;

0 comments on commit 1f61f19

Please sign in to comment.