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 java.util.Map;
17  
18  import org.junit.jupiter.api.Disabled;
19  import org.junit.jupiter.api.Test;
20  import org.slf4j.Marker;
21  import org.slf4j.MarkerFactory;
22  
23  import ch.qos.logback.classic.ClassicTestConstants;
24  import ch.qos.logback.classic.Level;
25  import ch.qos.logback.classic.Logger;
26  import ch.qos.logback.classic.LoggerContext;
27  import ch.qos.logback.classic.boolex.JaninoEventEvaluator;
28  import ch.qos.logback.classic.spi.ILoggingEvent;
29  import ch.qos.logback.classic.spi.LoggingEvent;
30  import ch.qos.logback.core.CoreConstants;
31  import ch.qos.logback.core.boolex.EvaluationException;
32  import ch.qos.logback.core.boolex.EventEvaluator;
33  import ch.qos.logback.core.joran.spi.JoranException;
34  
35  import static org.junit.jupiter.api.Assertions.assertFalse;
36  import static org.junit.jupiter.api.Assertions.assertNotNull;
37  import static org.junit.jupiter.api.Assertions.assertTrue;
38  
39  @Disabled
40  public class EvaluatorJoranTest {
41  
42      @Test
43      public void testSimpleEvaluator() throws NullPointerException, EvaluationException, JoranException {
44          JoranConfigurator jc = new JoranConfigurator();
45          LoggerContext loggerContext = new LoggerContext();
46          jc.setContext(loggerContext);
47          jc.doConfigure(ClassicTestConstants.JORAN_INPUT_PREFIX + "simpleEvaluator.xml");
48  
49          @SuppressWarnings("unchecked")
50          Map<String, EventEvaluator<?>> evalMap = (Map<String, EventEvaluator<?>>) loggerContext
51                  .getObject(CoreConstants.EVALUATOR_MAP);
52          assertNotNull(evalMap);
53          JaninoEventEvaluator evaluator = (JaninoEventEvaluator) evalMap.get("msgEval");
54          assertNotNull(evaluator);
55  
56          Logger logger = loggerContext.getLogger("xx");
57          ILoggingEvent event0 = new LoggingEvent("foo", logger, Level.DEBUG, "Hello world", null, null);
58          assertTrue(evaluator.evaluate(event0));
59  
60          ILoggingEvent event1 = new LoggingEvent("foo", logger, Level.DEBUG, "random blurb", null, null);
61          assertFalse(evaluator.evaluate(event1));
62      }
63  
64      @Disabled // markers are no longer supported in Janino
65      @Test
66      public void testIgnoreMarker() throws NullPointerException, EvaluationException, JoranException {
67          JoranConfigurator jc = new JoranConfigurator();
68          LoggerContext loggerContext = new LoggerContext();
69          jc.setContext(loggerContext);
70  
71          jc.doConfigure(ClassicTestConstants.JORAN_INPUT_PREFIX + "ignore.xml");
72          @SuppressWarnings("unchecked")
73          Map<String, EventEvaluator<?>> evalMap = (Map<String, EventEvaluator<?>>) loggerContext
74                  .getObject(CoreConstants.EVALUATOR_MAP);
75          assertNotNull(evalMap);
76  
77          Logger logger = loggerContext.getLogger("xx");
78  
79          JaninoEventEvaluator evaluator = (JaninoEventEvaluator) evalMap.get("IGNORE_EVAL");
80          LoggingEvent event = new LoggingEvent("foo", logger, Level.DEBUG, "Hello world", null, null);
81  
82          Marker ignoreMarker = MarkerFactory.getMarker("IGNORE");
83          event.addMarker(ignoreMarker);
84          assertTrue(evaluator.evaluate(event));
85  
86          logger.debug("hello", new Exception("test"));
87          logger.debug(ignoreMarker, "hello ignore", new Exception("test"));
88  
89          // logger.debug("hello", new Exception("test"));
90  
91          // StatusPrinter.print(loggerContext.getStatusManager());
92      }
93  
94      @Test
95      public void testMultipleConditionsInExpression() throws NullPointerException, EvaluationException {
96          LoggerContext loggerContext = new LoggerContext();
97          Logger logger = loggerContext.getLogger("xx");
98          JaninoEventEvaluator ee = new JaninoEventEvaluator();
99          ee.setName("testEval");
100         ee.setContext(loggerContext);
101         // &#38;&#38;
102         // &amp;&amp;
103         ee.setExpression("message.contains(\"stacktrace\") && message.contains(\"logging\")");
104         ee.start();
105         // StatusPrinter.print(loggerContext);
106 
107         String message = "stacktrace bla bla logging";
108         ILoggingEvent event = new LoggingEvent(this.getClass().getName(), logger, Level.DEBUG, message, null, null);
109 
110         assertTrue(ee.evaluate(event));
111     }
112 }