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