Anonymous7
Anonymous7

Reputation: 41

Equivalent of DailyRollingFileAppender from log4j in log4j2

we were earlier using Log4j in our application. we knew that some of the errors are logged are not actual errors. These logs are not directly from application but from some of the dependent jars. One option would be to downgrade all the errors from specified class. But we don't want to do that as the blast radius was more. We looked for specifically if the the logging msg from the class is not actual error then we downgraded the error to info. Now we have upgraded to log4j2 and are trying to find equivalent to this.

Here is the logic in log4j

    TestRollingFileAppender extends DailyRollingFileAppender {
     @Override
        protected void subAppend(LoggingEvent event) {
     if (event.getLevel().isGreaterOrEqual(Level.WARN)) {
           //stringsToDowngrade reads from properties file and gets set of msgs that can be downgraded
                String[] testSubstrings = stringsToDowngrade(event.getLogger());
      for (String testString : testSubstrings) {
                if (event.getMessage() != null &&
                        StringUtils.isNotBlank(testString) &&
                        event.getMessage().toString().contains(testString)) {

                   event = new LoggingEvent(...,LEVEL.WARN,...);

                 break;
                }
            }

    }
}

Upvotes: 1

Views: 705

Answers (1)

rgoers
rgoers

Reputation: 9141

You have a couple of choices.

  1. You can use a Filter. The regex filter would filter out the messages that are not errors by matching on them with a regex. Admittedly, this could get ugly if you have a log of messages to filter. You can also use a script filter to match on the messages you want and reject them.
  2. If you want to modify the events then you can wrap your Appender in a RewriteAppender. You will have to write your own custom RewritePolicy as Log4j does not yet implement a ScriptRewritePolicy. If you would prefer that please open a Jira issue to request that and I will look at implementing it.

Upvotes: 2

Related Questions