The purpose of this article is to document the creation of a logger using the incubation project Log4PHP from the Apache Foundation.  This logger is a port of the Log4J, and existed long before it became an incubation project at Apache.  Netlinx, Inc. has been using the 0.9.0 release for several years, and has been migrating to the Apache supported release.  Information on obtaining the Apache code can be found at http://incubator.apache.org/log4php/source-repository.html.

Part I - setting up a simple logger using the LoggerAppenderFile

In this first part we'll set up a logger that uses that LoggerAppenderFile class will log events to a log file.  First, we'll need to create a properties file.  The properties file can be either in the log4j properties format OR it can be an XML file.  In this first part, we'll demonstrate the use of the basic properties file.
Create the following properties file log4php.properties in the current directory of the web application:

log4php.rootLogger=INFO, LOGFILE
log4php.appender.LOGFILE=LoggerAppenderFile
log4php.appender.LOGFILE.name="test.log"
log4php.appender.LOGFILE.layout=LoggerPatternLayout
log4php.appender.LOGFILE.layout.ConversionPattern="%d{Y-m-d H:i:s} %F %p %m %n"

In this example properties file, we configure a root logger to accept all log levels from INFO and greater.  The rootLogger is given a "name" of LOGFILE in this example.   

Line 2: sets the appender to LoggerAppenderFile which is our class to write out to a simple log file
Line 3: defines the name of the actual log file that will get written out to the filesystem, "test.log"
Line 4: indicates which layout we plan to use, LoggerPatternLayout is the class that is used
Line 5: is the custom pattern that we want to apply to the layout.

Next, we write our simple web application to make use of the logger. Suppose we want a page that logs the $_SERVER['REMOTE_ADDR'] value to a log with a severity of INFO.  Here's what our script would look like:

require_once 'log4php/LoggerManager.php';
require_once 'log4php/LoggerPropertyConfigurator.php';

$logger =& LoggerManager::getRootLogger();
$logger->info($_SERVER['REMOTE_ADDR']);

Each time this script is executed for fetched, our logger will write a message w/ level INFO to the local test.log file with a date and timestamp. The LoggerAppenderFile appender is very basic and only supports a few parameters.  In part II we'll add functionality by using the recommended LoggerAppenderRollingFile appender.

Part II - setting up a logger using the LoggerAppenderRollingFile

Using the LoggerAppenderRollingFile appender we hvae a few more options and features that can be configured on our logger.  Using this appender our logger can be configured with the maxBackupIndex option to set the maximum number of backup files to retain.  This option determines the number of backup files that are retained before the oldest is erased or overwritten.  Additionally, this appender supports the ability to set the maximum size that the output log is allowed to reach before being rolled over to backup files.  The option MaxFileSize is set to an integer along with suffixes "KB", "MB", or "GB" to establish the max file size that our logger will write to.  The default is 10 MB and cannot be set to a value greater than 2GB.  An example properties file would look like the following:

log4php.rootLogger=INFO, LOGFILE
log4php.appender.LOGFILE=LoggerAppenderRollingFile
log4php.appender.LOGFILE.maxBackupIndex=2
log4php.appender.LOGFILE.maxFileSize=10240
log4php.appender.LOGFILE.filename="test.log"
log4php.appender.LOGFILE.layout=LoggerPatternLayout
log4php.appender.LOGFILE.layout.ConversionPattern="%d{Y-m-d H:i:s} %F %p %m %n"

This configuration will make use of the LoggerAppenderRollingFile appender, keeping up to 2 log files, each up to 10MB in size.  All other parameters remained the same from our first configuration.