Friday 27 September 2013

"Comb" Library: Logging (1 of 2)

Continuing with my overview of the comb library Lets taking a look at the logging functions available.

logging is critical in your applications, not just for errors but in order to get a good understanding of how your application is operating in the wild/production.

So what am I going to cover in the post.. well let see:

  • Logger inheritance through name spaces
  • Predefined level level definition along with the ability to define your own.
Logger inheritance through name spaces.. sample code anyone?

Lets load the comb library
var comb = require('comb'); //load the comb lib

Lets load a set of different elements for logging in different aspects
var logger_sys = comb.logger("sys");
var logger_user = comb.logger("user");
var logger_sys_logger = comb.logger("sys.logger");
var logger_user_logger = comb.logger("user.logger");

Note that the "." dot denotes the separation of "name space" levels

Next here's a simple function just to print out the current level attribute for each of our elements for logging.
function print(){
 console.log("sys:"+logger_sys.level.name);
 console.log("user:"+logger_user.level.name);
 console.log("sysL:"+logger_sys_logger.level.name);
 console.log("userL:"+logger_user_logger.level.name);
 console.log();//lets skip a line for readability
}

let's set a description for each of the logging levels
console.log(">> lets set what the defalts level looks like");
print();

console.log(">> lets set sys and its child to 'DEBUG'");
logger_sys.level = 'DEBUG';
print();


console.log(">> lets set user and its child to 'INFO'");
logger_user.level = 'INFO';
print();

console.log(">> Now we will ONLY set sys.logger to 'WARN'");
logger_sys_logger.level = 'WARN';
print();

examples of inheritance within logging
console.log('>> If will create a sub logger');
console.log('>> It will inherit the level from its parent');
console.log(comb.logger('sys.logger.log').level.name);

So what is the point in this??

Ok, let's take you have "INFO" and "ERROR" levels(for a full list of predefined logging levels see comb.logging.Level) So we can call a logging instance something inspired like "mypack.myclass.note" and set the level to INFO and another with "mypack.myclass.problem" to "ERROR".

Something important to note is that if you used the same "name space" name in the same or a different file, it will return the same global instants regardless.


Continue to part 2: Configurable with files OR programatically