View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.logging.slf4j;
18  
19  import java.util.ArrayList;
20  import java.util.Collections;
21  import java.util.List;
22  
23  import org.apache.commons.logging.Log;
24  import org.apache.commons.logging.LogFactory;
25  import org.apache.commons.logging.impl.Slf4jLogFactory;
26  import org.slf4j.LoggerFactory;
27  import org.slf4j.Marker;
28  import org.slf4j.MarkerFactory;
29  
30  import ch.qos.logback.classic.Level;
31  import ch.qos.logback.classic.Logger;
32  import ch.qos.logback.classic.LoggerContext;
33  import ch.qos.logback.classic.spi.ILoggingEvent;
34  import ch.qos.logback.classic.spi.ThrowableProxy;
35  import ch.qos.logback.core.filter.Filter;
36  import ch.qos.logback.core.read.ListAppender;
37  import ch.qos.logback.core.spi.FilterReply;
38  import junit.framework.TestCase;
39  
40  public class CallerInformationTestCase extends TestCase {
41  
42      private static final String STRING = "String";
43      private static final Throwable T = new RuntimeException();
44      private static final List<Marker> MARKERS = Collections.singletonList(MarkerFactory.getMarker("COMMONS-LOGGING"));
45  
46      private static final Level[] levels = {Level.ERROR, // SLF4J has no FATAL level
47              Level.ERROR, Level.WARN, Level.INFO, Level.DEBUG, Level.TRACE};
48  
49      private LogFactory factory;
50      private Log log;
51      private ListAppender<ILoggingEvent> appender;
52  
53      @Override
54      public void setUp() {
55          factory = LogFactory.getFactory();
56          log = factory.getInstance(getClass());
57          final LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
58          final Logger logger = context.getLogger(Logger.ROOT_LOGGER_NAME);
59          appender = (ListAppender) logger.getAppender("LIST");
60          appender.clearAllFilters();
61          appender.addFilter(new Filter<ILoggingEvent>() {
62              @Override
63              public FilterReply decide(final ILoggingEvent event) {
64                  // Force the registration of caller data
65                  event.getCallerData();
66                  return FilterReply.NEUTRAL;
67              }
68          });
69      }
70  
71      public void testFactoryClassName() {
72          assertEquals(Slf4jLogFactory.class, factory.getClass());
73      }
74  
75      public void testLocationInfo() {
76          appender.list.clear();
77          // The following value must match the line number
78          final int currentLineNumber = 78;
79          log.fatal(STRING);
80          log.fatal(STRING, T);
81          log.error(STRING);
82          log.error(STRING, T);
83          log.warn(STRING);
84          log.warn(STRING, T);
85          log.info(STRING);
86          log.info(STRING, T);
87          log.debug(STRING);
88          log.debug(STRING, T);
89          log.trace(STRING);
90          log.trace(STRING, T);
91          final List<ILoggingEvent> events = new ArrayList<>(appender.list);
92          assertEquals("All events received.", levels.length * 2, events.size());
93          for (int lev = 0; lev < levels.length; lev++) {
94              for (int hasThrowable = 0; hasThrowable <= 1; hasThrowable++) {
95                  final ILoggingEvent event = events.get(2 * lev + hasThrowable);
96                  assertEquals("Correct message.", STRING, event.getMessage());
97                  assertEquals("Correct marker.", MARKERS, event.getMarkerList());
98                  assertEquals("Level matches.", levels[lev], event.getLevel());
99                  final StackTraceElement[] callerData = event.getCallerData();
100                 assertTrue("Has location", callerData != null && callerData.length > 0);
101                 final StackTraceElement location = callerData[0];
102                 assertEquals("Correct location class.", getClass().getName(), location.getClassName());
103                 assertEquals("Correct location line.",
104                         currentLineNumber + 2 * lev + hasThrowable + 1,
105                         location.getLineNumber());
106                 final ThrowableProxy throwableProxy = (ThrowableProxy) event.getThrowableProxy();
107                 assertEquals("Correct exception",
108                         hasThrowable > 0 ? T : null,
109                         throwableProxy != null ? throwableProxy.getThrowable() : null);
110             }
111         }
112     }
113 }