[logback-dev] [GIT] Logback: the generic, reliable, fast and flexible logging framework. branch, master, updated. v_0.9.27-23-g06a5b69

added by portage for gitosis-gentoo git-noreply at pixie.qos.ch
Tue Jan 25 23:04:56 CET 2011


This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "Logback: the generic, reliable, fast and flexible logging framework.".

The branch, master has been updated
       via  06a5b692f14560636bd92d7bd7cf1f85830f4e55 (commit)
      from  b3b234ddef5a1ba6863cd922f4f410cde65c1a0d (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------
http://git.qos.ch/gitweb/?p=logback.git;a=commit;h=06a5b692f14560636bd92d7bd7cf1f85830f4e55
http://github.com/ceki/logback/commit/06a5b692f14560636bd92d7bd7cf1f85830f4e55

commit 06a5b692f14560636bd92d7bd7cf1f85830f4e55
Author: Ceki Gulcu <ceki at qos.ch>
Date:   Tue Jan 25 23:01:46 2011 +0100

    adding support for Java blocks in JaninoEventEvaluator

diff --git a/logback-access/src/main/java/ch/qos/logback/access/boolex/JaninoEventEvaluator.java b/logback-access/src/main/java/ch/qos/logback/access/boolex/JaninoEventEvaluator.java
index 0df2765..7702755 100644
--- a/logback-access/src/main/java/ch/qos/logback/access/boolex/JaninoEventEvaluator.java
+++ b/logback-access/src/main/java/ch/qos/logback/access/boolex/JaninoEventEvaluator.java
@@ -25,37 +25,43 @@ public class JaninoEventEvaluator extends JaninoEventEvaluatorBase<AccessEvent>
 
   public final static List<String> DEFAULT_PARAM_NAME_LIST = new ArrayList<String>();
   public final static List<Class> DEFAULT_PARAM_TYPE_LIST = new ArrayList<Class>();
-  
+
   static {
     DEFAULT_PARAM_NAME_LIST.add("event");
     DEFAULT_PARAM_TYPE_LIST.add(AccessEvent.class);
   }
-  
-  
+
+
   public JaninoEventEvaluator() {
-    
+
   }
-  
+
   protected String getDecoratedExpression() {
-    return getExpression();
+    String expression = getExpression();
+    if (!expression.contains("return")) {
+      expression = "return " + expression + ";";
+      addInfo("Adding [return] prefix and a semicolon suffix. Expression becomes [" + expression + "]");
+      addInfo("See also " + CoreConstants.CODES_URL + "#block");
+    }
+    return expression;
   }
 
   protected String[] getParameterNames() {
     List<String> fullNameList = new ArrayList<String>();
     fullNameList.addAll(DEFAULT_PARAM_NAME_LIST);
 
-    for(int i = 0; i < matcherList.size(); i++) {
+    for (int i = 0; i < matcherList.size(); i++) {
       Matcher m = (Matcher) matcherList.get(i);
       fullNameList.add(m.getName());
     }
-    
+
     return (String[]) fullNameList.toArray(CoreConstants.EMPTY_STRING_ARRAY);
   }
 
   protected Class[] getParameterTypes() {
     List<Class> fullTypeList = new ArrayList<Class>();
     fullTypeList.addAll(DEFAULT_PARAM_TYPE_LIST);
-    for(int i = 0; i < matcherList.size(); i++) {
+    for (int i = 0; i < matcherList.size(); i++) {
       fullTypeList.add(Matcher.class);
     }
     return (Class[]) fullTypeList.toArray(CoreConstants.EMPTY_CLASS_ARRAY);
@@ -64,16 +70,16 @@ public class JaninoEventEvaluator extends JaninoEventEvaluatorBase<AccessEvent>
   protected Object[] getParameterValues(AccessEvent event) {
     AccessEvent accessEvent = (AccessEvent) event;
     final int matcherListSize = matcherList.size();
-    
+
     int i = 0;
-    Object[] values = new Object[DEFAULT_PARAM_NAME_LIST.size()+matcherListSize];
+    Object[] values = new Object[DEFAULT_PARAM_NAME_LIST.size() + matcherListSize];
 
     values[i++] = accessEvent;
-    
-    for(int j = 0; j < matcherListSize; j++) {
+
+    for (int j = 0; j < matcherListSize; j++) {
       values[i++] = (Matcher) matcherList.get(j);
     }
-    
+
     return values;
   }
 
diff --git a/logback-access/src/test/java/ch/qos/logback/access/AllAccessTest.java b/logback-access/src/test/java/ch/qos/logback/access/AllAccessTest.java
index ee455f3..47aada2 100644
--- a/logback-access/src/test/java/ch/qos/logback/access/AllAccessTest.java
+++ b/logback-access/src/test/java/ch/qos/logback/access/AllAccessTest.java
@@ -19,6 +19,7 @@ import org.junit.runners.Suite.SuiteClasses;
 
 @RunWith(Suite.class)
 @SuiteClasses( { ch.qos.logback.access.spi.PackageTest.class,
+    ch.qos.logback.access.boolex.PackageTest.class,
     ch.qos.logback.access.net.PackageTest.class,
     ch.qos.logback.access.pattern.PackageTest.class,
     ch.qos.logback.access.jetty.PackageTest.class,
diff --git a/logback-access/src/test/java/ch/qos/logback/access/boolex/JaninoEventEvaluatorTest.java b/logback-access/src/test/java/ch/qos/logback/access/boolex/JaninoEventEvaluatorTest.java
new file mode 100644
index 0000000..f16caac
--- /dev/null
+++ b/logback-access/src/test/java/ch/qos/logback/access/boolex/JaninoEventEvaluatorTest.java
@@ -0,0 +1,80 @@
+/**
+ * Logback: the reliable, generic, fast and flexible logging framework.
+ * Copyright (C) 1999-2010, QOS.ch. All rights reserved.
+ *
+ * This program and the accompanying materials are dual-licensed under either
+ * the terms of the Eclipse Public License v1.0 as published by the Eclipse
+ * Foundation
+ *
+ * or (per the licensee's choosing)
+ *
+ * under the terms of the GNU Lesser General Public License version 2.1 as
+ * published by the Free Software Foundation.
+ */
+package ch.qos.logback.access.boolex;
+
+
+import ch.qos.logback.access.dummy.DummyRequest;
+import ch.qos.logback.access.dummy.DummyResponse;
+import ch.qos.logback.access.dummy.DummyServerAdapter;
+import ch.qos.logback.access.net.URLEvaluator;
+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;
+import ch.qos.logback.core.util.StatusPrinter;
+import org.junit.Before;
+import org.junit.Test;
+
+import static org.junit.Assert.assertTrue;
+import static org.junit.Assert.fail;
+
+public class JaninoEventEvaluatorTest {
+
+  final String expectedURL1 = "testUrl1";
+  final String expectedURL2 = "testUrl2";
+  Context context = new ContextBase();
+  JaninoEventEvaluator evaluator;
+  DummyRequest request;
+  DummyResponse response;
+  DummyServerAdapter serverAdapter;
+
+  @Before
+  public void setUp() throws Exception {
+    evaluator = new JaninoEventEvaluator();
+    evaluator.setContext(context);
+    request = new DummyRequest();
+    response = new DummyResponse();
+    serverAdapter = new DummyServerAdapter(request, response);
+  }
+
+
+  @Test
+  public void smoke() throws EvaluationException {
+    evaluator.setExpression("event.getProtocol().equals(\"testProtocol\")");
+    evaluator.start();
+    AccessEvent ae = new AccessEvent(request, response, serverAdapter);
+    assertTrue( evaluator.evaluate(ae));
+  }
+
+  @Test
+  public void block() throws EvaluationException {
+    evaluator.setExpression("String protocol = event.getProtocol();" +
+            "return protocol.equals(\"testProtocol\");");
+    evaluator.start();
+    AccessEvent ae = new AccessEvent(request, response, serverAdapter);
+    assertTrue(evaluator.evaluate(ae));
+  }
+
+  @Test
+  public void invalidExpression() throws EvaluationException {
+    evaluator.setExpression("return true");
+    evaluator.start();
+    AccessEvent ae = new AccessEvent(request, response, serverAdapter);
+   try {
+     evaluator.evaluate(ae);
+     fail("Was expecting an exception");
+   } catch (IllegalStateException e) {
+   }
+  }
+}
diff --git a/logback-access/src/test/java/ch/qos/logback/access/AllAccessTest.java b/logback-access/src/test/java/ch/qos/logback/access/boolex/PackageTest.java
similarity index 58%
copy from logback-access/src/test/java/ch/qos/logback/access/AllAccessTest.java
copy to logback-access/src/test/java/ch/qos/logback/access/boolex/PackageTest.java
index ee455f3..e5261b4 100644
--- a/logback-access/src/test/java/ch/qos/logback/access/AllAccessTest.java
+++ b/logback-access/src/test/java/ch/qos/logback/access/boolex/PackageTest.java
@@ -11,20 +11,14 @@
  * under the terms of the GNU Lesser General Public License version 2.1
  * as published by the Free Software Foundation.
  */
-package ch.qos.logback.access;
+package ch.qos.logback.access.boolex;
 
+import junit.framework.TestCase;
 import org.junit.runner.RunWith;
 import org.junit.runners.Suite;
 import org.junit.runners.Suite.SuiteClasses;
 
 @RunWith(Suite.class)
- at SuiteClasses( { ch.qos.logback.access.spi.PackageTest.class,
-    ch.qos.logback.access.net.PackageTest.class,
-    ch.qos.logback.access.pattern.PackageTest.class,
-    ch.qos.logback.access.jetty.PackageTest.class,
-    ch.qos.logback.access.filter.PackageTest.class,
-    ch.qos.logback.access.servlet.PackageTest.class,
-    ch.qos.logback.access.sift.PackageTest.class })
-public class AllAccessTest {
-
-}
+ at SuiteClasses({JaninoEventEvaluatorTest.class})
+public class PackageTest extends TestCase {
+}
\ No newline at end of file
diff --git a/logback-classic/src/main/java/ch/qos/logback/classic/boolex/JaninoEventEvaluator.java b/logback-classic/src/main/java/ch/qos/logback/classic/boolex/JaninoEventEvaluator.java
index 4d9d374..49838f2 100644
--- a/logback-classic/src/main/java/ch/qos/logback/classic/boolex/JaninoEventEvaluator.java
+++ b/logback-classic/src/main/java/ch/qos/logback/classic/boolex/JaninoEventEvaluator.java
@@ -78,7 +78,14 @@ public class JaninoEventEvaluator extends
   }
 
   protected String getDecoratedExpression() {
-    return IMPORT_LEVEL + getExpression();
+    String expression = getExpression();
+    if(!expression.contains("return")) {
+      expression = "return "+expression +";";
+      addInfo("Adding [return] prefix and a semicolon suffix. Expression becomes ["+expression+"]");
+      addInfo("See also "+CoreConstants.CODES_URL+"#block");
+
+    }
+    return IMPORT_LEVEL + expression;
   }
 
   protected String[] getParameterNames() {
diff --git a/logback-classic/src/test/java/ch/qos/logback/classic/boolex/JaninoEventEvaluatorTest.java b/logback-classic/src/test/java/ch/qos/logback/classic/boolex/JaninoEventEvaluatorTest.java
index 7430f46..e6a7fab 100644
--- a/logback-classic/src/test/java/ch/qos/logback/classic/boolex/JaninoEventEvaluatorTest.java
+++ b/logback-classic/src/test/java/ch/qos/logback/classic/boolex/JaninoEventEvaluatorTest.java
@@ -70,6 +70,7 @@ public class JaninoEventEvaluatorTest {
     jee.setExpression("message.equals(\"Some message\")");
     jee.start();
 
+    StatusPrinter.print(loggerContext);
     ILoggingEvent event = makeLoggingEvent(null);
     assertTrue(jee.evaluate(event));
   }
diff --git a/logback-core/src/main/java/ch/qos/logback/core/boolex/JaninoEventEvaluatorBase.java b/logback-core/src/main/java/ch/qos/logback/core/boolex/JaninoEventEvaluatorBase.java
index 6e0ebd8..7389295 100644
--- a/logback-core/src/main/java/ch/qos/logback/core/boolex/JaninoEventEvaluatorBase.java
+++ b/logback-core/src/main/java/ch/qos/logback/core/boolex/JaninoEventEvaluatorBase.java
@@ -17,6 +17,7 @@ import java.util.ArrayList;
 import java.util.List;
 
 import org.codehaus.janino.ExpressionEvaluator;
+import org.codehaus.janino.ScriptEvaluator;
 
 /**
  * Abstract class which sets the groundwork for janino based evaluations.
@@ -37,7 +38,7 @@ abstract public class JaninoEventEvaluatorBase<E> extends EventEvaluatorBase<E>
 
   private String expression;
 
-  ExpressionEvaluator ee;
+  ScriptEvaluator scriptEvaluator;
   private int errorCount = 0;
 
   abstract protected String getDecoratedExpression();
@@ -54,9 +55,8 @@ abstract public class JaninoEventEvaluatorBase<E> extends EventEvaluatorBase<E>
   public void start() {
     try {
       assert context != null;
-      ClassLoader cl = context.getClass().getClassLoader();
-      ee = new ExpressionEvaluator(getDecoratedExpression(), EXPRESSION_TYPE,
-          getParameterNames(), getParameterTypes(), THROWN_EXCEPTIONS, cl);
+      scriptEvaluator = new ScriptEvaluator(getDecoratedExpression(), EXPRESSION_TYPE,
+          getParameterNames(), getParameterTypes(), THROWN_EXCEPTIONS);
       super.start();
     } catch (Exception e) {
       addError(
@@ -70,7 +70,7 @@ abstract public class JaninoEventEvaluatorBase<E> extends EventEvaluatorBase<E>
           + "] was called in stopped state");
     }
     try {
-      Boolean result = (Boolean) ee.evaluate(getParameterValues(event));
+      Boolean result = (Boolean) scriptEvaluator.evaluate(getParameterValues(event));
       return result.booleanValue();
     } catch (Exception ex) {
       errorCount++;
diff --git a/logback-site/src/site/pages/codes.html b/logback-site/src/site/pages/codes.html
index e80c770..7bf4ac2 100644
--- a/logback-site/src/site/pages/codes.html
+++ b/logback-site/src/site/pages/codes.html
@@ -468,6 +468,53 @@
   </p>
   
   <hr/>
+
+  <!-- ============================================================= -->
+  <p><a name="block" href="#block">As of logback version 0.9.28,
+  JaninoEventEvaluator expects Java blocks.</a>
+  </p>
+
+  <p>As of logback version 0.9.28, JaninoEvaluator expects Java
+  "block", i.e. the body of a method. In previous versions only
+  boolean expressions were allowed. For backward compatibility
+  reasons, whenever logback sees a boolean expression it will attempt
+  to convert it to a block by prefixing the expression with "return"
+  and suffixing it with a semicolon.
+  </p>
+
+  <p>Boolean expressions can quickly become hairy. For example, the
+  following boolean expression is rather difficult to grok.
+</p>
+
+  <pre class="prettyprint source"> !logger.startsWith("org.apache.http")
+  ||
+  ( logger.equals("org.apache.http.wire")  &amp;&amp;
+       (mdc != null &amp;&amp; mdc.get("entity") != null
+         &amp;&amp;
+       ((String) mdc.get("entity")).contains("someSpecialValue"))
+       &amp;&amp;
+     !message.contains("someSecret")
+  )</pre>
+
+  <p>whereas as its Java block equivalent is considerably easier to
+  follow.</p>
+
+<pre class="prettyprint source">if(logger.startsWith("org.apache.http"))
+  return true;
+
+if(mdc == null || mdc.get("entity") == null)
+  return false;
+
+String payee = (String) mdc.get("entity");
+
+if(logger.equals("org.apache.http.wire") &amp;&amp;
+  payee.contains("someSpecialValue") &amp;&amp;
+  !message.contains("someSecret")) {
+  return true;
+}
+
+return false;</pre>
+
   
   <script src="templates/footer.js" type="text/javascript"></script>
   </div>
diff --git a/logback-site/src/site/pages/manual/configuration.html b/logback-site/src/site/pages/manual/configuration.html
index 372b410..17fc59e 100644
--- a/logback-site/src/site/pages/manual/configuration.html
+++ b/logback-site/src/site/pages/manual/configuration.html
@@ -1525,7 +1525,8 @@ fileName=myApp.log
   configuration files with the help of <code>&lt;if></code>,
   <code>&lt;then></code> and <code>&lt;else></code> elements so that a
   single configuration file can adequately target several
-  environments.
+  environments. Note that conditional processing requires the <a
+  href="../setup.html#janino">Janino library</a>.
   </p>
 
   <p>The general format for conditional statements is shown below.</p>
diff --git a/logback-site/src/site/pages/manual/filters.html b/logback-site/src/site/pages/manual/filters.html
index f354b8c..0d9e9dd 100644
--- a/logback-site/src/site/pages/manual/filters.html
+++ b/logback-site/src/site/pages/manual/filters.html
@@ -366,8 +366,8 @@ public class SampleFilter extends Filter&gt;ILoggingEvent> {
     <p>Logback-classic ships with another concrete
     <code>EventEvaluator</code> implementation called <a
     href="../xref/ch/qos/logback/classic/boolex/JaninoEventEvaluator.html">JaninoEventEvaluator</a>
-    taking arbitrary Java language boolean expressions as the
-    evaluation criteria. We refer to such Java language boolean
+    taking an arbitrary Java language block returning a boolean value
+    as the evaluation criteria. We refer to such Java language boolean
     expressions as "<em>evaluation expressions</em>". Evaluation
     expressions enable great flexibility in event
     filtering. <code>JaninoEventEvaluator</code> requires the <a
@@ -534,7 +534,7 @@ public class SampleFilter extends Filter&gt;ILoggingEvent> {
   &lt;appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
     <b>&lt;filter class="ch.qos.logback.core.filter.EvaluatorFilter">      
       &lt;evaluator> &lt;!-- defaults to type ch.qos.logback.classic.boolex.JaninoEventEvaluator -->
-        &lt;expression><span class="green">message.contains("billing")</span>&lt;/expression>
+        &lt;expression><span class="green">return message.contains("billing");</span>&lt;/expression>
       &lt;/evaluator>
       &lt;OnMismatch>NEUTRAL&lt;/OnMismatch>
       &lt;OnMatch>DENY&lt;/OnMatch>
@@ -565,7 +565,7 @@ public class SampleFilter extends Filter&gt;ILoggingEvent> {
 
     <p>The <em>expression</em> element corresponds to the evaluation
     expression just discussed. The expression
-    <code>message.contains("billing")</code> returns a boolean
+    <code>return message.contains("billing");</code> returns a boolean
     value. Notice that the <em>message</em> variable is exported
     automatically by <code>JaninoEventEvaluator</code>.
     </p>
@@ -621,6 +621,31 @@ java chapters.filters.FilterEvents src/main/java/chapters/filters/basicConfigura
 0    [main] INFO  chapters.filters.FilterEvents - logging statement 8
 0    [main] INFO  chapters.filters.FilterEvents - logging statement 9</p>
 		
+
+    <p>Evaluation expressions can be Java blocks. For example, the
+    following is a valid expression.</p>
+
+    <pre class="prettyprint source">&lt;evaluator>
+  &lt;expression>
+    if(logger.startsWith("org.apache.http"))
+      return true;
+
+    if(mdc == null || mdc.get("entity") == null)
+      return false;
+
+    String payee = (String) mdc.get("entity");
+
+    if(logger.equals("org.apache.http.wire") &amp;amp;&amp;amp; &lt;!-- &amp; encoded as &amp;amp; -->
+        payee.contains("someSpecialValue") &amp;amp;&amp;amp;
+        !message.contains("someSecret")) {
+      return true;
+    }
+
+    return false;
+  &lt;/expression>
+&lt;/evaluator></pre>
+
+
  	  <h2><a name="matcher" href="#matcher">Matchers</a></h2>
 
     <p>While it is possible to do pattern matching by invoking the <a
@@ -1085,16 +1110,16 @@ logger.debug("Hello {}.", name1);</pre>
     can implement your own event evaluation logic by sub-classing
     <code>EventEvaluator</code>. Logback-access ships with a concrete
     implementation named <a
-    href="../xref/ch/qos/logback/access/boolex/JaninoEventEvaluator.html">JaninoEventEvaluator</a>. 
-    It takes  arbitrary Java language boolean expressions as the
-    evaluation criteria. We refer to such Java language boolean
-    expressions as "<em>evaluation expressions</em>". Evaluation
-    expressions enable great flexibility in event
+    href="../xref/ch/qos/logback/access/boolex/JaninoEventEvaluator.html">JaninoEventEvaluator</a>.
+    It takes arbitrary Java language boolean expressions as the
+    evaluation criteria. We refer to such Java language blocks as
+    "<em>evaluation expresisons</em>". Evaluation expressions enable
+    great flexibility in event
     filtering. <code>JaninoEventEvaluator</code> requires the <a
     href="http://docs.codehaus.org/display/JANINO/Home">Janino
     library</a>. Please see the <a
     href="../setup.html#janino">corresponding section</a> of the setup
-    document.  
+    document.
     </p>
 
     <p>Evaluation expressions are compiled on-the-fly during the
diff --git a/logback-site/src/site/pages/news.html b/logback-site/src/site/pages/news.html
index af19296..c39fb87 100644
--- a/logback-site/src/site/pages/news.html
+++ b/logback-site/src/site/pages/news.html
@@ -28,7 +28,7 @@
 
     <hr width="80%" align="center" />
 
-    <h3>January xx, 2011 - Release of version 0.9.28</h3>
+    <h3>January 25th, 2011 - Release of version 0.9.28</h3>
 
     <div style="border: 1px solid #F44; background-color: #FED; padding-left: 1ex; padding-right: 1ex;">
   
@@ -50,6 +50,14 @@
     start method now correctly sets the state of the instance.
     </p>
 
+    <p><code>JaninoEventEvaluator</code> now expects Java blocks
+    instead of boolean expressions. For even moderately complex
+    logical statements, blocks can be more convenient to read or to
+    write compared with boolean expressions. To ensure backward
+    compatibility, existing boolean expressions are converted into
+    blocks on the fly.
+    </p>
+
     <p>Corrected handling of null valued arguments by DBAppender. This
     fixes <a
     href="http://jira.qos.ch/browse/LBCLASSIC-239">LBCLASSIC-239</a>

-----------------------------------------------------------------------

Summary of changes:
 .../access/boolex/JaninoEventEvaluator.java        |   34 +++++----
 .../java/ch/qos/logback/access/AllAccessTest.java  |    1 +
 .../JaninoEventEvaluatorTest.java}                 |   82 ++++++++++----------
 .../access/{filter => boolex}/PackageTest.java     |    5 +-
 .../classic/boolex/JaninoEventEvaluator.java       |    9 ++-
 .../classic/boolex/JaninoEventEvaluatorTest.java   |    1 +
 .../core/boolex/JaninoEventEvaluatorBase.java      |   10 +-
 logback-site/src/site/pages/codes.html             |   47 +++++++++++
 .../src/site/pages/manual/configuration.html       |    3 +-
 logback-site/src/site/pages/manual/filters.html    |   45 ++++++++---
 logback-site/src/site/pages/news.html              |   10 ++-
 11 files changed, 170 insertions(+), 77 deletions(-)
 copy logback-access/src/test/java/ch/qos/logback/access/{net/URLEvaluatorTest.java => boolex/JaninoEventEvaluatorTest.java} (51%)
 copy logback-access/src/test/java/ch/qos/logback/access/{filter => boolex}/PackageTest.java (88%)


hooks/post-receive
-- 
Logback: the generic, reliable, fast and flexible logging framework.


More information about the logback-dev mailing list