Custom Loggers

Overview

How to integrate the logger with alternate logging systems.

Custom Loggers

Custom loggers can be used to write log entries to a centralized application management tool, or to write messages to a developer tool such as FirePHP.
To do this, you can create a new instance class that implements the LoggerTemplate interface. The below code is an example of how to create a FirePHP logger.
You will first need to create your logger class in ./custom/include/SugarLogger/. For this example, we will create ./custom/include/SugarLogger/FirePHPLogger.php.
<?php

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

class FirePHPLogger implements LoggerTemplate{
    
    /** Constructor */
    public function __construct()
    {
        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);
        }
        
        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;
        }
    }
}
The only method that needs to be implemented by default is the log() method, which writes the log message to the backend. You can specify which log levels this backend can use in the constuctor by calling the LoggerManager::setLogger() method and specifying the level to use for this logger in the first parameter; passing ‘default’ makes it the logger for all logging levels.
You will then specify your default logger as ‘FirePHP’ in your config_override.php file.
$sugar_config['logger']['default'] = 'FirePHP';