Global Logging

by alr3

JavaScript

function Logger() {
    this.log = function() {
        console.log.apply(this, arguments);
    };
    
    this.debug = function() {
        console.debug.apply(this, arguments);
    };
    
    this.info = function() {
        console.info.apply(this, arguments);
    };
    
    this.warn = function() {
        console.warn.apply(this, arguments);
    };
    
    this.error = function() {
        try {
            console.error.apply(this, arguments);
        } catch(err) {
            this.warn("Error while trying to display an error: " + err);
            this.log.apply(this, arguments);
        }
    };
}

var G = new Logger();

$(document).ready(function() {
    
    G.debug("DEBUG: String.format = %d", "try");
   
    G.warn("WARN: String.format = %d", "try");
   
    G.log("LOG: String.format = %d", "try");
   
    G.info("INFO: String.format = %d", "try");
   
    G.error("ERROR: String.format = %d", "try"); 
    
    G.info("INFO: String.format = %d : %d : %d", "try", "yo", "mama");
   
});