-
Notifications
You must be signed in to change notification settings - Fork 0
Features and Usage
- Multiple logging profiles can be maintained at the same time with different properties.
var sl = new SmartLogger(); var sl2 = new SmartLogger({selectiveEnable: 'block1'});
- Proprieties can be set at the time of instantiation or even later.
var sl = new SmartLogger(); sl.loggerOutput = 'console'; var sl2 = new SmartLogger({loggerOutput: 'console'});
- name, version number and whoami to identify the logger with a string of its current properties.
var sl = new SmartLogger(); sl.name // SmartLogger. sl.version // 0.7 sl.whoamI() // Returns a string of its properties with the name of the object in a specific sequence: // "SmartLogger_"+ enableLogger +"_"+ enableAssert+"_"+ loggerOutput+"_"+ selectiveEnable+"_"+ selectiveDisable; // Example: SmartLogger_true_true_console__b // We will see what these properties are in some time..
- Enable or disable logging altogether: enableLogger controls if the statements should ever be logged.
var sl = new SmartLogger(); sl.log('gets logged'); sl.enableLogger = false; sl.log('never gets logged');
- Intelligently decides where the logging statements should go...
sl.loggerOutput = undefined; //default
Decides based on presence of 'console' object. If console is present statements will be logged to console, else like in case of IE, will be 'alerted' to the user. Now at times this can get messy, with loads of log statements alerting on our face.. But wait, we have ways to handle that.
sl.loggerOutput = 'console'; // Plain instruction, no intelligence, all statements will always go to console. // If console is not present statements will just be eaten-up. sl.loggerOutput = 'alert'; // Another plain instruction, all statements will always be alerted. // Will not bother to check if console exists or not.
- Log formatted objects to console. Now you wont need that much with firebug but to see the entire contents of the object, well formatted you can just say stringToConsole.
// Just a sample object with unknown properties. var obj = {prop1: 'value',functProp:function(){return "this is a function that returns me!";}, propObj:{prop2:'value2'}}; sl.stringToConsole(obj); // You say this. // On console or in the alert prompt, you get this Object : { prop1 : value, functProp : function () { return "this is a function that returns me!"; }, propObj : { prop2 : value2, }, }
- Assert your assumptions. Checks that the assumption is true, if yes, logs so. If assumption fails, will write out an error on the console and invoke the debugger so the user can check in the stack exactly where the assumption failed and why.
sl.assert(Obj.str===Obj2.str) sl.assert(1==1); // logs 'Assumption: true' to console. sl.assert(1==2); // Logs error 'Assumption: failed!' and invoke debugger to the assert line in SmartLogger. //Now you can go and check in the stack and watch to panels to check value and call stack.
-
Has a wrapper for 4 of the logging APIs from firebug and adding new is not much of a task. What it already has: log debug info error
-
Has ability of selective logging. Now this thing is a live saver. The properties selectiveEnable and selectiveDisable control what statements to log. While these are not mandatory inputs to all the wrappers but I suggest you set them always. These are logging context that can be used to selectively enable logs for only partial of the code, the code that currently interests you..
// Suppose we were working on a defect number 101 and now we are developing a functionality for // automating welcome messages to users and are asked to urgently fix defect 203. // Ah, complex scenario, but it will only help understand the purpose. // When we are working on defect 101, we had the logger configured as statements as: sl.log("reached here"); // worst way to log: who knows wheres here! but just an example. // Now we are working on the functionality and // we would not want those 10 logging statements added while we were working on the defect. // We can remove them or simply enable the 'selective logger'! sl.selectiveEnable = 'welcomer'; sl.log("fetching message", "welcomer"); // And voila, only the 'welcomer' messages will be logged. // Now we get the next urgent defect. sl.selectiveEnable = 'defect203'; sl.log("value in Obj1.str"+Obje1.str, "defect203"); // We get only the defect203 logs! // Now some of our new changes depend on the changes we made in defect101, but we cant get the logs from those.. // What do we do? If someone did not enable selective logger and removed the statements, please add them back (:p), // remove statements for 'welcomer' functionality. Or simply, disable 'welcomer' messages..! sl.selectiveEnable = ''; sl.selectiveDisable = 'welcomer'; sl.log("value in Obj1.str"+Obje1.str, "defect203"); sl.log("value in Obj2.varInt1"+Obj2.varInt1, "defect101"); // Ha ha! Log statements for 'welcomer' gone and we get the rest!
While using the SmartLogger, I suggest you always pass the string identifier, so that you can control the logs at any point of time later.