This xml configuration example was submitted by Rich Bagley (thank you!), and is compatible with v1.2.7. A version specific to your environment will vary. Please see the JDBCAppender documentation.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <appender name="jdbcAppender" class="org.apache.log4j.jdbc.JDBCAppender"> 
        <param name="URL" value="jdbc:oracle:thin:@sd1.hbs.edu:1521:sc1" /> 
        <param name="Driver" value="oracle.jdbc.driver.OracleDriver" /> 
        <param name="User" value="user" /> 
        <param name="Password" value="password" /> 
        <layout class="org.apache.log4j.PatternLayout"> 
            <param name="ConversionPattern" 
              value="INSERT INTO LOGGING_SAMPLES_TEST (log_date, log_level, location, message) VALUES ( '%d{ISO8601}','%p', '%C;%L', '%m' )" 
            /> 
        </layout> 
    </appender> 

    <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender"> 
        <layout class="org.apache.log4j.PatternLayout"> 
            <param name="ConversionPattern" 
              value="%d{ISO8601} %p (%C;%L) %m%n" 
            /> 
        </layout> 
    </appender> 

    <logger name="logging.simple.jdbcLogger"> 
        <level value="debug"/> 
        <appender-ref ref="jdbcAppender"/> 
    </logger> 

    <root> 
        <level value="error"/> 
        <appender-ref ref="STDOUT"/> 
    </root> 

</log4j:configuration>

A config for postgresql

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <appender name="jdbcAppender" class="org.apache.log4j.jdbc.JDBCAppender"> 
        <param name="URL" value="jdbc:postgresql:db" /> 
        <param name="Driver" value="org.postgresql.Driver" /> 
        <param name="User" value="user" /> 
        <param name="Password" value="password" /> 
        <layout class="org.apache.log4j.PatternLayout"> 
            <param name="ConversionPattern" 
              value="INSERT INTO log4j (log_date, log_level, log_location, log_message) VALUES ( '%d{yyyy-MM-dd}','%p', '%C;%L', '%m' )" 
            /> 
        </layout> 
    </appender> 

    <appender name="STDOUT" class="org.apache.log4j.ConsoleAppender"> 
        <layout class="org.apache.log4j.PatternLayout"> 
            <param name="ConversionPattern" 
              value="%d{ISO8601} %p (%C;%L) %m%n" 
            /> 
        </layout> 
    </appender> 

    <logger name="logging.simple.jdbcLogger"> 
        <level value="debug"/> 
        <appender-ref ref="jdbcAppender"/> 
    </logger> 

    <root> 
        <level value="error"/> 
        <appender-ref ref="STDOUT"/> 
    </root> 

</log4j:configuration>

If the first field is declare as a date in the db (it's the most usefull method, i think), the %d{}-variable must be changed. Also the SQL-statement is other than for oracle, the driver is changed and the url is another one.

An alternative JDBC appender (with documentation) is available at http://www.dankomannhaupt.de/projects/index.html that has a few additional features which may be of interest.

JDBCAppenderConfiguration (last edited 2012-02-04 20:44:01 by PidSter)