1
2
3
4
5
6
7
8
9
10
11
12
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
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
90
91
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
102
103 ee.setExpression("message.contains(\"stacktrace\") && message.contains(\"logging\")");
104 ee.start();
105
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 }