Marco Vassura Log4php - Quick Setup Guide

After unpacking the distribution file the following source tree will be created:

log4php-{version}
    +---docs
    |   +---api
    |   qsg.html (this file)
    |       ...
    \---src
        +---log4php
        |   +---appenders
        |   +---config
        |   +---helpers
        |   +---layouts
        |   +---or
        |   +---spi
        |   +---varia
        |   \---xml
        \---tests
            ...
                

Copy the "log4php-{version}/src/log4php" directory in a place accessible by PHP (called in this document {LOG4PHP-ROOT}) and that's all! Log4php is installed. Optionally the LOG4PHP_DIR constant can be defined to point to {LOG4PHP-ROOT}.

Three steps are required to use log4php:

  1. Create a configuration file (can be an ".ini"-type or xml file) that will configure the log4php loggers hierarchy tree.
    See the tests/*/configs directories for examples.
    See log4php.dtd for xml elements reference.
    Finally, take a look at the original log4j manual for more examples.
  2. (Optional) Define the LOG4PHP_CONFIGURATION constant to point to the configuration above.
  3. (Optional) Define the LOG4PHP_CONFIGURATOR_CLASS constant to point to a configuration class file.
  4. Include the 'LoggerManager.php' class file in your php scripts.
  5. Since log4php makes use of date functions, it is recommended that you have set: date_default_timezone_set(); somewhere within your application.

Once the 'LoggerManager' is included, it will start the default init procedure that can be parameterized by the previously defined LOG4PHP_DEFAULT_INIT_OVERRIDE, LOG4PHP_CONFIGURATION and LOG4PHP_CONFIGURATOR_CLASS constants.

Here is how to use log4php in user PHP code:

<?php

// Setting default timezone
date_default_timezone_set('Europe/London');

/*
    Set LOG4PHP_* constants here 
*/

require_once(LOG4PHP_DIR . '/LoggerManager.php');

/*
    In a class
*/
class Log4phpTest {
    /*
        Your public and private vars
    */
    var $_logger;
    
    function Log4phpTest()
    {
        $this->_logger =& LoggerManager::getLogger('Log4phpTest');
        $this->_logger->debug('Hello!');
    }

}

function Log4phpTestFunction()
{
    $logger =& LoggerManager::getLogger('Log4phpTestFunction');
    $logger->debug('Hello again!');    
}

/*
    Your PHP code
*/

//Safely close all appenders with...

LoggerManager::shutdown();

?>