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