Skip to content

Commit

Permalink
Support for IE8+ in Logger.useDefaults(), Fixes #4
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnyreeves committed May 20, 2014
1 parent 8981287 commit 40e9a9e
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions src/logger.js
Expand Up @@ -45,7 +45,7 @@
var defineLogLevel = function(value, name) {
return { value: value, name: name };
};

// Predefined logging levels.
Logger.DEBUG = defineLogLevel(1, 'DEBUG');
Logger.INFO = defineLogLevel(2, 'INFO');
Expand All @@ -60,7 +60,7 @@
this.setLevel(defaultContext.filterLevel);
this.log = this.info; // Convenience alias.
};

ContextualLogger.prototype = {
// Changes the current logging level for the logging instance.
setLevel: function(newLevel) {
Expand All @@ -75,45 +75,45 @@
var filterLevel = this.context.filterLevel;
return lvl.value >= filterLevel.value;
},

debug: function () {
this.invoke(Logger.DEBUG, arguments);
},

info: function () {
this.invoke(Logger.INFO, arguments);
},

warn: function () {
this.invoke(Logger.WARN, arguments);
},

error: function () {
this.invoke(Logger.ERROR, arguments);
},

// Invokes the logger callback if it's not being filtered.
invoke: function (level, msgArgs) {
if (logHandler && this.enabledFor(level)) {
logHandler(msgArgs, merge({ level: level }, this.context));
}
}
};
};

// Protected instance which all calls to the to level `Logger` module will be routed through.
var globalLogger = new ContextualLogger({ filterLevel: Logger.OFF });

// Configure the global Logger instance.
(function() {
(function() {
// Shortcut for optimisers.
var L = Logger;

L.enabledFor = bind(globalLogger, globalLogger.enabledFor);
L.debug = bind(globalLogger, globalLogger.debug);
L.info = bind(globalLogger, globalLogger.info);
L.warn = bind(globalLogger, globalLogger.warn);
L.error = bind(globalLogger, globalLogger.error);

// Don't forget the convenience alias!
L.log = L.info;
}());
Expand All @@ -130,7 +130,7 @@
Logger.setLevel = function(level) {
// Set the globalLogger's level.
globalLogger.setLevel(level);

// Apply this level to all registered contextual loggers.
for (var key in contextualLoggersByNameMap) {
if (contextualLoggersByNameMap.hasOwnProperty(key)) {
Expand All @@ -143,10 +143,10 @@
// default context and log handler.
Logger.get = function (name) {
// All logger instances are cached so they can be configured ahead of use.
return contextualLoggersByNameMap[name] ||
return contextualLoggersByNameMap[name] ||
(contextualLoggersByNameMap[name] = new ContextualLogger(merge({ name: name }, globalLogger.context)));
};
};

// Configure and example a Default implementation which writes to the `window.console` (if present).
Logger.useDefaults = function(defaultLevel) {
// Check for the presence of a logger.
Expand All @@ -163,7 +163,7 @@
if (context.name) {
messages[0] = "[" + context.name + "] " + messages[0];
}

// Delegate through to custom warn/error loggers if present on the console.
if (context.level === Logger.WARN && console.warn) {
hdlr = console.warn;
Expand All @@ -173,7 +173,8 @@
hdlr = console.info;
}

hdlr.apply(console, messages);
// Support for IE8+ (and other, slightly more sane environments)
Function.prototype.apply.call(hdlr, console, messages);
});
};

Expand Down

0 comments on commit 40e9a9e

Please sign in to comment.