[logback-dev] svn commit: r913 - in logback/trunk: logback-access/src/main/java/ch/qos/logback/access/net logback-access/src/test/java/ch/qos/logback/access/net logback-access/src/test/java/ch/qos/logback/access/pattern/helpers logback-classic/src/main/java/ch/qos/logback/classic/net logback-classic/src/test/java/ch/qos/logback/classic/net logback-core/src/main/java/ch/qos/logback/core/net
noreply.seb at qos.ch
noreply.seb at qos.ch
Tue Nov 14 16:17:30 CET 2006
Author: seb
Date: Tue Nov 14 16:17:29 2006
New Revision: 913
Added:
logback/trunk/logback-access/src/main/java/ch/qos/logback/access/net/DefaultSMTPEvaluator.java
logback/trunk/logback-access/src/test/java/ch/qos/logback/access/net/
logback/trunk/logback-access/src/test/java/ch/qos/logback/access/net/DefaultSMTPEvaluatorTest.java
logback/trunk/logback-access/src/test/java/ch/qos/logback/access/net/PackageTest.java
logback/trunk/logback-access/src/test/java/ch/qos/logback/access/pattern/helpers/DummyServerAdapter.java
Modified:
logback/trunk/logback-access/src/main/java/ch/qos/logback/access/net/SMTPAppender.java
logback/trunk/logback-access/src/test/java/ch/qos/logback/access/pattern/helpers/DummyResponse.java
logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SMTPAppender.java
logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SMTPAppenderTest.java
logback/trunk/logback-core/src/main/java/ch/qos/logback/core/net/SMTPAppenderBase.java
Log:
SMTPAppenders now use an EventEvaluator.
Classic version uses a JaninoEventEvaluator with a given default expression.
Access version uses a class extending JaninoEventEvaluator to implement the correct behavior.
Added: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/net/DefaultSMTPEvaluator.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/net/DefaultSMTPEvaluator.java Tue Nov 14 16:17:29 2006
@@ -0,0 +1,39 @@
+package ch.qos.logback.access.net;
+
+import ch.qos.logback.access.boolex.JaninoEventEvaluator;
+import ch.qos.logback.core.Context;
+import ch.qos.logback.core.boolex.EvaluationException;
+
+public class DefaultSMTPEvaluator extends JaninoEventEvaluator {
+
+ private static final long ONE_DAY = 1000*60*60*24;
+ private long LAST_TRIGGER_DATE = 0L;
+
+ public DefaultSMTPEvaluator(Context context) {
+ this.context = context;
+ setName("SMTPAppender's default event evaluator");
+ setExpression("event.getStatusCode() >= 500");
+ }
+
+ /**
+ * Is this <code>event</code> the e-mail triggering event?
+ *
+ * <p>
+ * This method returns <code>true</code>, if the event is
+ * evaluated to true. Otherwise it returns <code>false</code>.
+ *
+ * Once an email is sent, the next one will not be sent unless a certain amount
+ * of time passed.
+ */
+ @Override
+ public boolean evaluate(Object event) throws EvaluationException {
+
+ if (super.evaluate(event)) {
+ if (System.currentTimeMillis() >= LAST_TRIGGER_DATE + ONE_DAY) {
+ LAST_TRIGGER_DATE = System.currentTimeMillis();
+ return true;
+ }
+ }
+ return false;
+ }
+}
Modified: logback/trunk/logback-access/src/main/java/ch/qos/logback/access/net/SMTPAppender.java
==============================================================================
--- logback/trunk/logback-access/src/main/java/ch/qos/logback/access/net/SMTPAppender.java (original)
+++ logback/trunk/logback-access/src/main/java/ch/qos/logback/access/net/SMTPAppender.java Tue Nov 14 16:17:29 2006
@@ -10,14 +10,12 @@
package ch.qos.logback.access.net;
-import java.io.File;
-
import ch.qos.logback.access.PatternLayout;
-import ch.qos.logback.core.helpers.CyclicBuffer;
import ch.qos.logback.access.spi.AccessEvent;
import ch.qos.logback.core.Layout;
+import ch.qos.logback.core.boolex.EventEvaluator;
+import ch.qos.logback.core.helpers.CyclicBuffer;
import ch.qos.logback.core.net.SMTPAppenderBase;
-import ch.qos.logback.core.rolling.TriggeringPolicy;
/**
* Send an e-mail when a specific access event occurs, typically on server errors.
@@ -34,7 +32,7 @@
* an email will be sent only if the previous email was sent more that 24 hours ago.
* <p>
* This behaviour can be easily bypassed either by modifying this class, or by
- * imlementing a new <code>TriggeringPolicy</code>.
+ * imlementing a new <code>EventEvaluator</code>.
* <p>
* @author Ceki Gülcü
* @author Sébastien Pennec
@@ -49,19 +47,26 @@
/**
* The default constructor will instantiate the appender with a
- * {@link TriggeringEventEvaluator} that will trigger on events with level
+ * {@link EventEvaluator} that will trigger on events with level
* ERROR or higher.
*/
public SMTPAppender() {
- this(new DefaultEvaluator());
+ DefaultSMTPEvaluator evaluator = new DefaultSMTPEvaluator(getContext());
+ setEventEvaluator(evaluator);
}
/**
* Use <code>evaluator</code> passed as parameter as the {@link
- * TriggeringEventEvaluator} for this SMTPAppender.
+ * EventEvaluator} for this SMTPAppender.
*/
- public SMTPAppender(TriggeringPolicy evaluator) {
- this.triggeringPolicy = evaluator;
+ public SMTPAppender(EventEvaluator evaluator) {
+ this.eventEvaluator = evaluator;
+ }
+
+ @Override
+ public void start() {
+ ((DefaultSMTPEvaluator) eventEvaluator).start();
+ super.start();
}
/**
@@ -115,47 +120,3 @@
return pl;
}
}
-
-class DefaultEvaluator implements TriggeringPolicy {
-
- private boolean started;
-
- private static final int TRIGGERING_STATUS_CODE = 500;
- private static final long ONE_DAY = 1000*60*60*24;
- private long LAST_TRIGGER_DATE = 0L;
-
-
- /**
- * Is this <code>event</code> the e-mail triggering event?
- *
- * <p>
- * This method returns <code>true</code>, if the event status code
- * is 500 (server error) or higher. Otherwise it returns <code>false</code>.
- *
- * Once an email is sent, the next one will not be sent unless a certain amount
- * of time passed.
- */
- public boolean isTriggeringEvent(File activeFile, Object eventObject) {
- AccessEvent event = (AccessEvent) eventObject;
-
- if (TRIGGERING_STATUS_CODE <= event.getStatusCode()) {
- if (System.currentTimeMillis() >= LAST_TRIGGER_DATE + ONE_DAY) {
- LAST_TRIGGER_DATE = System.currentTimeMillis();
- return true;
- }
- }
- return false;
- }
-
- public boolean isStarted() {
- return started == true;
- }
-
- public void start() {
- started = true;
- }
-
- public void stop() {
- started = false;
- }
-}
Added: logback/trunk/logback-access/src/test/java/ch/qos/logback/access/net/DefaultSMTPEvaluatorTest.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-access/src/test/java/ch/qos/logback/access/net/DefaultSMTPEvaluatorTest.java Tue Nov 14 16:17:29 2006
@@ -0,0 +1,61 @@
+package ch.qos.logback.access.net;
+
+import junit.framework.TestCase;
+import ch.qos.logback.access.pattern.helpers.DummyRequest;
+import ch.qos.logback.access.pattern.helpers.DummyResponse;
+import ch.qos.logback.access.pattern.helpers.DummyServerAdapter;
+import ch.qos.logback.access.spi.AccessEvent;
+import ch.qos.logback.core.Context;
+import ch.qos.logback.core.ContextBase;
+import ch.qos.logback.core.boolex.EvaluationException;
+
+public class DefaultSMTPEvaluatorTest extends TestCase {
+
+
+ Context context = new ContextBase();
+ DefaultSMTPEvaluator evaluator;
+ DummyRequest request;
+ DummyResponse response;
+ DummyServerAdapter serverAdapter;
+
+ public void setUp() throws Exception {
+ evaluator = new DefaultSMTPEvaluator(context);
+ evaluator.start();
+ request = new DummyRequest();
+ response = new DummyResponse();
+ serverAdapter = new DummyServerAdapter(request, response);
+ super.setUp();
+ }
+
+ public void tearDown() throws Exception {
+ evaluator.stop();
+ evaluator = null;
+ request = null;
+ response = null;
+ serverAdapter = null;
+ context = null;
+ }
+
+ public void testExpectFalseBecauseOfStatus() throws EvaluationException {
+ AccessEvent ae = new AccessEvent(request, response, serverAdapter);
+ response.setStatus(100);
+ assertFalse(evaluator.evaluate(ae));
+ }
+
+ public void testExpectTrue() throws EvaluationException {
+ AccessEvent ae = new AccessEvent(request, response, serverAdapter);
+ response.setStatus(500);
+ assertTrue(evaluator.evaluate(ae));
+ }
+
+ public void testExpectFalseBecauseOfTime() throws EvaluationException {
+ AccessEvent ae = new AccessEvent(request, response, serverAdapter);
+ response.setStatus(500);
+ //this returns true and sets the time
+ evaluator.evaluate(ae);
+ //this should return false because 24h have not passed yet.
+ assertFalse(evaluator.evaluate(ae));
+ }
+
+
+}
Added: logback/trunk/logback-access/src/test/java/ch/qos/logback/access/net/PackageTest.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-access/src/test/java/ch/qos/logback/access/net/PackageTest.java Tue Nov 14 16:17:29 2006
@@ -0,0 +1,21 @@
+/**
+ * Logback: the generic, reliable, fast and flexible logging framework for Java.
+ *
+ * Copyright (C) 2000-2006, QOS.ch
+ *
+ * This library is free software, you can redistribute it and/or modify it under
+ * the terms of the GNU Lesser General Public License as published by the Free
+ * Software Foundation.
+ */
+package ch.qos.logback.access.net;
+
+import junit.framework.*;
+
+public class PackageTest extends TestCase {
+
+ public static Test suite() {
+ TestSuite suite = new TestSuite();
+ suite.addTestSuite(DefaultSMTPEvaluatorTest.class);
+ return suite;
+ }
+}
\ No newline at end of file
Modified: logback/trunk/logback-access/src/test/java/ch/qos/logback/access/pattern/helpers/DummyResponse.java
==============================================================================
--- logback/trunk/logback-access/src/test/java/ch/qos/logback/access/pattern/helpers/DummyResponse.java (original)
+++ logback/trunk/logback-access/src/test/java/ch/qos/logback/access/pattern/helpers/DummyResponse.java Tue Nov 14 16:17:29 2006
@@ -12,7 +12,9 @@
public class DummyResponse implements HttpServletResponse {
+ int status;
Map<String, String> headerMap;
+
public DummyResponse() {
headerMap = new HashMap<String, String>();
headerMap.put("headerName1", "headerValue1");
@@ -70,9 +72,6 @@
public void setIntHeader(String arg0, int arg1) {
}
- public void setStatus(int arg0) {
- }
-
public void setStatus(int arg0, String arg1) {
}
@@ -131,5 +130,17 @@
public String getHeader(String key) {
return headerMap.get(key);
}
+
+ public long getContentCount() {
+ return 10000L;
+ }
+
+ public int getStatus() {
+ return status;
+ }
+ public void setStatus(int status) {
+ this.status = status;
+ }
+
}
Added: logback/trunk/logback-access/src/test/java/ch/qos/logback/access/pattern/helpers/DummyServerAdapter.java
==============================================================================
--- (empty file)
+++ logback/trunk/logback-access/src/test/java/ch/qos/logback/access/pattern/helpers/DummyServerAdapter.java Tue Nov 14 16:17:29 2006
@@ -0,0 +1,27 @@
+package ch.qos.logback.access.pattern.helpers;
+
+import ch.qos.logback.access.spi.ServerAdapter;
+
+public class DummyServerAdapter implements ServerAdapter {
+
+ DummyRequest request;
+ DummyResponse response;
+
+ public DummyServerAdapter(DummyRequest dummyRequest, DummyResponse dummyResponse) {
+ this.request = dummyRequest;
+ this.response = dummyResponse;
+ }
+
+ public long getContentLength() {
+ return response.getContentCount();
+ }
+
+ public int getStatusCode() {
+ return response.getStatus();
+ }
+
+ public String getResponseHeader(String key) {
+ return response.getHeader(key);
+ }
+
+}
Modified: logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SMTPAppender.java
==============================================================================
--- logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SMTPAppender.java (original)
+++ logback/trunk/logback-classic/src/main/java/ch/qos/logback/classic/net/SMTPAppender.java Tue Nov 14 16:17:29 2006
@@ -10,15 +10,13 @@
package ch.qos.logback.classic.net;
-import java.io.File;
-
-import ch.qos.logback.classic.Level;
import ch.qos.logback.classic.PatternLayout;
-import ch.qos.logback.core.helpers.CyclicBuffer;
+import ch.qos.logback.classic.boolex.JaninoEventEvaluator;
import ch.qos.logback.classic.spi.LoggingEvent;
import ch.qos.logback.core.Layout;
+import ch.qos.logback.core.boolex.EventEvaluator;
+import ch.qos.logback.core.helpers.CyclicBuffer;
import ch.qos.logback.core.net.SMTPAppenderBase;
-import ch.qos.logback.core.rolling.TriggeringPolicy;
/**
* Send an e-mail when a specific logging event occurs, typically on errors or
@@ -38,25 +36,36 @@
public class SMTPAppender extends SMTPAppenderBase {
static final String DEFAULT_SUBJECT_PATTERN = "%logger{20} - %m";
+ static final String DEFAULT_EVALUATOR_EXPRESSION = "level >= ERROR";
private int bufferSize = 512;
protected CyclicBuffer cb = new CyclicBuffer(bufferSize);
/**
* The default constructor will instantiate the appender with a
- * {@link TriggeringPolicy} that will trigger on events with level
+ * {@link EventEvaluator} that will trigger on events with level
* ERROR or higher.
*/
public SMTPAppender() {
- this(new DefaultSMTPTriggeringPolicy());
+ JaninoEventEvaluator jee = new JaninoEventEvaluator();
+ jee.setContext(getContext());
+ jee.setExpression(DEFAULT_EVALUATOR_EXPRESSION);
+ jee.setName("SMTPAppender's default event evaluator");
+ this.eventEvaluator = jee;
}
/**
* Use the parameter as the {@link
- * TriggeringPolicy} for this SMTPAppender.
+ * EventEvaluator} for this SMTPAppender.
*/
- public SMTPAppender(TriggeringPolicy triggeringPolicy) {
- this.triggeringPolicy = triggeringPolicy;
+ public SMTPAppender(EventEvaluator eventEvaluator) {
+ this.eventEvaluator = eventEvaluator;
+ }
+
+ @Override
+ public void start() {
+ ((JaninoEventEvaluator) eventEvaluator).start();
+ super.start();
}
/**
@@ -111,32 +120,3 @@
return pl;
}
}
-
-class DefaultSMTPTriggeringPolicy implements TriggeringPolicy {
-
- private boolean started;
-
- /**
- * Is this <code>event</code> the e-mail triggering event?
- *
- * <p>
- * This method returns <code>true</code>, if the event level has ERROR
- * level or higher. Otherwise it returns <code>false</code>.
- */
- public boolean isTriggeringEvent(File activeFile, Object eventObject) {
- LoggingEvent event = (LoggingEvent) eventObject;
- return event.getLevel().isGreaterOrEqual(Level.ERROR);
- }
-
- public boolean isStarted() {
- return started == true;
- }
-
- public void start() {
- started = true;
- }
-
- public void stop() {
- started = false;
- }
-}
Modified: logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SMTPAppenderTest.java
==============================================================================
--- logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SMTPAppenderTest.java (original)
+++ logback/trunk/logback-classic/src/test/java/ch/qos/logback/classic/net/SMTPAppenderTest.java Tue Nov 14 16:17:29 2006
@@ -75,7 +75,7 @@
}
public void setTriggeringPolicy() {
- appender.setTriggeringPolicy(null);
+ appender.setEventEvaluator(null);
appender.checkEntryConditions();
assertEquals(1, appender.getContext().getStatusManager().getCount());
}
@@ -86,20 +86,6 @@
assertEquals(1, appender.getContext().getStatusManager().getCount());
}
- public void testDefaultEvaluatorNoTrigger() {
- DefaultSMTPTriggeringPolicy evaluator = new DefaultSMTPTriggeringPolicy();
- LoggingEvent le = new LoggingEvent();
- le.setLevel(Level.DEBUG);
- assertFalse(evaluator.isTriggeringEvent(null, le));
- }
-
- public void testDefaultEvaluatorTrigger() {
- DefaultSMTPTriggeringPolicy evaluator = new DefaultSMTPTriggeringPolicy();
- LoggingEvent le = new LoggingEvent();
- le.setLevel(Level.ERROR);
- assertTrue(evaluator.isTriggeringEvent(null, le));
- }
-
private static Layout buildLayout(LoggerContext lc) {
PatternLayout layout = new PatternLayout();
layout.setContext(lc);
Modified: logback/trunk/logback-core/src/main/java/ch/qos/logback/core/net/SMTPAppenderBase.java
==============================================================================
--- logback/trunk/logback-core/src/main/java/ch/qos/logback/core/net/SMTPAppenderBase.java (original)
+++ logback/trunk/logback-core/src/main/java/ch/qos/logback/core/net/SMTPAppenderBase.java Tue Nov 14 16:17:29 2006
@@ -26,7 +26,8 @@
import ch.qos.logback.core.AppenderBase;
import ch.qos.logback.core.Layout;
-import ch.qos.logback.core.rolling.TriggeringPolicy;
+import ch.qos.logback.core.boolex.EvaluationException;
+import ch.qos.logback.core.boolex.EventEvaluator;
/**
* An abstract class that provides basic support for
@@ -49,7 +50,7 @@
protected Message msg;
- protected TriggeringPolicy triggeringPolicy;
+ protected EventEvaluator eventEvaluator;
/**
* return a layout for the subjet string as appropriate for the
@@ -86,6 +87,8 @@
subjectLayout = makeSubjectLayout(subjectStr);
+
+
started = true;
} catch (MessagingException e) {
@@ -105,9 +108,13 @@
subAppend(eventObject);
- if (triggeringPolicy.isTriggeringEvent(null, eventObject)) {
+ try {
+ if (eventEvaluator.evaluate(eventObject)) {
sendBuffer(eventObject);
}
+ } catch (EvaluationException ex) {
+ addError("SMTPAppender's EventEvaluator threw an Exception" + ex);
+ }
}
abstract protected void subAppend(Object eventObject);
@@ -126,8 +133,8 @@
return false;
}
- if (this.triggeringPolicy == null) {
- addError("No TriggeringPolicy is set for appender [" + name + "].");
+ if (this.eventEvaluator == null) {
+ addError("No EventEvaluator is set for appender [" + name + "].");
return false;
}
@@ -272,13 +279,13 @@
}
/**
- * The <b>TriggeringPolicy</b> option takes a string value representing the
- * name of the class implementing the {@link TriggeringPolicy}
+ * The <b>EventEvaluator</b> option takes a string value representing the
+ * name of the class implementing the {@link EventEvaluators}
* interface. A corresponding object will be instantiated and assigned as the
- * triggering event evaluator for the SMTPAppender.
+ * event evaluator for the SMTPAppender.
*/
- public void setTriggeringPolicy(TriggeringPolicy triggeringPolicy) {
- this.triggeringPolicy = triggeringPolicy;
+ public void setEventEvaluator(EventEvaluator eventEvaluator) {
+ this.eventEvaluator = eventEvaluator;
}
public Layout getLayout() {
More information about the logback-dev
mailing list