1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2024, 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  
15  package ch.qos.logback.classic.issue.github876;
16  
17  import ch.qos.logback.classic.Logger;
18  import ch.qos.logback.classic.LoggerContext;
19  import ch.qos.logback.classic.spi.ILoggingEvent;
20  import ch.qos.logback.classic.util.LogbackMDCAdapter;
21  import ch.qos.logback.core.read.ListAppender;
22  import org.junit.jupiter.api.BeforeEach;
23  import org.junit.jupiter.api.Test;
24  
25  import static org.junit.jupiter.api.Assertions.assertEquals;
26  
27  public class Github876Test {
28  
29      LoggerContext loggerContext = new LoggerContext();
30      LogbackMDCAdapter logbackMDCAdapter = new LogbackMDCAdapter();
31      ListAppender<ILoggingEvent> listAppender = new ListAppender<ILoggingEvent>();
32      final Logger logger = loggerContext.getLogger(Github876Test.class);
33  
34      @BeforeEach
35      public void setUp() {
36          loggerContext.setMDCAdapter(logbackMDCAdapter);
37  
38          listAppender.setContext(loggerContext);
39          listAppender.setName("list");
40          listAppender.start();
41  
42          logger.addAppender(listAppender);
43  
44      }
45  
46  
47      @Test
48      public void traditionalTest() {
49          Exception ex = new Exception("Some message");
50          logger.error("Exception Message: {}", ex, ex);
51  
52          assertEquals(1, listAppender.list.size());
53          ILoggingEvent iLoggingEvent0 = listAppender.list.get(0);
54  
55          String formattedMessage0 = iLoggingEvent0.getFormattedMessage();
56          assertEquals("Exception Message: java.lang.Exception: Some message", formattedMessage0);
57      }
58  
59      @Test
60      public void fluentTest() {
61          Exception ex = new Exception("Some message");
62          logger.atError().addArgument(ex)
63                          .setCause(ex).setMessage("Exception Message: {}")
64                          .log();
65  
66          assertEquals(1, listAppender.list.size());
67          ILoggingEvent iLoggingEvent0 = listAppender.list.get(0);
68  
69          String formattedMessage0 = iLoggingEvent0.getFormattedMessage();
70          assertEquals("Exception Message: java.lang.Exception: Some message", formattedMessage0);
71      }
72  }