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

前端 未结 9 1721
忘了有多久
忘了有多久 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:33

    i solved the thing in the following way :

    Copied the source code of the JDBCAppender called ACMEJDBCAppender

    override the getLogStatement(LoggingEvent event) method, cloning the old event and providing the new one with the escaped message.

    Not the cleanest solution from the oop point of view but it does the work. Hope it helps.

    protected String getLogStatement(LoggingEvent event) {
    
      LoggingEvent clone = new LoggingEvent(
        event.fqnOfCategoryClass,
        LogManager.getLogger(event.getLoggerName()),
        event.getLevel(),
        AidaUtils.sqlEscape(event.getMessage().toString()),
        event.getThrowableInformation()!=null ? event.getThrowableInformation().getThrowable() : null
      );
    
      return getLayout().format(clone);
    }
    

提交回复
热议问题