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

前端 未结 9 1633
忘了有多久
忘了有多久 2021-01-01 05:03

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:



        
相关标签:
9条回答
  • 2021-01-01 05:41

    If you are using SQL server you can use the following

    SET QUOTED_IDENTIFIER OFF;
    INSERT INTO log_messages ( log_level, message, log_date ) VALUES ( "%p", ":%m", "%d{yyyy-MM-dd HH:mm:ss}" )'
    SET QUOTED_IDENTIFIER OFF;
    

    Which shifts the problem to double quotes. If you don't have double quotes in your messages this could solve the problem.

    0 讨论(0)
  • 2021-01-01 05:50

    Joao, sorry for being late, but here it is:

    <appender name="DB" class="org.apache.log4j.db.DBAppender">                                                                                                             
                    <connectionSource class="org.apache.log4j.db.DriverManagerConnectionSource">                                                                                       
                            <param name="driverClass" value="org.postgresql.Driver" />                                                                                                 
                            <param name="url" value="jdbc:postgresql://localhost/database" />                                                                                      
                            <param name="user" value="user" />                                                                                                                          
                            <param name="password" value="password" />                                                                                                                 
                    </connectionSource>                                                                                                                                                
                    <layout class="org.apache.log4j.PatternLayout">                                                                                                                    
                            <param name="ConversionPattern" value="%d %-5p [%t] %c - %m%n" />                                                                                          
                    </layout>                                                                                                                                                          
            </appender>
    

    Hope it helps!

    0 讨论(0)
  • 2021-01-01 05:55

    I'm not familiar with log4j or JDBC, but I do know JDBC supports prepared statements. Perhaps there is a way to use that with the JDBCAppender

    0 讨论(0)
提交回复
热议问题