log4j style logging in Javascript

Are you from a Java background? Did you love working with log4j? Do you miss log4j style logging in Javascript? Worry no more, because with a few lines of code, you can achieve log4j style logging in your JS code.

Here’s how you can use log4j style logging in your code:

var MyClass1, MyClass2;
MyClass1 = (function() {
    var logger = Logger.getLogger('com.umar.demo.MyClass1');
    function MyClass1() {
        logger.debug('Instantiating MyClass1');
    }
    return MyClass1;
})();

MyClass2 = (function() {
    var logger = Logger.getLogger('com.umar.demo.MyClass2');
    function MyClass2() {
        logger.debug('Instantiating MyClass2');
    }
    return MyClass2;

And here’s how you can specify your configurations:

Logger.setup({
root:{level:'OFF'},
logger:[
{name:'com.umar.demo.MyClass1',level:'TRACE'},
{name:'com.umar.demo.MyClass2',level:'TRACE'}
]
});

When your code will run:

var o1 = new MyClass1();
var o2 = new MyClass2();

You will see something like this in your console:

Mon Feb 27 2012 12:47:28 GMT+0500 (Pakistan Standard Time) TRACE com.umar.demo.MyClass1 - Instantiating MyClass1
Mon Feb 27 2012 12:47:28 GMT+0500 (Pakistan Standard Time) TRACE com.umar.demo.MyClass2 - Instantiating MyClass2

You can always specify different logging levels (or turn them off) for different packages classes just like you did in log4j. You just need to include following code in your project:

var Logger;
Logger = (function(){
function Logger(name) {
this.name = name;
}

Logger.prototype.__determineLevel = function() {
var name = Logger.root.level;
for (var i=0, max=Logger.logger.length; i= Logger.level.number && (Logger.name==='' || this.name.indexOf(Logger.name))
var result = false;
var configuredLevel = this.__determineLevel();
if (configuredLevel.number > -1 && level.number >= configuredLevel.number) {
result = true;
}

//             alert('level.name: '+level.name+'; configuredLevel.name: '+configuredLevel.name+';name: '+this.name+'; isPrintable: '+result);
return result;
};

Logger.prototype.__determineLevelNumber = function(levelName) {
var number = -1;
if (levelName === 'TRACE') {
number = 0;
} else if (levelName === 'DEBUG') {
number = 1;
} else if (levelName === 'INFO') {
number = 2;
} else if (levelName === 'ERROR') {
number = 3;
} else if (levelName === 'FATAL') {
number = 4;
}

return number;
};

Logger.prototype.__now = function() {
return new Date();
};

Logger.prototype.__print = function(levelName, levelNumber, message) {
if (this.__isPrintable({name:levelName, number:levelNumber}, message)) {
console.log(this.__now()+' '+levelName+' '+this.name+' - '+message);
}
};

Logger.prototype.trace = function(message) {
this.__print('TRACE', 0, message);
};

Logger.prototype.debug = function(message) {
this.__print('DEBUG', 1, message);
};

Logger.prototype.info = function(message) {
this.__print('INFO', 2, message);
};

Logger.prototype.error = function(message) {
this.__print('ERROR', 3, message);
};

Logger.prototype.fatal = function(message) {
this.__print('FATAL', 4, message);
};

return Logger;
})();

Logger.getLogger = function(name) { // public static method
return new Logger(name);
};

Logger.setup = function(options) {
options = options ? options : {};
if (options.logger) {
// {name:'pwp.classes.Panel1',level:'TRACE'}
options.logger.sort(function(a, b){
if (a.name.length > b.name.length) {
return 1;
} else if (a.name.length < b.name.length) {
return -1;
} else {
if (a.name > b.name) {
return 1;
} else if (a.name < b.name) {
return -1;
} else {
return 0;
}
return 0;
}
});
Logger.logger = options.logger;
}

Logger.root = options.root ? options.root : {level:'OFF'};
Logger.root.level = Logger.root.level ? Logger.root.level : 'OFF';
};

Comments and suggestions are welcome.

Published by

Umar Ashfaq

Umar Ashfaq is a full-stack web developer. His core strength is building neat UIs with JavaScript on web but he also enjoys server side Java, NodeJS and Objective C. Follow him on twitter @umarashfaq87

3 thoughts on “log4j style logging in Javascript”

  1. Hi! Thank you for your log4j style logging implementation!
    BTW, should the line with number 100 be in the code?

Leave a Reply

Your email address will not be published. Required fields are marked *