Skip to content

Instantly share code, notes, and snippets.

@sdeleuze
Last active September 29, 2015 11:32
Show Gist options
  • Save sdeleuze/739a693c1f6acf90b0b1 to your computer and use it in GitHub Desktop.
Save sdeleuze/739a693c1f6acf90b0b1 to your computer and use it in GitHub Desktop.
// IE
if(typeof(window.console) === 'undefined') {
window.console = {};
window.console.log = window.console.error = window.console.info = window.console.debug = window.console.warn = window.console.trace = window.console.dir = window.console.dirxml = window.console.group = window.console.groupEnd = window.console.time = window.console.timeEnd = window.console.assert = window.console.profile = function() {};
}
var console = window.console;
// IE8 & 9
var methods = ['log', 'debug', 'info','warn','error','assert','dir','clear','profile','profileEnd'];
if (typeof console.log === 'object' || typeof console.log === 'function') {
console.debug = console.log;
_.each(methods, function (method) {
console['_' + method] = console[method];
});
}
// Can be customized if needed
console.serverUrl = 'api/log';
console.levels = ['debug', 'info', 'warn', 'error'];
// Disabled by default
console.level = 'off';
var enabledFor = function (level) {
if(console.level === 'off') return false;
if (_.indexOf(console.levels, level) === -1) {
throw new Error('Invalid log level "' + strategy + '", must be one of ' + JSON.stringify(console.levels));
}
if (_.indexOf(console.levels, console.level) === -1) {
throw new Error('Invalid log console.level "' + strategy + '", must be one of ' + JSON.stringify(console.levels));
}
return _.indexOf(console.levels, level) >= _.indexOf(console.levels, console.level);
};
var log = console._log;
var debug = console._debug;
var info = console._info;
var warn = console._warn;
var error = console._error;
// console.log = console.debug
console.log = function () {
Function.prototype.apply.call(log, this, arguments);
if(enabledFor('debug')) {
sendLogToServer('debug', arguments);
}
};
console.debug = function () {
Function.prototype.apply.call(debug, this, arguments);
if(enabledFor('debug')) {
sendLogToServer('debug', arguments);
}
};
console.info = function () {
Function.prototype.apply.call(info, this, arguments);
if(enabledFor('info')) {
sendLogToServer('info', arguments);
}
};
console.warn = function () {
Function.prototype.apply.call(warn, this, arguments);
if(enabledFor('warn')) {
sendLogToServer('warn', arguments);
}
};
console.error = function () {
Function.prototype.apply.call(error, this, arguments);
if(enabledFor('error')) {
sendLogToServer('error', arguments);
}
};
var sendLogToServer = function(level, msg) {
var message;
if((typeof msg === 'object') && (msg.length === 1) && (typeof msg[0] === 'string')) {
message = msg[0];
}
else {
message = JSON.stringify(msg);
}
var log = { 'level':level, 'message': message};
$.ajax({
type: 'POST',
url: console.serverUrl,
data: JSON.stringify(log),
contentType:'application/json',
success: function(){
// update log level
}
});
};
// manage global JS errors
window.onerror = function(message, url, linenumber) {
if(enabledFor('error')) {
sendLogToServer('error', 'JavaScript error: ' + message + ' on line ' + linenumber + ' for ' + url);
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment