ConsoleAppender.java

/*
 * Logback: the reliable, generic, fast and flexible logging framework.
 * Copyright (C) 1999-2026, QOS.ch. All rights reserved.
 *
 * This program and the accompanying materials are dual-licensed under
 * either the terms of the Eclipse Public License v2.0 as published by
 * the Eclipse Foundation
 *
 *   or (per the licensee's choosing)
 *
 * under the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation.
 */
package ch.qos.logback.core;

import java.io.OutputStream;
import java.io.PrintStream;
import java.lang.reflect.Method;
import java.lang.reflect.Modifier;
import java.util.Arrays;
import java.util.NoSuchElementException;
import java.util.Optional;

import ch.qos.logback.core.joran.spi.ConsoleTarget;
import ch.qos.logback.core.status.Status;
import ch.qos.logback.core.status.WarnStatus;
import ch.qos.logback.core.util.Loader;
import ch.qos.logback.core.util.ReentryGuard;
import ch.qos.logback.core.util.ReentryGuardFactory;

/**
 * ConsoleAppender appends log events to <code>System.out</code> or
 * <code>System.err</code> using a layout specified by the user. The default
 * target is <code>System.out</code>.
 * <p>
 * &nbsp;
 * </p>
 * For more information about this appender, please refer to the online manual
 * at http://logback.qos.ch/manual/appenders.html#ConsoleAppender
 *
 * @author Ceki G&uuml;lc&uuml;
 * @author Tom SH Liu
 * @author Ruediger Dohna
 */

public class ConsoleAppender<E> extends OutputStreamAppender<E> {

    protected ConsoleTarget target = ConsoleTarget.SystemOut;
    protected boolean withJansi = false;


    public final static String JLINE_JANSI_ANSI_CONSOLE_CLASS_NAME = "org.jline.jansi.AnsiConsole";
    public final static String FUSESOURCE_JANSI_ANSI_CONSOLE_CLASS_NAME = "org.fusesource.jansi.AnsiConsole";

    // Jansi was migrated from FuseSource (org.fusesource.jansi) to JLine (org.jline.jansi), which
    // changed the package of AnsiConsole. Probe the JLine coordinates first, then fall back to the
    // legacy FuseSource ones so that <withJansi> keeps working with both artifacts. See LOGBACK issue 1043.
    private final static String[] ANSI_CONSOLE_CLASS_NAMES = { JLINE_JANSI_ANSI_CONSOLE_CLASS_NAME,
            FUSESOURCE_JANSI_ANSI_CONSOLE_CLASS_NAME };

    protected String preferredJansiClassName = null;

    private final static String JANSI2_OUT_METHOD_NAME = "out";
    private final static String JANSI2_ERR_METHOD_NAME = "err";
    private final static String WRAP_SYSTEM_OUT_METHOD_NAME = "wrapSystemOut";
    private final static String WRAP_SYSTEM_ERR_METHOD_NAME = "wrapSystemErr";
    private final static String SYSTEM_INSTALL_METHOD_NAME = "systemInstall";
    private final static Class<?>[] ARGUMENT_TYPES = { PrintStream.class };

    private final static String CONSOLE_APPENDER_WARNING_URL = CoreConstants.CODES_URL+"#slowConsole";

    /**
     * Sets the value of the <b>Target</b> option. Recognized values are
     * "System.out" and "System.err". Any other value will be ignored.
     */
    public void setTarget(String value) {
        ConsoleTarget t = ConsoleTarget.findByName(value.trim());
        if (t == null) {
            targetWarn(value);
        } else {
            target = t;
        }
    }

    /**
     * Returns the current value of the <b>target</b> property. The default value of
     * the option is "System.out".
     * <p>
     * See also {@link #setTarget}.
     */
    public String getTarget() {
        return target.getName();
    }

    /**
     *
     * @return the preferred Jansi class name
     */
    public String getPreferredJansiClassName() {
        return preferredJansiClassName;
    }

    /**
     * It allows to force Jansi class name used for probing.
     *
     * <p>Used for testing purposes.</p>
     * <p>Valid values are {@link #JLINE_JANSI_ANSI_CONSOLE_CLASS_NAME} and
     * {@link #FUSESOURCE_JANSI_ANSI_CONSOLE_CLASS_NAME}.</p>
     *
     * @param preferredJansiClassName the preferred Jansi class name
     * @since 1.6.1
     */
    public void setPreferredJansiClassName(String preferredJansiClassName) {
        this.preferredJansiClassName = preferredJansiClassName;
    }

    private boolean isValidPreferredJansiClassName(String className) {
        return JLINE_JANSI_ANSI_CONSOLE_CLASS_NAME.equals(className)
                || FUSESOURCE_JANSI_ANSI_CONSOLE_CLASS_NAME.equals(className);
    }

    private void preferredJansiClassNameWarn(String val) {
        Status status = new WarnStatus(
                "[" + val + "] should be one of " + Arrays.toString(ANSI_CONSOLE_CLASS_NAMES), this);
        status.add(new WarnStatus("Ignoring preferredJansiClassName, using default probing order.", this));
        addStatus(status);
    }

