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.

Revisions

  1. sdeleuze revised this gist Sep 29, 2015. 1 changed file with 92 additions and 95 deletions.
    187 changes: 92 additions & 95 deletions server-logger.js
    Original file line number Diff line number Diff line change
    @@ -1,105 +1,102 @@
    define(['jquery', 'underscore'], function ($, _) {

    // 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() {};
    }
    // 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;
    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;
    // 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];
    });
    }
    _.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);
    };
    // Can be customized if needed
    console.serverUrl = 'api/log';
    console.levels = ['debug', 'info', 'warn', 'error'];
    // Disabled by default
    console.level = 'off';

    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 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;

    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);
    }
    // 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 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);
    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);
    }
    };
  2. sdeleuze revised this gist Sep 29, 2015. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion server-logger.js
    Original file line number Diff line number Diff line change
    @@ -83,7 +83,7 @@ define(['jquery', 'underscore'], function ($, _) {
    message = JSON.stringify(msg);
    }

    var log = { 'level':level, 'message': message, 'context' : App.Logs.currentMarker};
    var log = { 'level':level, 'message': message};

    $.ajax({
    type: 'POST',
  3. sdeleuze created this gist Sep 29, 2015.
    105 changes: 105 additions & 0 deletions server-logger.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,105 @@
    define(['jquery', 'underscore'], function ($, _) {

    // 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, 'context' : App.Logs.currentMarker};

    $.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);
    }
    };
    });