1
2
3
4
5
6
7
8
9
10
11
12
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 }