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 org.dummy;
15  
16  import ch.qos.logback.classic.Level;
17  import ch.qos.logback.classic.LoggerContext;
18  import ch.qos.logback.classic.PatternLayout;
19  import ch.qos.logback.classic.spi.ILoggingEvent;
20  import org.apache.log4j.Logger;
21  import org.junit.jupiter.api.BeforeEach;
22  import org.junit.jupiter.api.Test;
23  import org.slf4j.LoggerFactory;
24  
25  import static org.junit.jupiter.api.Assertions.assertEquals;
26  
27  /**
28   * Used to test log4j-over-slf4j
29   * 
30   * @author Ceki Gulcu
31   *
32   */
33  public class Log4jInvocation {
34  
35      static final String HELLO = "Hello";
36  
37      DummyLBAppender listAppender;
38      LoggerContext lc;
39      ch.qos.logback.classic.Logger rootLogger;
40  
41      @BeforeEach
42      public void fixture() {
43          lc = (LoggerContext) LoggerFactory.getILoggerFactory();
44          lc.reset();
45  
46          listAppender = new DummyLBAppender();
47          listAppender.setContext(lc);
48          listAppender.start();
49          rootLogger = lc.getLogger("root");
50          rootLogger.addAppender(listAppender);
51      }
52  
53      @Test
54      public void basic() {
55          assertEquals(0, listAppender.list.size());
56  
57          Logger logger = Logger.getLogger("basic-test");
58          logger.debug(HELLO);
59  
60          assertEquals(1, listAppender.list.size());
61          ILoggingEvent event = (ILoggingEvent) listAppender.list.get(0);
62          assertEquals(HELLO, event.getMessage());
63      }
64  
65      @Test
66      public void callerData() {
67          assertEquals(0, listAppender.list.size());
68  
69          PatternLayout pl = new PatternLayout();
70          pl.setPattern("%-5level [%class] %logger - %msg");
71          pl.setContext(lc);
72          pl.start();
73          listAppender.layout = pl;
74  
75          Logger logger = Logger.getLogger("basic-test");
76          logger.trace("none");
77          assertEquals(0, listAppender.list.size());
78  
79          rootLogger.setLevel(Level.TRACE);
80          logger.trace(HELLO);
81          assertEquals(1, listAppender.list.size());
82  
83          ILoggingEvent event = (ILoggingEvent) listAppender.list.get(0);
84          assertEquals(HELLO, event.getMessage());
85  
86          assertEquals(1, listAppender.stringList.size());
87          assertEquals("TRACE [" + Log4jInvocation.class.getName() + "] basic-test - Hello",
88                  listAppender.stringList.get(0));
89      }
90  }