View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
4    *
5    * This program and the accompanying materials are dual-licensed under
6    * either the terms of the Eclipse Public License v1.0 as published by
7    * the Eclipse Foundation
8    *
9    *   or (per the licensee's choosing)
10   *
11   * under the terms of the GNU Lesser General Public License version 2.1
12   * as published by the Free Software Foundation.
13   */
14  package ch.qos.logback.classic.joran;
15  
16  import static org.junit.Assert.assertFalse;
17  import static org.junit.Assert.assertNotNull;
18  import static org.junit.Assert.assertTrue;
19  
20  import java.util.Map;
21  
22  import org.junit.Test;
23  import org.slf4j.Marker;
24  import org.slf4j.MarkerFactory;
25  
26  import ch.qos.logback.classic.ClassicTestConstants;
27  import ch.qos.logback.classic.Level;
28  import ch.qos.logback.classic.Logger;
29  import ch.qos.logback.classic.LoggerContext;
30  import ch.qos.logback.classic.boolex.JaninoEventEvaluator;
31  import ch.qos.logback.classic.spi.ILoggingEvent;
32  import ch.qos.logback.classic.spi.LoggingEvent;
33  import ch.qos.logback.core.CoreConstants;
34  import ch.qos.logback.core.boolex.EvaluationException;
35  import ch.qos.logback.core.boolex.EventEvaluator;
36  import ch.qos.logback.core.joran.spi.JoranException;
37  
38  public class EvaluatorJoranTest {
39  
40      @Test
41      public void testSimpleEvaluator() throws NullPointerException, EvaluationException, JoranException {
42          JoranConfigurator jc = new JoranConfigurator();
43          LoggerContext loggerContext = new LoggerContext();
44          jc.setContext(loggerContext);
45          jc.doConfigure(ClassicTestConstants.JORAN_INPUT_PREFIX + "simpleEvaluator.xml");
46  
47          @SuppressWarnings("unchecked")
48          Map<String, EventEvaluator<?>> evalMap = (Map<String, EventEvaluator<?>>) loggerContext.getObject(CoreConstants.EVALUATOR_MAP);
49          assertNotNull(evalMap);
50          JaninoEventEvaluator evaluator = (JaninoEventEvaluator) evalMap.get("msgEval");
51          assertNotNull(evaluator);
52  
53          Logger logger = loggerContext.getLogger("xx");
54          ILoggingEvent event0 = new LoggingEvent("foo", logger, Level.DEBUG, "Hello world", null, null);
55          assertTrue(evaluator.evaluate(event0));
56  
57          ILoggingEvent event1 = new LoggingEvent("foo", logger, Level.DEBUG, "random blurb", null, null);
58          assertFalse(evaluator.evaluate(event1));
59      }
60  
61      @Test
62      public void testIgnoreMarker() throws NullPointerException, EvaluationException, JoranException {
63          JoranConfigurator jc = new JoranConfigurator();
64          LoggerContext loggerContext = new LoggerContext();
65          jc.setContext(loggerContext);
66          jc.doConfigure(ClassicTestConstants.JORAN_INPUT_PREFIX + "ignore.xml");
67  
68          @SuppressWarnings("unchecked")
69          Map<String, EventEvaluator<?>> evalMap = (Map<String, EventEvaluator<?>>) loggerContext.getObject(CoreConstants.EVALUATOR_MAP);
70          assertNotNull(evalMap);
71  
72          Logger logger = loggerContext.getLogger("xx");
73  
74          JaninoEventEvaluator evaluator = (JaninoEventEvaluator) evalMap.get("IGNORE_EVAL");
75          LoggingEvent event = new LoggingEvent("foo", logger, Level.DEBUG, "Hello world", null, null);
76  
77          Marker ignoreMarker = MarkerFactory.getMarker("IGNORE");
78          event.setMarker(ignoreMarker);
79          assertTrue(evaluator.evaluate(event));
80  
81          logger.debug("hello", new Exception("test"));
82          logger.debug(ignoreMarker, "hello ignore", new Exception("test"));
83  
84          // logger.debug("hello", new Exception("test"));
85  
86          // StatusPrinter.print(loggerContext.getStatusManager());
87      }
88  
89      @Test
90      public void testMultipleConditionsInExpression() throws NullPointerException, EvaluationException {
91          LoggerContext loggerContext = new LoggerContext();
92          Logger logger = loggerContext.getLogger("xx");
93          JaninoEventEvaluator ee = new JaninoEventEvaluator();
94          ee.setName("testEval");
95          ee.setContext(loggerContext);
96          // &#38;&#38;
97          // &amp;&amp;
98          ee.setExpression("message.contains(\"stacktrace\") && message.contains(\"logging\")");
99          ee.start();
100         // StatusPrinter.print(loggerContext);
101 
102         String message = "stacktrace bla bla logging";
103         ILoggingEvent event = new LoggingEvent(this.getClass().getName(), logger, Level.DEBUG, message, null, null);
104 
105         assertTrue(ee.evaluate(event));
106     }
107 }