LoggerAppenderPDO

LoggerAppenderPDO appender logs to a database using the PHP's PDO extension.

Layout

This appender does not require a layout.

Parameters

The following parameters are available:

Parameter Type Required Default Description
dsn string Yes - The Data Source Name (DSN) used to connect to the database.
user string Yes - Username used to connect to the database.
password string Yes - Password used to connect to the database.
createTable boolean No true If set to true, the table will be created if it doesn't exist.
table string No - Name of the table to which log entries are be inserted.
insertSql string No see below SQL query used to insert a log event.
insertPattern string No see below A comma separated list of format strings used in conjunction with insertSql parameter.

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 configuration

Parameters 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:

Parameter Default value
insertSql INSERT INTO __TABLE__ (timestamp, logger, level, message, thread, file, line) VALUES (?, ?, ?, ?, ?, ?, ?)
insertPattern %d,%c,%p,%m,%t,%F,%L

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.

Examples

Example 1

The 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.

  • XML
  • PHP
<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 2

A 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.

  • XML
  • PHP
<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   |
 +-------------------------+--------+-------+--------------+--------+--------------+------+