Renderers

Apache log4php can log more than just string messages. If you try to log an object it will be converted to a string and logged. The component which converts Objects to strings in log4php is called a renderer.

Default renderer

For example, let's say that an application uses a Person object, like this one:

class Person {
    public $firstName;
    public $lastName;    
    public $age;
}

If you try logging it:

$person = new Person();
$person->firstName = 'John';
$person->lastName = 'Doe';
$person->age = 37;

$logger = Logger::getLogger('main');
$logger->info("Here comes the person:");
$logger->info($person);

Log4php will render it using the default renderer and you will end the output will look something like:

INFO - Here comes the person:
INFO - Person::__set_state(array(
   'firstName' => 'John',
   'lastName' => 'Doe',
   'age' => 37,
))

Creating a custom renderer

In order to make log4php render our Person object in a more readable format, we need to create a custom renderer class which will convert Person objects to a string suitable for logging.

Let's call our renderer class PersonRenderer.

/** All object renderers must implement the LoggerRendererObject interface. */
class PersonRenderer implements LoggerRendererObject {
    public function render($person) {
        return "{$person->firstName} {$person->lastName} ({$person->age})";
    }
}

Now log4php has to be configured to use PersonRenderer for rendering Person objects. This is done in the configuration file.

XML configuration:

<configuration xmlns="http://logging.apache.org/log4php/">
    <renderer renderedClass="Person" renderingClass="PersonRenderer" />
    <appender name="defualt" class="LoggerAppenderEcho" />
    <root>
        <appender_ref ref="defualt" />
    </root>
</configuration>

Equivalent PHP configuration:

array(
    'renderers' => array(
        'renderedClass' => 'Person',
        'renderingClass' => 'PersonRenderer'
    ),
    'appenders' => array(
        'default' => array(
            'class' => 'LoggerAppenderEcho',
        ),
    ),
    'rootLogger' => array(
        'appenders' => array('default'),
    ),
);

If the same code is run as above, the following output is produced:

INFO - Here comes the person:
INFO - John Doe (37)

Which is much more readable than the default rendering.

Class hierarchy

Object rendering follows the class hierarchy.

For example, if there is a class named Fruit, and classes Orange, Lemon and Apple all extend Fruit. When FruitRenderer is registered as renderer for the Fruit class, all subclasses of Fruit will also be rendered by FruitRenderer. Of course, it is possible to override this by registering OrangeRenderer as renderer for the Orange class.