Search This Blog

Showing posts with label log4j. Show all posts
Showing posts with label log4j. Show all posts

Wednesday, May 30, 2012

log4j filter some text and don't log it

Use org.apache.log4j.varia.StringMatchFilter to filter the text not to display.  Here is the example for log4j.xml:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
  <appender name="console" class="org.apache.log4j.ConsoleAppender">
    <layout class="org.apache.log4j.PatternLayout">
      <param name="ConversionPattern" value="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5p - %m%n" />
    </layout>

    <filter class="org.apache.log4j.varia.StringMatchFilter">
      <param name="StringToMatch" value="the-text-not-to-log" />
      <param name="AcceptOnMatch" value="false" />
    </filter>

    <!-- <filter class="org.apache.log4j.varia.DenyAllFilter" /> -->
  </appender>

  <root>
    <priority value="info" />
    <appender-ref ref="console" />
  </root>
</log4j:configuration>
Then run the following to test:

        LOG.info("This is an info and shouldn't be logged with the-text-not-to-log and blah blah");
        LOG.info("This is logged");
        LOG.debug("This is a debug and not logged");
        LOG.error("This is an error and logged");

If you only want to log some text, then change the value for AcceptOnMatch to true and add DenyAllFilter.