Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaScript OOP: Implementation of Logging

Tags:

javascript

oop

I have written the following code to implement logging in a separate js file logger.js by using OOP.

var console;

function Logger() {
    init();
}

var init = function() {
    if(!window.console){ 
        console = {
            log: function(message){},
            info: function(message){},
            warn: function(message){},
            error: function(message){}
        }; 
    } else {
        console = window.console;
    }
};

Logger.prototype.log = function(message) {
    console.log(message);    
}

Logger.prototype.logInfo = function(message) {
    console.info(message);
}

Logger.prototype.logWarn = function(message) {
    console.warn(message);
}

Logger.prototype.logError = function(message) {
    console.error(message);
}

I am using it from another js file, site.js as:

var logger = new Logger(); //global variable

var getComponentById = function(id) {
    var component = null;

    if(id) {
        try {
            component = AdfPage.PAGE.findComponentByAbsoluteId(id);
        }catch(e){
            logger.logError(e);
        }
    }

    return component;
}

I was wondering

  • If I have implemented the Logger class in proper way, by maintaining OOP of JavaScript.
  • Will it handle the scenario where the browser don't have any console?
  • How can I make init() method inaccessible from other js file or method? I mean how can I make it private?

Any pointer would be very helpful to me.

Update

From another SO thread I found information about private method and I changed my approach:

function Logger() {
    init();
}

Logger.prototype = (function() {
    var console;

    var init = function() {
        if(!window.console){ 
            this.console = {
                log: function(message){},
                info: function(message){},
                warn: function(message){},
                error: function(message){}
            }; 
        } else {
            this.console = window.console;
        }
    };

    return {
        constructor: Logger,

        log: function(message) {
            this.console.log(message);    
        },

        logInfo: function(message) {
            this.console.info(message);
        },

        logWarn: function(message) {
            this.console.warn(message);
        },

        logError: function(message) {
            this.console.error(message);
        }
    };
})();

But in this case I am getting error that init is not defined.

like image 204
Tapas Bose Avatar asked Jul 14 '13 16:07

Tapas Bose


1 Answers

To answer your questions:

  • your implementation of the class is a bit odd. You're accessing the console variable with a closure, having it as a property on the Logger makes more sense.
  • if the browser has no console, you wont get an error (but the logger wont do anything)
  • To make you init function private you could wrap it in an IIFE (immediately invoked function expression)

I took your code and changed it slightly to come up with this:

// Create the Logger function with an IIFE, this keeps all of the private
// variables out of the global scope, the only thing in the global scope
// is the function returned by the IIFE.
var Logger = (function (w) {
    var Logger,
        DummyConsole;

    DummyConsole = function () {
        this.log = function (message) {
            alert(message);
        };
        this.info = function (message) {
            // Implement however you want.
        };
        this.warn = function (message) {
            // ... 
        };
        this.error= function (message) {
            // ...
        };
    };

    Logger = function () {
        if (!w.console) {
            this.console = new DummyConsole();
        } else {
            this.console = w.console;
        }
    };

    Logger.prototype.log = function(message) {
        this.console.log(message);    
    };

    Logger.prototype.logInfo = function(message) {
        this.console.info(message);
    };

    Logger.prototype.logWarn = function(message) {
        this.console.warn(message);
    };

    Logger.prototype.logError = function(message) {
        this.console.error(message);
    };

    return Logger;
}(window));

// create a logger instance to check that the Logger class logs to the console.
var a = new Logger();
a.log("hello");

// Remove the console.
window.console = null;

// Create a new logger checking that it falls back to the dummy console implementation.
var b = new Logger();

// An (annoying) alert is shown.
b.log("Hi");

Code is available as a JSFiddle here: http://jsfiddle.net/mtufW/

like image 189
RobH Avatar answered Sep 28 '22 07:09

RobH