LoggerAppenderPDOLoggerAppenderPDO appender logs to a database using the PHP's PDO extension. ParametersThe following parameters are available:
Parameters dsn, user and password are used by PDO to connect to the database which will be used for logging. For available database drivers and corresponding DSN format, please see the PDO driver documentation. Advanced configurationParameters insertSql and insertPattern can be used to change how events are inserted into the database. By manipulating them, it is possible to use a custom table structure to suit your needs. WARNING: Change these settings only if you are sure you know what you are doing. The default values of these parameters are:
The string __TABLE__ in insertSql will be replaced with the table name defined in table. Question marks in insertSql will be replaced by evaluated LoggerPatternLayout format strings defined in insertPattern. See LoggerPatternLayout documentation for format string description. ExamplesExample 1The simplest example is connecting to an SQLite database which does not require any authentication. SQLite databases are contained in simple files and don't reuquire a server to run. This example will log to the database contained in /var/log/log.sqlite. The database will be created if it doesn't already exist.
<configuration xmlns="http://logging.apache.org/log4php/"> <appender name="default" class="LoggerAppenderPDO"> <param name="dsn" value="sqlite:/var/log/log.sqlite" /> </appender> <root> <appender_ref ref="default" /> </root> </configuration> array( 'appenders' => array( 'default' => array( 'class' => 'LoggerAppenderPDO', 'params' => array( 'dsn' => 'sqlite:/var/log/log.sqlite', ), ), ), 'rootLogger' => array( 'appenders' => array('default'), ), ); Example 2A slightly more complex example is connecting to a MySQL database which requires user credentials to be provided. Additionally, a user-specified table name is used. For this example, MySQL server needs to be running on localhost, and needs to contain a database named logdb. The log will be written to a table named log_table. The table will be created if it doesn't already exist.
<configuration xmlns="http://logging.apache.org/log4php/"> <appender name="default" class="LoggerAppenderPDO"> <param name="dsn" value="mysql:host=localhost;dbname=logdb" /> <param name="user" value="root" /> <param name="password" value="secret" /> <param name="table" value="log_table" /> </appender> <root> <appender_ref ref="default" /> </root> </configuration> array( 'appenders' => array( 'default' => array( 'class' => 'LoggerAppenderPDO', 'params' => array( 'dsn' => 'mysql:host=localhost;dbname=logdb', 'user' => 'root', 'password' => 'secret', 'table' => 'log_table', ), ), ), 'rootLogger' => array( 'appenders' => array('default'), ), ); This is an output sample retrieved from the MySQL database. mysql> desc log_table; +-----------+---------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-----------+---------------+------+-----+---------+-------+ | timestamp | varchar(32) | YES | | NULL | | | logger | varchar(64) | YES | | NULL | | | level | varchar(32) | YES | | NULL | | | message | varchar(9999) | YES | | NULL | | | thread | varchar(32) | YES | | NULL | | | file | varchar(255) | YES | | NULL | | | line | varchar(6) | YES | | NULL | | +-----------+---------------+------+-----+---------+-------+ mysql> SELECT * FROM log_table; +-------------------------+--------+-------+--------------+--------+--------------+------+ | timestamp | logger | level | message | thread | file | line | +-------------------------+--------+-------+--------------+--------+--------------+------+ | 2011-10-02 19:36:07,935 | main | INFO | Hello World! | 21858 | /tmp/log.php | 24 | +-------------------------+--------+-------+--------------+--------+--------------+------+ |