Easily make your own SugarLogger with Sugar 6

bsoremsugar —  May 18, 2010 — 3 Comments

Way back when we released Sugar 5.1, we changed the logger engine used in the application from log4php to our own homegrown SugarLogger. Part of the design of this logger engine was to make it extensible, so that multiple logging backends could be used. However, doing this wasn’t necessarily upgrade-safe, so most people avoid mucking with it.

Now in Sugar 6, as a part of the many changes added to make it easier to create upgrade-safe customizations, you can now add a custom SugarLogger in an upgrade safe way. All that’s needed is to implement a new class based upon the LoggerTemplate, and add in your logging code, and save that class in the custom directory. It thats simple!

One such example logger you may create is one to output debugging code to FirePHP. For those not familiar with it, FirePHP is a plugin to the popular Firebug Firefox extension that allows you to write debugging code from your PHP application directly to the Firebug console. Creating a logger backend for this in Sugar is now super simple; check out the below example:

// change the path below to the path to your FirePHP install
require_once('fb.php');

class FirePHPLogger implements LoggerTemplate
{
    /**
     * Constructor
     */
    public function __construct()
    {
        // set the default logger to FirePHP if the config says to.
        if ( isset($GLOBALS['sugar_config']['logger']['default'])
                && $GLOBALS['sugar_config']['logger']['default'] == 'FirePHP' )
            LoggerManager::setLogger('default','FirePHPLogger');
    }

    /**
     * see LoggerTemplate::log()
     */
    public function log(
	    $level,
	    $message
	    )
	{
	    // change to a string if there is just one entry
	    if ( is_array($message) && count($message) == 1 )
	        $message = array_shift($message);

	    // map the sugar logging level to the appropriate FirePHP log level
	    switch ($level)
	    {
        case 'debug':
            FB::log($message);
            break;
        case 'info':
            FB::info($message);
            break;
        case 'deprecated':
        case 'warn':
            FB::warn($message);
            break;
        case 'error':
        case 'fatal':
        case 'security':
            FB::error($message);
            break;
	    }
	}
}

Now if we save this file to our custom/include/SugarLogger/ directory, and set the $sugar_config['logger']['default'] to 'FirePHP' in config.php, Sugar will output it's log messages to FireBug.

3 responses to Easily make your own SugarLogger with Sugar 6

  1. 

    This is very useful. Now we can see the log output in firefox directly.

Trackbacks and Pingbacks:

  1. SugarCRM Developer Blog » Blog Archive » Making Sugar a bit more filesystem friendly every day… - June 27, 2012

    [...] Logs ( sugarcrm.log, upgradeWizard.log, install.log ). You can put the sugarcrm.log in a different location by using the config directive ‘log_dir’ to specify it, or write your own logger class to managing the log with a different backend. [...]

  2. Problems creating new record - July 13, 2012

    [...] [...]

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s