001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.classic.joran;
015
016import static org.junit.Assert.assertFalse;
017import static org.junit.Assert.assertNotNull;
018import static org.junit.Assert.assertTrue;
019
020import java.util.Map;
021
022import org.junit.Test;
023import org.slf4j.Marker;
024import org.slf4j.MarkerFactory;
025
026import ch.qos.logback.classic.ClassicTestConstants;
027import ch.qos.logback.classic.Level;
028import ch.qos.logback.classic.Logger;
029import ch.qos.logback.classic.LoggerContext;
030import ch.qos.logback.classic.boolex.JaninoEventEvaluator;
031import ch.qos.logback.classic.spi.ILoggingEvent;
032import ch.qos.logback.classic.spi.LoggingEvent;
033import ch.qos.logback.core.CoreConstants;
034import ch.qos.logback.core.boolex.EvaluationException;
035import ch.qos.logback.core.boolex.EventEvaluator;
036import ch.qos.logback.core.joran.spi.JoranException;
037
038public class EvaluatorJoranTest {
039
040    @Test
041    public void testSimpleEvaluator() throws NullPointerException, EvaluationException, JoranException {
042        JoranConfigurator jc = new JoranConfigurator();
043        LoggerContext loggerContext = new LoggerContext();
044        jc.setContext(loggerContext);
045        jc.doConfigure(ClassicTestConstants.JORAN_INPUT_PREFIX + "simpleEvaluator.xml");
046
047        @SuppressWarnings("unchecked")
048        Map<String, EventEvaluator<?>> evalMap = (Map<String, EventEvaluator<?>>) loggerContext.getObject(CoreConstants.EVALUATOR_MAP);
049        assertNotNull(evalMap);
050        JaninoEventEvaluator evaluator = (JaninoEventEvaluator) evalMap.get("msgEval");
051        assertNotNull(evaluator);
052
053        Logger logger = loggerContext.getLogger("xx");
054        ILoggingEvent event0 = new LoggingEvent("foo", logger, Level.DEBUG, "Hello world", null, null);
055        assertTrue(evaluator.evaluate(event0));
056
057        ILoggingEvent event1 = new LoggingEvent("foo", logger, Level.DEBUG, "random blurb", null, null);
058        assertFalse(evaluator.evaluate(event1));
059    }
060
061    @Test
062    public void testIgnoreMarker() throws NullPointerException, EvaluationException, JoranException {
063        JoranConfigurator jc = new JoranConfigurator();
064        LoggerContext loggerContext = new LoggerContext();
065        jc.setContext(loggerContext);
066        jc.doConfigure(ClassicTestConstants.JORAN_INPUT_PREFIX + "ignore.xml");
067
068        @SuppressWarnings("unchecked")
069        Map<String, EventEvaluator<?>> evalMap = (Map<String, EventEvaluator<?>>) loggerContext.getObject(CoreConstants.EVALUATOR_MAP);
070        assertNotNull(evalMap);
071
072        Logger logger = loggerContext.getLogger("xx");
073
074        JaninoEventEvaluator evaluator = (JaninoEventEvaluator) evalMap.get("IGNORE_EVAL");
075        LoggingEvent event = new LoggingEvent("foo", logger, Level.DEBUG, "Hello world", null, null);
076
077        Marker ignoreMarker = MarkerFactory.getMarker("IGNORE");
078        event.setMarker(ignoreMarker);
079        assertTrue(evaluator.evaluate(event));
080
081        logger.debug("hello", new Exception("test"));
082        logger.debug(ignoreMarker, "hello ignore", new Exception("test"));
083
084        // logger.debug("hello", new Exception("test"));
085
086        // StatusPrinter.print(loggerContext.getStatusManager());
087    }
088
089    @Test
090    public void testMultipleConditionsInExpression() throws NullPointerException, EvaluationException {
091        LoggerContext loggerContext = new LoggerContext();
092        Logger logger = loggerContext.getLogger("xx");
093        JaninoEventEvaluator ee = new JaninoEventEvaluator();
094        ee.setName("testEval");
095        ee.setContext(loggerContext);
096        // &#38;&#38;
097        // &amp;&amp;
098        ee.setExpression("message.contains(\"stacktrace\") && message.contains(\"logging\")");
099        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}