@Plugin(name="TerminalConsole",
category="Core",
elementType="appender",
printObject=true)
public class TerminalConsoleAppender
extends org.apache.logging.log4j.core.appender.AbstractAppender
Appender
that uses the JLine 3.x Terminal
to print messages
to the console.
The JLine Terminal
extends the regular console output with support
for Ansi escape codes on Windows. Additionally, it's LineReader
interface can be used to implement enhanced console input, with an
persistent input line, as well as command history and command completion.
The TerminalConsole
appender replaces the default Console
appender in your log4j configuration. By default, log4j will automatically
close the standard output when the original Console
appender is
removed. Consequently, it is necessary to keep an unused Console
appender.
Example usage:
<TerminalConsole>
<PatternLayout pattern="[%d{HH:mm:ss} %level]: %msg%n"/>
</TerminalConsole>
<Console name="SysOut" target="SYSTEM_OUT"/>
To use the enhanced console input it is necessary to set the
LineReader
using setReader(LineReader)
. The appender will
then automatically redraw the current prompt. When creating the
LineReader
it's important to use the Terminal
returned by getTerminal()
. Additionally, the reader should
be removed from the appender as soon as it's no longer accepting
input (for example when the user interrupted input using CTRL + C.
By default, the JLine Terminal
is enabled when the application
is started with an attached terminal session. Usually, this is only the
case if the application is started from the command line, not if it gets
started by another application.
In some cases, it might be possible to support a subset of the features in these unsupported environments (e.g. only ANSI color codes). In these cases, the system properties may be used to override the default behaviour:
JLINE_OVERRIDE_PROPERTY
- To enable the extended JLine
input. By default this will also enable the ANSI escape codes.ANSI_OVERRIDE_PROPERTY
- To enable the output of ANSI
escape codes. May be used to force the use of ANSI escape codes
if JLine is disabled or to disable them if it is enabled.Modifier and Type | Field and Description |
---|---|
static java.lang.Boolean |
ANSI_OVERRIDE |
static java.lang.String |
ANSI_OVERRIDE_PROPERTY
System property that allows overriding the use of ANSI escape codes
for console formatting even though running in an unsupported
environment.
|
private static boolean |
initialized |
static java.lang.String |
JLINE_OVERRIDE_PROPERTY
System property that allows overriding the default detection of the
console to force enable or force disable the use of JLine.
|
static java.lang.String |
PLUGIN_NAME |
static java.lang.String |
PROPERTY_PREFIX
The prefix used for all system properties in TerminalConsoleAppender.
|
private static org.jline.reader.LineReader |
reader |
private static java.io.PrintStream |
stdout
We grab the standard output
PrintStream early, otherwise we
might cause infinite loops later if the application redirects
System.out to Log4J. |
private static org.jline.terminal.Terminal |
terminal |
Modifier | Constructor and Description |
---|---|
protected |
TerminalConsoleAppender(java.lang.String name,
org.apache.logging.log4j.core.Filter filter,
org.apache.logging.log4j.core.Layout<? extends java.io.Serializable> layout,
boolean ignoreExceptions)
Constructs a new
TerminalConsoleAppender . |
Modifier and Type | Method and Description |
---|---|
void |
append(org.apache.logging.log4j.core.LogEvent event) |
static void |
close()
Closes the JLine
Terminal (if available) and restores the original
terminal settings. |
static TerminalConsoleAppender |
createAppender(java.lang.String name,
org.apache.logging.log4j.core.Filter filter,
org.apache.logging.log4j.core.Layout<? extends java.io.Serializable> layout,
boolean ignoreExceptions)
Creates a new
TerminalConsoleAppender . |
private static java.lang.Boolean |
getOptionalBooleanProperty(java.lang.String name) |
static org.jline.reader.LineReader |
getReader()
Returns the currently configured
LineReader that is used to
read input from the console. |
static org.jline.terminal.Terminal |
getTerminal()
Returns the
Terminal that is used to print messages to the
console. |
private static void |
initializeTerminal() |
static boolean |
isAnsiSupported()
Returns whether ANSI escapes codes should be written to the console
output.
|
static void |
setReader(org.jline.reader.LineReader newReader)
Sets the
LineReader that is used to read input from the console. |
error, error, error, getHandler, getLayout, getName, ignoreExceptions, parseInt, setHandler, toString
addFilter, getFilter, hasFilter, isFiltered, removeFilter, start, stop, stop
equalsImpl, getState, getStatusLogger, hashCodeImpl, initialize, isInitialized, isStarted, isStarting, isStopped, isStopping, setStarted, setStarting, setState, setStopped, setStopping, stop, stop
public static final java.lang.String PLUGIN_NAME
public static final java.lang.String PROPERTY_PREFIX
public static final java.lang.String JLINE_OVERRIDE_PROPERTY
If this system property is not set, or set to an invalid value
(neither true
nor false
) then we will attempt
to detect the best option automatically.
public static final java.lang.String ANSI_OVERRIDE_PROPERTY
If this system property is not set, or set to an invalid value
(neither true
nor false
) then we will attempt
to detect the best option automatically.
public static final java.lang.Boolean ANSI_OVERRIDE
private static final java.io.PrintStream stdout
PrintStream
early, otherwise we
might cause infinite loops later if the application redirects
System.out
to Log4J.private static boolean initialized
@Nullable private static org.jline.terminal.Terminal terminal
@Nullable private static org.jline.reader.LineReader reader
protected TerminalConsoleAppender(java.lang.String name, @Nullable org.apache.logging.log4j.core.Filter filter, org.apache.logging.log4j.core.Layout<? extends java.io.Serializable> layout, boolean ignoreExceptions)
TerminalConsoleAppender
.name
- The name of the appenderfilter
- The filter, can be null
layout
- The layout to useignoreExceptions
- If true
exceptions encountered when
appending events are logged, otherwise they are propagated to the
caller@Nullable public static org.jline.terminal.Terminal getTerminal()
Terminal
that is used to print messages to the
console. Returns null
in unsupported environments, unless
overridden using the JLINE_OVERRIDE_PROPERTY
system
property.TerminalConsoleAppender
@Nullable public static org.jline.reader.LineReader getReader()
LineReader
that is used to
read input from the console. May be null if no LineReader
was configured by the environment.public static void setReader(@Nullable org.jline.reader.LineReader newReader)
LineReader
that is used to read input from the console.
Setting the LineReader
will allow the appender to automatically
redraw the input line when a new log message is added.
Note: The specified LineReader
must be created with
the terminal returned by getTerminal()
.
newReader
- The new line readerpublic static boolean isAnsiSupported()
The return value is true
by default if the JLine terminal
is enabled and false
otherwise. It may be overridden using
the ANSI_OVERRIDE_PROPERTY
system property.
private static void initializeTerminal()
@Nullable private static java.lang.Boolean getOptionalBooleanProperty(java.lang.String name)
public void append(org.apache.logging.log4j.core.LogEvent event)
public static void close() throws java.io.IOException
Terminal
(if available) and restores the original
terminal settings.java.io.IOException
- If an I/O error occurs@PluginFactory public static TerminalConsoleAppender createAppender(@Required(message="No name provided for TerminalConsoleAppender") @PluginAttribute(value="name") java.lang.String name, @PluginElement(value="Filter") @Nullable org.apache.logging.log4j.core.Filter filter, @PluginElement(value="Layout") @Nullable org.apache.logging.log4j.core.Layout<? extends java.io.Serializable> layout, @PluginAttribute(value="ignoreExceptions",defaultBoolean=true) boolean ignoreExceptions)
TerminalConsoleAppender
.name
- The name of the appenderfilter
- The filter, can be null
layout
- The layout, can be null
ignoreExceptions
- If true
exceptions encountered when
appending events are logged, otherwise they are propagated to the
caller