View Javadoc
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.logback_1759;
16  
17  import ch.qos.logback.classic.Level;
18  import ch.qos.logback.classic.Logger;
19  import ch.qos.logback.classic.LoggerContext;
20  import ch.qos.logback.classic.encoder.PatternLayoutEncoder;
21  import ch.qos.logback.classic.spi.LoggingEvent;
22  import ch.qos.logback.classic.util.LogbackMDCAdapter;
23  import ch.qos.logback.core.ConsoleAppender;
24  import ch.qos.logback.core.util.StatusPrinter2;
25  import org.junit.jupiter.api.BeforeEach;
26  import org.junit.jupiter.api.Test;
27  
28  public class Logback1759Test {
29  
30      LoggerContext context = new LoggerContext();
31      Logger logger = context.getLogger("toto.foo");
32      StatusPrinter2 statusPrinter2 = new StatusPrinter2();
33      PatternLayoutEncoder patternLayoutEncoder = null;
34      ConsoleAppender consoleAppender = null;
35      LogbackMDCAdapter logbackMDCAdapter = new LogbackMDCAdapter();
36  
37      @BeforeEach
38      public void setup() {
39          context.setMDCAdapter(logbackMDCAdapter);
40          init();
41      }
42  
43      void init() {
44          System.out.println("Init called");
45          this.patternLayoutEncoder = new PatternLayoutEncoder();
46          patternLayoutEncoder.setContext(context);
47          patternLayoutEncoder.setPattern("%highlight(%level) %message%n");
48          patternLayoutEncoder.start();
49  
50          this.consoleAppender = new ConsoleAppender();
51          consoleAppender.setContext(context);
52          consoleAppender.setEncoder(patternLayoutEncoder);
53      }
54  
55      @Test
56      public void smoke() {
57          consoleAppender.setWithJansi(true);
58          consoleAppender.start();
59  
60          //String fqcn, Logger logger, Level level, String message, Throwable throwable,
61          //        Object[] argArray
62  
63          LoggingEvent le = new LoggingEvent("x", logger, Level.INFO, "hello", null, null);
64  
65          consoleAppender.doAppend(le);
66  
67          consoleAppender.stop();
68  
69          init();
70          consoleAppender.setWithJansi(true);
71          consoleAppender.start();
72  
73          consoleAppender.doAppend(le);
74  
75          //statusPrinter2.print(context);
76      }
77  }