Hi again,
I'm wondering if it is possible to use the ExpressionFilter to only log
events occurring within a certain time span each day. For example, I don't
want to log events in my application between the hours of 01:00 and 03:00
(due to scheduled environment outage where tons of errors are spewed forth).
It seems that the TIMESPAN filter field in ExpressionFilter is a long which
represents a specific date. So I could then log all events occurring after,
say, "2007/01/01 12:00". But can I use this to only log events relative to a
certain time each day? If it can only be a hard date, then I don't see much
point of the filter, unless you knew that you didn't want to log information
for a given day at some point in the future (an unlikely scenario).
Here's my appender below. I've got it to only log events from com.creditex,
but I want to restrict it further that that it doesn't log events between
01:00 and 03:00 each day.
<appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d %-5p [%t] %c
(%F:%L) - %m%n"/>
</layout>
<filter class="org.apache.log4j.filter.ExpressionFilter">
<param name="Expression" value="(LOGGER ~= com.creditex)" />
<param name="AcceptOnMatch" value="true"/>
<param name="ConvertInFixToPostFix" value="true"/>
</filter>
<filter class="org.apache.log4j.filter.DenyAllFilter"/>
</appender>
One more thing: why is TIMESPAN a long instead of a date string in the
format of, say, "yyyy/MM/dd hh:mm:ss". Is there a quick way to determine the
long value of a date I have in mind?
Cheers.
Simon Wallis
Developer
RBS Global Banking & Markets
London - 42 Islington High St, (Regents House) - N1 8XL
Office: +44 20 7085 2929
-----Original Message-----
From: WALLIS, Simon, GBM
Sent: 15 August 2007 15:09
To: 'Log4J Users List'
Subject: RE: Using the extras companion
Thanks Curt.
It turns out the problem was that I didn't have jakarta-oro-2.0.8.jar in my
classpath. I'm not sure if I was supposed to know about this requirement,
but I didn't notice it until I stepped through the log4j code in my debugger
and saw it was getting a class not found exception trying to throw a
MalformedPatternException.
I've got it working now so thanks for your help!
Simon Wallis
Developer
RBS Global Banking & Markets
London - 42 Islington High St, (Regents House) - N1 8XL
-----Original Message-----
From: Curt Arnold [mailto:[EMAIL PROTECTED]
Sent: 15 August 2007 02:00
To: Log4J Users List
Subject: Re: Using the extras companion
On Aug 14, 2007, at 11:44 AM, WALLIS, Simon, GBM wrote:
> Hi,
>
>
>
> I have downloaded the log4j extras companion from
> http://people.apache.org/builds/logging/log4j/companions/extras/1.0/
> <http://people.apache.org/builds/logging/log4j/companions/extras/
> 1.0/> and
> I am trying to use an Expression Filter with log4j 1.2.13. I have
> added the
> apache-log4j-extras-1.0.jar to my classpath. But, for the life of me I
> cannot get it to work.
>
>
>
> Here is my appender config where I'm trying to use the Expression
> Filter.
>
....
>
> It just seems to halt on load and doesn't give me any error
> message. Here is
> the log4j debug output. You will see it just halts after parsing the
> convertInFixToPostFix property.
>
>
>
> log4j: Class name: [org.apache.log4j.net.SMTPAppender]
>
> log4j: Setting property [SMTPHost] to [blah].
>
> log4j: Setting property [from] to [EMAIL PROTECTED] ].
>
> log4j: Setting property [to] to [EMAIL PROTECTED] ].
>
> log4j: Setting property [subject] to [my subject].
>
> log4j: Setting property [bufferSize] to [50].
>
> log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
>
> log4j: Setting property [conversionPattern] to [%d{yyyy-MM-dd
> HH:mm:ss,SSS}
> %5p %c{1}:%L - %m%n].
>
> log4j: Setting property [expression] to [msg ~= blah].
>
> log4j: Setting property [acceptOnMatch] to [true].
>
> log4j: Setting property [convertInFixToPostFix] to [true].
>
>
...
Thanks for the feedback. I've tried but haven't been able to
reproduce the problem. The log that I'm getting looks like:
log4j: Threshold ="".
log4j: Class name: [org.apache.log4j.net.SMTPAppender]
log4j: Setting property [SMTPHost] to [blah].
log4j: Setting property [from] to [EMAIL PROTECTED] ].
log4j: Setting property [to] to [EMAIL PROTECTED] ].
log4j: Setting property [subject] to [my subject].
log4j: Setting property [bufferSize] to [50].
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%d{yyyy-MM-dd
HH:mm:ss,SSS} %5p %c{1}:%L - %m%n].
log4j: Setting property [expression] to [msg ~= blah].
log4j: Setting property [acceptOnMatch] to [true].
log4j: Setting property [convertInFixToPostFix] to [true].
log4j: Adding filter of type [class
org.apache.log4j.filter.ExpressionFilter] to appender named
[SMTPAppender].
log4j: Adding filter of type [class
org.apache.log4j.filter.DenyAllFilter] to appender named [SMTPAppender].
log4j: Adding appender named [SMTPAppender] to category [root].
log4j: Level value for root is [DEBUG].
log4j: root level set to DEBUG
Disconnected from the target VM, address: '127.0.0.1:56913',
transport: 'socket'
The next major event that follows "Setting property
[convertInFixToPostFix]" that ends your log should be the call to
propSetter.activate() in DOMConfigurator.parseFilters. The
propSetter.activate() call should in turn call
ExpressionFilter.activateOptions(). If that had succeeded, then you
should have seen the "Adding filter of type ...ExpressionFilter"
message from the line following the propSetter.activateCall(). If
you can set a breakpoint on the propSetter.activate call and then
step-into, hopefully you can get an indication of what is going wrong.
I made up a test case that parses an configuration file with your
snippet of configuration code. To apply
cd apache-log4j-extras-1.0
echo "" > src/test/resource/org/apache/log4j/filter/filteredsmtp.xml
patch -p 1 -i extras.patch
where extras.patch contains
diff -u -r apache-log4j-extras-1.0/pom.xml apache-log4j-extras-
patched/pom.xml
--- apache-log4j-extras-1.0/pom.xml 2007-06-30 01:09:21.000000000 -0500
+++ apache-log4j-extras-patched/pom.xml 2007-08-14 19:01:50.000000000
-0500
@@ -180,9 +180,14 @@
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
- <version>1.2.9</version>
- </dependency>
- <dependency>
+ <version>1.2.13</version>
+ </dependency>
+ <dependency>
+ <groupId>javax.mail</groupId>
+ <artifactId>mail</artifactId>
+ <version>1.4</version>
+ </dependency>
+ <dependency>
<groupId>oro</groupId>
<artifactId>oro</artifactId>
<version>2.0.8</version>
diff -u -r apache-log4j-extras-1.0/src/test/java/org/apache/log4j/
filter/SimpleFilterTest.java apache-log4j-extras-patched/src/test/
java/org/apache/log4j/filter/SimpleFilterTest.java
--- apache-log4j-extras-1.0/src/test/java/org/apache/log4j/filter/
SimpleFilterTest.java 2007-06-30 01:09:13.000000000 -0500
+++ apache-log4j-extras-patched/src/test/java/org/apache/log4j/filter/
SimpleFilterTest.java 2007-08-14 19:13:33.000000000 -0500
@@ -21,6 +21,7 @@
import junit.framework.TestSuite;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
+import org.apache.log4j.Appender;
import org.apache.log4j.util.Compare;
import org.apache.log4j.util.ControlFilter;
import org.apache.log4j.util.Filter;
@@ -29,6 +30,7 @@
import org.apache.log4j.util.SunReflectFilter;
import org.apache.log4j.util.Transformer;
import org.apache.log4j.xml.DOMConfigurator;
+import org.apache.log4j.xml.Log4jEntityResolver;
import org.w3c.dom.Document;
import javax.xml.parsers.DocumentBuilder;
@@ -69,6 +71,26 @@
root.getLoggerRepository().resetConfiguration();
}
+ public void test13() throws Exception {
+ String resourceName = "filteredsmtp.xml";
+ InputStream is = getClass().getResourceAsStream(resourceName);
+ if (is == null) {
+ throw new FileNotFoundException(
+ "Could not find resource " + resourceName);
+ }
+ DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
+ DocumentBuilder builder = factory.newDocumentBuilder();
+ builder.setEntityResolver(new Log4jEntityResolver());
+ Document doc = builder.parse(is);
+ org.apache.log4j.xml.DOMConfigurator.configure
(doc.getDocumentElement());
+ Appender smtpAppender = Logger.getRootLogger().getAppender
("SMTPAppender");
+ assertNotNull(smtpAppender);
+ org.apache.log4j.spi.Filter filter = smtpAppender.getFilter();
+ assertTrue(filter instanceof
org.apache.log4j.filter.ExpressionFilter);
+ org.apache.log4j.spi.Filter filter2 = filter.getNext();
+ assertTrue(filter2 instanceof
org.apache.log4j.filter.DenyAllFilter);
+ }
+
public void test1() throws Exception {
InputStream is = getClass().getResourceAsStream
("simpleFilter1.xml");
@@ -122,10 +144,4 @@
root.error("Message " + i, e);
}
- public static Test suite() {
- TestSuite suite = new TestSuite();
- suite.addTest(new SimpleFilterTest("test1"));
- return suite;
- }
-
}
diff -u -r apache-log4j-extras-1.0/src/test/resources/org/apache/
log4j/filter/filteredsmtp.xml apache-log4j-extras-patched/src/test/
resources/org/apache/log4j/filter/filteredsmtp.xml
--- apache-log4j-extras-1.0/src/test/resources/org/apache/log4j/
filter/filteredsmtp.xml 2007-08-14 19:20:20.000000000 -0500
+++ apache-log4j-extras-patched/src/test/resources/org/apache/log4j/
filter/filteredsmtp.xml 2007-08-14 19:01:49.000000000 -0500
@@ -1 +1,47 @@
+<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"
debug="true">
+<appender name="SMTPAppender"
+class="org.apache.log4j.net.SMTPAppender">
+
+ <param name="SMTPHost" value="blah"/>
+
+ <param name="From" value="[EMAIL PROTECTED] "/>
+
+ <param name="To" value="[EMAIL PROTECTED] "/>
+
+ <param name="Subject" value="my subject"/>
+
+ <param name="BufferSize" value="50" />
+
+ <layout class="org.apache.log4j.PatternLayout">
+
+ <param name="ConversionPattern"
+value="%d{yyyy-MM-dd HH:mm:ss,SSS} %5p %c{1}:%L - %m%n"/>
+
+ </layout>
+
+ <filter
+class="org.apache.log4j.filter.ExpressionFilter">
+
+ <param name="Expression" value="msg ~=
+blah" />
+
+ <param name="AcceptOnMatch"
+value="true"/>
+
+ <param name="ConvertInFixToPostFix"
+value="true"/>
+
+ </filter>
+
+ <filter
+class="org.apache.log4j.filter.DenyAllFilter"/>
+
+</appender>
+
+ <root>
+ <appender-ref ref="SMTPAppender"/>
+ <level value="DEBUG"/>
+ </root>
+
+</log4j:configuration>
To run, install Maven 2.0.7 on your path and then run "mvn test".
You should get 339 test run and 0 errors or failures. target/
surefire-reports/org...SimpleFilterTest.txt should list two tests
run or should contain details on the test failure on the new test.
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
****************************************************************************
*******
The Royal Bank of Scotland plc. Registered in Scotland No 90312. Registered
Office: 36 St Andrew Square, Edinburgh EH2 2YB.
Authorised and regulated by the Financial Services Authority
This e-mail message is confidential and for use by the
addressee only. If the message is received by anyone other
than the addressee, please return the message to the sender
by replying to it and then delete the message from your
computer. Internet e-mails are not necessarily secure. The
Royal Bank of Scotland plc does not accept responsibility for
changes made to this message after it was sent.
Whilst all reasonable care has been taken to avoid the
transmission of viruses, it is the responsibility of the recipient to
ensure that the onward transmission, opening or use of this
message and any attachments will not adversely affect its
systems or data. No responsibility is accepted by The
Royal Bank of Scotland plc in this regard and the recipient should carry
out such virus and other checks as it considers appropriate.
Visit our websites at:
www.rbs.com
www.rbs.com/gbm
www.rbsgc.com
****************************************************************************
*******
______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email
______________________________________________________________________
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]