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 ch.qos.logback.classic;
15  
16  
17  import org.junit.jupiter.api.BeforeEach;
18  import org.junit.jupiter.api.Test;
19  
20  import ch.qos.logback.classic.spi.ILoggingEvent;
21  import ch.qos.logback.core.read.ListAppender;
22  
23  import static org.junit.jupiter.api.Assertions.assertEquals;
24  
25  public class LoggerMessageFormattingTest {
26  
27      LoggerContext lc;
28      ListAppender<ILoggingEvent> listAppender;
29  
30      @BeforeEach
31      public void setUp() {
32          lc = new LoggerContext();
33          Logger logger = lc.getLogger(Logger.ROOT_LOGGER_NAME);
34          listAppender = new ListAppender<ILoggingEvent>();
35          listAppender.setContext(lc);
36          listAppender.start();
37          logger.addAppender(listAppender);
38      }
39  
40      @Test
41      public void testFormattingOneArg() {
42          Logger logger = lc.getLogger(Logger.ROOT_LOGGER_NAME);
43          logger.debug("{}", Integer.valueOf(12));
44          ILoggingEvent event = (ILoggingEvent) listAppender.list.get(0);
45          assertEquals("12", event.getFormattedMessage());
46      }
47  
48      @Test
49      public void testFormattingTwoArg() {
50          Logger logger = lc.getLogger(Logger.ROOT_LOGGER_NAME);
51          logger.debug("{}-{}", Integer.valueOf(12), Integer.valueOf(13));
52          ILoggingEvent event = (ILoggingEvent) listAppender.list.get(0);
53          assertEquals("12-13", event.getFormattedMessage());
54      }
55  
56      @Test
57      public void testNoFormatting() {
58          Logger logger = lc.getLogger(Logger.ROOT_LOGGER_NAME);
59          logger.debug("test", Integer.valueOf(12), Integer.valueOf(13));
60          ILoggingEvent event = (ILoggingEvent) listAppender.list.get(0);
61          assertEquals("test", event.getFormattedMessage());
62      }
63  
64      @Test
65      public void testNoFormatting2() {
66          Logger logger = lc.getLogger(Logger.ROOT_LOGGER_NAME);
67          logger.debug("test");
68          ILoggingEvent event = (ILoggingEvent) listAppender.list.get(0);
69          assertEquals("test", event.getFormattedMessage());
70      }
71  
72      @Test
73      public void testMessageConverter() {
74          Logger logger = lc.getLogger(Logger.ROOT_LOGGER_NAME);
75          logger.debug("{}", 12);
76          ILoggingEvent event = (ILoggingEvent) listAppender.list.get(0);
77          PatternLayout layout = new PatternLayout();
78          layout.setContext(lc);
79          layout.setPattern("%m");
80          layout.start();
81          String formattedMessage = layout.doLayout(event);
82          assertEquals("12", formattedMessage);
83      }
84  
85  }