Jacob
Jacob

Reputation: 14731

Log4j not writing logs to database

I need to log errors for my application to database. I added the following in log4j.xml Database logs are not getting written to tables, but I could see log messages on my console.

What could be the reason for this. My database connect string details are correct.

log4j.xml

  <appender name="jdbcAppender" class="org.apache.log4j.jdbc.JDBCAppender"> 
        <param name="URL" value="jdbc:oracle:thin:@host:1521:test" /> 
        <param name="Driver" value="oracle.jdbc.driver.OracleDriver" /> 
        <param name="User" value="scott" /> 
        <param name="Password" value="tiger" /> 
        <layout class="org.apache.log4j.PatternLayout"> 
            <param name="ConversionPattern" 
              value="INSERT INTO my_table (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" additivity="true">
        <level value="info"/> 
        <appender-ref ref="jdbcAppender"/> 
    </logger> 
    <root> 
        <level value="info"/> 
        <appender-ref ref="STDOUT"/> 
    </root> 
</log4j:configuration>

Upvotes: 1

Views: 3748

Answers (2)

Chris Nava
Chris Nava

Reputation: 6802

Add <appender-ref ref="jdbcAppender"/> to the <root>...</root> section.

Upvotes: 1

matt b
matt b

Reputation: 139931

You'll see log messages on the console because you've sent all log messages to the console, but only loggers under the hierarchy of logging.simple.jdbcLogger are sent to jdbcAppender.

Do you see logging.simple.jdbcLogger messages on the console?

Upvotes: 4

Related Questions