Skip to Main Content

Java EE (Java Enterprise Edition) General Discussion

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Using StringMatchFilter in Log4J XML Config. File?

843838Jan 10 2006
I'm trying to use the StringMatchFilter in an appender to determine based on the contents of the message when to send an email, when testing with single words it works great. For example if the message has "Here is a DEBUG" and my "StringToMatch" value is DEBUG then that message is correctly identified. However if I want to match on more specific string like "Here is DEBUG" for my "StringToMatch" value it will match on every message that has the word "Here" in it anywhere. So it would incorrectly match on "Here is a WARN" when the "StringToMatch" value specified is "Here is DEBUG". Below are my two files I'm using to run my test. How should I specify a string with embedded spaces for this to work? I've tried with single quotes inside the double quotes like this: "'Here is DEBUG'" but it still matched on 'Here'.

My XML Config. file looks like this:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
  <appender name="EMAIL"  class="org.apache.log4j.net.SMTPAppender">
          <param name="BufferSize" value="512" />
          <param name="SMTPHost" value="???" />
          <param name="From" value="???" />
          <param name="To" value="???" />
          <param name="Subject" value="Test LogMessage" />
          <layout class="org.apache.log4j.PatternLayout">
                  <param name="ConversionPattern" value="[%d{ISO8601}]%n%n%-5p%n%n%c%n%n%m%n%n" />
          </layout>

          <filter class="org.apache.log4j.varia.StringMatchFilter">
                  <param name="StringToMatch" value="Here is DEBUG" />
                  <param name="AcceptOnMatch" value="true" />
          </filter>

          <filter class="org.apache.log4j.varia.LevelRangeFilter">
                  <param name="LevelMax" value="FATAL"/>
                  <param name="LevelMin" value="DEBUG"/>
          </filter>

  </appender>

  <root>
    <appender-ref ref="EMAIL"/>
  </root>
</log4j:configuration>
and my test java code looks like this:
import org.apache.log4j.Logger;
import org.apache.log4j.xml.DOMConfigurator;
public class Test{
   static Logger logger = Logger.getLogger(Test.class);
   public static void main(String args[]) {
      DOMConfigurator.configure("/home/agsteam/testing/log4j/Test.xml");
      logger.debug("Here is DEBUG");
      logger.info("Here is some INFO blah");
      logger.warn("Here is some WARN");
      logger.error("Here");
      logger.fatal("fatal here");
   }
}
In the above example code every logger message matches, the debug, info, warn, error and fatal all match because of the the 'here' that is found in the message, even though the filter StringToMatch values has "Here is DEBUG".

Thanks for any help.
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Feb 7 2006
Added on Jan 10 2006
0 comments
1,633 views