How to sanitize log messages in Log4j to save them in database

Posted by Rafael on Stack Overflow See other posts from Stack Overflow or by Rafael
Published on 2010-01-11T13:24:59Z Indexed on 2010/04/30 20:17 UTC
Read the original article Hit count: 232

Filed under:
|
|

Hello,

I'm trying to save log messages to a central database. In order to do this, I configured the following Appender in log4j's xml configuration:

<appender name="DB" class="org.apache.log4j.jdbc.JDBCAppender">
            <param name="URL" value="jdbc:postgresql://localhost/logging_test" />
            <param name="user" value="test_user" />
            <param name="password" value="test_password" />
            <param name="sql" value="INSERT INTO log_messages ( log_level, message, log_date ) VALUES ( '%p', '%m', '%d{yyyy-MM-dd HH:mm:ss}' )" />
</appender>

This works fine, except some of the messages contain ', and then the appender fails.

Is there an easy way to do this?

© Stack Overflow or respective owner

Related posts about java

Related posts about log4j