Skip to content

Commit

Permalink
prepare 3.2.6 release (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
LaunchDarklyCI authored Mar 31, 2020
1 parent ee034b4 commit 24bb6a3
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 8 deletions.
19 changes: 19 additions & 0 deletions src/__tests__/ConsoleLogger-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,25 @@ describe('createConsoleLogger', () => {
expect(infoSpy).not.toHaveBeenCalled();
expect(warnSpy).not.toHaveBeenCalled();
});

it('does not throw an error if console is undefined or null', () => {
// see comments in ConsoleLogger.js
const oldConsole = console;
try {
console = null; // eslint-disable-line no-global-assign
logger.debug('x');
logger.info('x');
logger.warn('x');
logger.error('x');
console = undefined; // eslint-disable-line no-global-assign
logger.debug('x');
logger.info('x');
logger.warn('x');
logger.error('x');
} finally {
console = oldConsole; // eslint-disable-line no-global-assign
}
});
});
});
});
23 changes: 15 additions & 8 deletions src/consoleLogger.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@
// If no minimum level is specified, all messages will be logged. Setting the level to "none"
// disables all logging.

// Note that the global console variable is not guaranteed to be defined at all times in all
// browsers, so this implementation checks for its existence at the time a message is logged.
// See: https://www.beyondjava.net/console-log-surprises-with-internet-explorer-11-and-edge

export default function createConsoleLogger(level, maybePrefix) {
const allLevels = ['debug', 'info', 'warn', 'error'];
let prefix;
Expand All @@ -21,17 +25,20 @@ export default function createConsoleLogger(level, maybePrefix) {

const logger = {};

function log(levelIndex, outputFn, msg) {
if (levelIndex >= minLevelIndex) {
const levelName = levelIndex < allLevels.length ? allLevels[levelIndex] : '?';
outputFn(prefix + '[' + levelName + '] ' + msg);
function log(levelIndex, methodName, msg) {
if (levelIndex >= minLevelIndex && console) {
const method = console[methodName];
if (method) {
const levelName = levelIndex < allLevels.length ? allLevels[levelIndex] : '?';
method.call(console, prefix + '[' + levelName + '] ' + msg);
}
}
}

logger.debug = msg => log(0, console.log, msg); // eslint-disable-line no-console
logger.info = msg => log(1, console.info, msg); // eslint-disable-line no-console
logger.warn = msg => log(2, console.warn, msg); // eslint-disable-line no-console
logger.error = msg => log(3, console.error, msg); // eslint-disable-line no-console
logger.debug = msg => log(0, 'log', msg); // eslint-disable-line no-console
logger.info = msg => log(1, 'info', msg); // eslint-disable-line no-console
logger.warn = msg => log(2, 'warn', msg); // eslint-disable-line no-console
logger.error = msg => log(3, 'error', msg); // eslint-disable-line no-console

return logger;
}

0 comments on commit 24bb6a3

Please sign in to comment.