    private void targetWarn(String val) {
        Status status = new WarnStatus("[" + val + "] should be one of " + Arrays.toString(ConsoleTarget.values()),
                this);
        status.add(new WarnStatus("Using previously set target, System.out by default.", this));
        addStatus(status);
    }

    @Override
    public void start() {
        addInfo("NOTE: Writing to the console can be slow. Try to avoid logging to the ");
        addInfo("console in production environments, especially in high volume systems.");
        addInfo("See also "+CONSOLE_APPENDER_WARNING_URL);
        OutputStream targetStream = target.getStream();
        // enable jansi only if withJansi set to true
        if (withJansi) {
            targetStream = wrapWithJansi(targetStream);
        }
        setOutputStream(targetStream);
        super.start();
    }

    /**
     * Create a ThreadLocal ReentryGuard to prevent recursive appender invocations.
     * @return a ReentryGuard instance of type {@link ReentryGuardFactory.GuardType#THREAD_LOCAL THREAD_LOCAL}.
     */
    protected ReentryGuard buildReentryGuard() {
        return ReentryGuardFactory.makeGuard(ReentryGuardFactory.GuardType.THREAD_LOCAL);
    }

    private OutputStream wrapWithJansi(OutputStream targetStream) {
        try {
            addInfo("Enabling JANSI AnsiPrintStream for the console.");
            ClassLoader classLoader = Loader.getClassLoaderOfObject(context);
            Class<?> classObj = loadAnsiConsoleClass(classLoader);

            Method systemInstallMethod  = classObj.getMethod(SYSTEM_INSTALL_METHOD_NAME);
            if(systemInstallMethod != null) {
                systemInstallMethod.invoke(null);
            }

            // check for JAnsi 2
            String methodNameJansi2 = target == ConsoleTarget.SystemOut ? JANSI2_OUT_METHOD_NAME
                    : JANSI2_ERR_METHOD_NAME;
            final Optional<Method> optOutMethod = Arrays.stream(classObj.getMethods())
                    .filter(m -> m.getName().equals(methodNameJansi2))
                    .filter(m -> m.getParameters().length == 0)
                    .filter(m -> Modifier.isStatic(m.getModifiers()))
                    .filter(m -> PrintStream.class.isAssignableFrom(m.getReturnType()))
                    .findAny();
            if (optOutMethod.isPresent()) {
                final Method outMethod = optOutMethod.orElseThrow(() -> new NoSuchElementException("No out/err method present"));
                return (PrintStream) outMethod.invoke(null);
            }

            // JAnsi 1
            String methodName = target == ConsoleTarget.SystemOut ? WRAP_SYSTEM_OUT_METHOD_NAME
                    : WRAP_SYSTEM_ERR_METHOD_NAME;
            Method method = classObj.getMethod(methodName, ARGUMENT_TYPES);
            return (OutputStream) method.invoke(null, new PrintStream(targetStream));
        } catch (Exception e) {
            addWarn("Failed to create AnsiPrintStream. Falling back on the default stream.", e);
        }
        return targetStream;
    }

    /**
     * Loads the Jansi {@code AnsiConsole} class.
     * <p>
     * If {@link #preferredJansiClassName} is set to a valid value
     * ({@link #JLINE_JANSI_ANSI_CONSOLE_CLASS_NAME} or {@link #FUSESOURCE_JANSI_ANSI_CONSOLE_CLASS_NAME}),
     * that class is loaded. An invalid preferred value is reported and ignored.
     * <p>
     * If {@code preferredJansiClassName} is not set (or was invalid), candidates are probed in
     * {@link #ANSI_CONSOLE_CLASS_NAMES} order (JLine's {@code org.jline.jansi} first, then the legacy
     * FuseSource {@code org.fusesource.jansi}). This keeps {@code <withJansi>} working across the Jansi
     * migration from FuseSource to JLine.
     *
     * @throws ClassNotFoundException if none of the candidate classes is available.
     */
    Class<?> loadAnsiConsoleClass(ClassLoader classLoader) throws ClassNotFoundException {
        if (preferredJansiClassName != null) {
            if (isValidPreferredJansiClassName(preferredJansiClassName)) {
                return classLoader.loadClass(preferredJansiClassName);
            } else {
                preferredJansiClassNameWarn(preferredJansiClassName);
            }
        }
        ClassNotFoundException lastException = null;
        for (String className : ANSI_CONSOLE_CLASS_NAMES) {
            try {
                return classLoader.loadClass(className);
            } catch (ClassNotFoundException e) {
                lastException = e;
            }
        }
        throw lastException;
    }

    /**
     * @return whether to use JANSI or not.
     */
    public boolean isWithJansi() {
        return withJansi;
    }

    /**
     * If true, this appender will output to a stream provided by the JANSI library.
     *
     * @param withJansi whether to use JANSI or not.
     * @since 1.0.5
     */
    public void setWithJansi(boolean withJansi) {
        this.withJansi = withJansi;
    }

}