View Javadoc
1   /**
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2022, 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 ch.qos.logback.classic;
15  
16  
17  import java.util.ArrayList;
18  import java.util.List;
19  
20  import org.junit.jupiter.api.BeforeEach;
21  
22  import ch.qos.logback.classic.pattern.ConverterTest;
23  import ch.qos.logback.classic.pattern.LineOfCallerConverter;
24  import ch.qos.logback.classic.spi.ILoggingEvent;
25  import ch.qos.logback.core.AppenderBase;
26  import ch.qos.logback.core.pattern.DynamicConverter;
27  import org.junit.jupiter.api.Test;
28  
29  import static org.junit.jupiter.api.Assertions.assertEquals;
30  
31  public class FluentAPILocationExtractionTest {
32      static public class WithLocationInfoListAppender extends AppenderBase<ILoggingEvent> {
33  
34          DynamicConverter<ILoggingEvent> converter = new LineOfCallerConverter();
35          public List<String> list = new ArrayList<>();
36  
37          protected void append(ILoggingEvent e) {
38              String val = converter.convert(e);
39              list.add(val);
40          }
41      }
42  
43      LoggerContext lc = new LoggerContext();
44      Logger logger = lc.getLogger(ConverterTest.class);
45      WithLocationInfoListAppender wlila = new WithLocationInfoListAppender();
46  
47      @BeforeEach
48      public void setUp() {
49          wlila.setContext(lc);
50          wlila.start();
51  
52          logger.addAppender(wlila);
53      }
54  
55      @Test
56      public void smoke() {
57          logger.addAppender(wlila);
58          // line number to retain is the next line's number
59          logger.atInfo().log("smoke");
60          
61          assertEquals(1, wlila.list.size());
62          String result = wlila.list.get(0);
63          assertEquals("59", result);
64      }
65  
66  }