PDA

View Full Version : Simple Logger



Bendulu
26 Oct 2011, 1:56 PM
Hey,

after using some of the cool stuff i found here i thought it might be time to give something back. my search for a simple logging solution for ext came up empty so i decided to roll my own logger. it is very, very simple and lacks support for browsers without a console implementation (i will work on that later if time permits) but it has proven to be very useful (at least for me). i hope someone will find it as useful.

CODE :


/**
* A simple logger
*/
Ext.define('Refactored.extjs.utils.Logger',
{
// ---- Class Configuration ---------------------------------------------------------------------------------

statics :
{
/**
* Flags if the logger is enabled
* @property isEnabled
* @type {Boolean}
*/
isEnabled : true,

/**
* The default for isLoggingEnabled
* @property isLoggingEnabledDefault
* @type {Boolean}
*/
isLoggingEnabledDefault : true,

/**
* Installs the logger to all classes
*/
install : function()
{
Ext.Base.borrow(Refactored.extjs.utils.Logger, ['log']);
}
},

// ---- Configuration ---------------------------------------------------------------------------------------
// ---- Properties ------------------------------------------------------------------------------------------
// ---- Event Handler ---------------------------------------------------------------------------------------
// ---- Methods ---------------------------------------------------------------------------------------------

/**
* Logs all given arguments to the console if this.isLoggingEnabled == true and
* Refactored.extjs.debug.Logger.isEnabled == true
*/
log : function()
{
if (!Refactored.extjs.utils.Logger.isEnabled)
{
return;
}
if (typeof this.isLoggingEnabled == 'undefined')
{
this.isLoggingEnabled = Refactored.extjs.utils.Logger.isLoggingEnabledDefault;
}
if (!this.isLoggingEnabled)
{
return;
}


var info = '';

//Add Timestamp
var date = new Date();
info += Ext.String.leftPad(date.getHours().toString(), 2, '0') + ':';
info += Ext.String.leftPad(date.getMinutes().toString(), 2, '0') + ':';
info += Ext.String.leftPad(date.getSeconds().toString(), 2, '0') + ':';
info += Ext.String.leftPad(date.getMilliseconds().toString(), 3, '0');

//Add class
if (typeof this.$className != 'undefined')
{
info += ' - ' + this.$className;

//Add id
if (typeof this.id != 'undefined')
{
info += ' (' + this.id + ')';
}
}

//Add seperator
info += ' || ';

//Add info to arguments
var args = Array.prototype.slice.call(arguments);
args.unshift(info);

//Log...
console.log.apply(console, args);
}
});


//Install the logger
Refactored.extjs.utils.Logger.install();


DESCRIPTION
The logger installs itself to all classes based on Ext.define and is configurable for each instance via isLoggerEnabled. To enable or disable it globally use Refactored.extjs.utils.Logger.isEnabled. The output will look something like this :


23:45:33:900 - Sample.controller.Main (1) || constructor() : config = Object { application={...}, viewContainer={...}}

The timestamp, the classname and the id (the number in parenthesis) are provided by the logger the rest ist the actual log message.

USAGE
Just require the class and use it like this in all your classes:


this.log('constructor() : config = ', this.config);



cheers
Chris