View Javadoc
1   /*
2    * Logback: the reliable, generic, fast and flexible logging framework.
3    * Copyright (C) 1999-2026, 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 v2.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.core;
15  
16  import java.io.OutputStream;
17  
18  import org.jline.jansi.AnsiConsole;
19  
20  import ch.qos.logback.core.joran.spi.ConsoleTarget;
21  
22  import static ch.qos.logback.core.util.Loader.isClassLoadable;
23  
24  /**
25   * A {@link ConsoleAppender} that always writes through JLine's
26   * {@link AnsiConsole}, enabling ANSI sequences on platforms that need Jansi
27   * (notably Windows).
28   * <p>
29   * Unlike {@link ConsoleAppender}'s deprecated {@code withJansi} path, this
30   * class overrides {@link #wrapTarget(OutputStream)} and calls
31   * {@link AnsiConsole} directly (no reflection). It requires
32   * {@code org.jline:jansi-core} on the classpath.
33   * </p>
34   * <p>
35   * {@link AnsiConsole#systemInstall()} is paired with
36   * {@link AnsiConsole#systemUninstall()} on {@link #stop()} when this appender
37   * performed the install. Console streams are still only flushed on stop (not
38   * closed); see {@link ConsoleAppender#closeOutputStream()}.
39   * </p>
40   *
41   * @param <E> the type of logging events
42   * @author Ceki G&uuml;lc&uuml;
43   * @since 1.6.3
44   * @see AnsiConsole
45   * @see ConsoleAppender#wrapTarget(OutputStream)
46   */
47  public class JansiConsoleAppender<E> extends ConsoleAppender<E> {
48  
49  
50      static final String JLINE_JANSI_ANSI_CONSOLE_CLASS_NAME = "org.jline.jansi.AnsiConsole";
51      /**
52       * Status message emitted when {@link #JLINE_JANSI_ANSI_CONSOLE_CLASS_NAME}
53       * cannot be loaded. The appender then falls back on the raw console stream.
54       */
55      static final String JANSI_NOT_LOADABLE_MSG0 = "Could not find " + JLINE_JANSI_ANSI_CONSOLE_CLASS_NAME
56              + " on the class path. Falling back on the default stream.";
57  
58      static final String JANSI_NOT_LOADABLE_MSG1= "To enable JANSI, add org.jline:jansi-core to the class path.";
59      static final String JANSI_NOT_LOADABLE_MSG2= "See also "+CoreConstants.CODES_URL+"#missingJlineJansi";
60  
61      /**
62       * True after this instance has successfully called
63       * {@link AnsiConsole#systemInstall()} and until the matching
64       * {@link AnsiConsole#systemUninstall()} on {@link #stop()}.
65       */
66      private boolean installedByThisAppender;
67  
68      /**
69       * Flushes the console stream (via {@link ConsoleAppender#stop()}), then
70       * undoes {@link AnsiConsole#systemInstall()} if this appender performed it.
71       */
72      @Override
73      public void stop() {
74          try {
75              super.stop();
76          } finally {
77              uninstallAnsiConsoleIfInstalledByThisAppender();
78          }
79      }
80  
81      /**
82       * Installs Jansi and returns {@link AnsiConsole#out()} or
83       * {@link AnsiConsole#err()} according to the configured target.
84       * <p>
85       * Does not use the deprecated {@code withJansi} / {@code wrapWithJansi}
86       * path. {@link AnsiConsole#systemInstall()} is invoked at most once per
87       * install ownership of this instance.
88       * </p>
89       * <p>
90       * If {@link #JLINE_JANSI_ANSI_CONSOLE_CLASS_NAME} is not loadable, a warning
91       * is emitted and {@code targetStream} is returned unchanged.
92       * </p>
93       */
94      @Override
95      protected OutputStream wrapTarget(OutputStream targetStream) {
96          boolean jansiLoadable = isClassLoadable(JLINE_JANSI_ANSI_CONSOLE_CLASS_NAME, getContext());
97          if (!jansiLoadable) {
98              addWarn(JANSI_NOT_LOADABLE_MSG0);
99              addWarn(JANSI_NOT_LOADABLE_MSG1);
100             addWarn(JANSI_NOT_LOADABLE_MSG2);
101             return targetStream;
102         }
103         try {
104             addInfo("Enabling JANSI AnsiPrintStream via " + JLINE_JANSI_ANSI_CONSOLE_CLASS_NAME + ".");
105             if (!installedByThisAppender) {
106                 AnsiConsole.systemInstall();
107                 installedByThisAppender = true;
108             }
109             if (target == ConsoleTarget.SystemErr) {
110                 return AnsiConsole.err();
111             } else {
112                 return AnsiConsole.out();
113             }
114         } catch (Exception e) {
115             addWarn("Failed to create AnsiPrintStream. Falling back on the default stream.", e);
116             return targetStream;
117         }
118     }
119 
120     private void uninstallAnsiConsoleIfInstalledByThisAppender() {
121         if (!installedByThisAppender) {
122             return;
123         }
124         installedByThisAppender = false;
125         try {
126             AnsiConsole.systemUninstall();
127             addInfo("Uninstalled JANSI AnsiConsole previously installed by this appender.");
128         } catch (RuntimeException e) {
129             addWarn("Failed to uninstall AnsiConsole.", e);
130         }
131     }
132 
133 }