Skip to content

Commit 24bb6a3

Browse files
prepare 3.2.6 release (#21)
1 parent ee034b4 commit 24bb6a3

File tree

2 files changed

+34
-8
lines changed

2 files changed

+34
-8
lines changed

src/__tests__/ConsoleLogger-test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,25 @@ describe('createConsoleLogger', () => {
9393
expect(infoSpy).not.toHaveBeenCalled();
9494
expect(warnSpy).not.toHaveBeenCalled();
9595
});
96+
97+
it('does not throw an error if console is undefined or null', () => {
98+
// see comments in ConsoleLogger.js
99+
const oldConsole = console;
100+
try {
101+
console = null; // eslint-disable-line no-global-assign
102+
logger.debug('x');
103+
logger.info('x');
104+
logger.warn('x');
105+
logger.error('x');
106+
console = undefined; // eslint-disable-line no-global-assign
107+
logger.debug('x');
108+
logger.info('x');
109+
logger.warn('x');
110+
logger.error('x');
111+
} finally {
112+
console = oldConsole; // eslint-disable-line no-global-assign
113+
}
114+
});
96115
});
97116
});
98117
});

src/consoleLogger.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
// If no minimum level is specified, all messages will be logged. Setting the level to "none"
33
// disables all logging.
44

5+
// Note that the global console variable is not guaranteed to be defined at all times in all
6+
// browsers, so this implementation checks for its existence at the time a message is logged.
7+
// See: https://www.beyondjava.net/console-log-surprises-with-internet-explorer-11-and-edge
8+
59
export default function createConsoleLogger(level, maybePrefix) {
610
const allLevels = ['debug', 'info', 'warn', 'error'];
711
let prefix;
@@ -21,17 +25,20 @@ export default function createConsoleLogger(level, maybePrefix) {
2125

2226
const logger = {};
2327

24-
function log(levelIndex, outputFn, msg) {
25-
if (levelIndex >= minLevelIndex) {
26-
const levelName = levelIndex < allLevels.length ? allLevels[levelIndex] : '?';
27-
outputFn(prefix + '[' + levelName + '] ' + msg);
28+
function log(levelIndex, methodName, msg) {
29+
if (levelIndex >= minLevelIndex && console) {
30+
const method = console[methodName];
31+
if (method) {
32+
const levelName = levelIndex < allLevels.length ? allLevels[levelIndex] : '?';
33+
method.call(console, prefix + '[' + levelName + '] ' + msg);
34+
}
2835
}
2936
}
3037

31-
logger.debug = msg => log(0, console.log, msg); // eslint-disable-line no-console
32-
logger.info = msg => log(1, console.info, msg); // eslint-disable-line no-console
33-
logger.warn = msg => log(2, console.warn, msg); // eslint-disable-line no-console
34-
logger.error = msg => log(3, console.error, msg); // eslint-disable-line no-console
38+
logger.debug = msg => log(0, 'log', msg); // eslint-disable-line no-console
39+
logger.info = msg => log(1, 'info', msg); // eslint-disable-line no-console
40+
logger.warn = msg => log(2, 'warn', msg); // eslint-disable-line no-console
41+
logger.error = msg => log(3, 'error', msg); // eslint-disable-line no-console
3542

3643
return logger;
3744
}

0 commit comments

Comments
 (0)