All Projects → Minecrell → TerminalConsoleAppender

Minecrell / TerminalConsoleAppender

Licence: MIT license
JLine 3 appender for Log4j2, allows extending command line apps using colors and command completion

Programming Languages

java
68154 projects - #9 most used programming language
kotlin
9241 projects

Projects that are alternatives of or similar to TerminalConsoleAppender

term
终端输出控制
Stars: ✭ 38 (-22.45%)
Mutual labels:  console, ansi-colors
Spectre.console
A .NET library that makes it easier to create beautiful console applications.
Stars: ✭ 4,226 (+8524.49%)
Mutual labels:  console, ansi-colors
Gradle Test Logger Plugin
A Gradle plugin for printing beautiful logs on the console while running tests
Stars: ✭ 460 (+838.78%)
Mutual labels:  console, ansi-colors
log4j-sniffer
A tool that scans archives to check for vulnerable log4j versions
Stars: ✭ 180 (+267.35%)
Mutual labels:  log4j, log4j2
ansicolor
A JavaScript ANSI color/style management. ANSI parsing. ANSI to CSS. Small, clean, no dependencies.
Stars: ✭ 91 (+85.71%)
Mutual labels:  console, ansi-colors
Mordant
Full-featured text styling for Kotlin command-line applications
Stars: ✭ 382 (+679.59%)
Mutual labels:  console, ansi-colors
Ansi Econsole
Eclipse plugin that understands ANSI escape sequences to color the Eclipse console output.
Stars: ✭ 72 (+46.94%)
Mutual labels:  console, ansi-colors
log4jshield
Log4j Shield - fast ⚡, scalable and easy to use Log4j vulnerability CVE-2021-44228 finder and patcher
Stars: ✭ 13 (-73.47%)
Mutual labels:  log4j, log4j2
log-utils
Basic logging utils: colors, symbols and timestamp.
Stars: ✭ 24 (-51.02%)
Mutual labels:  console, ansi-colors
liquibase-slf4j
Liquibase SLF4J Logger.
Stars: ✭ 42 (-14.29%)
Mutual labels:  log4j, log4j2
Log4j-RCE-Scanner
Remote command execution vulnerability scanner for Log4j.
Stars: ✭ 200 (+308.16%)
Mutual labels:  log4j, log4j2
ansiart2utf8
Processes legacy BBS-style ANSI art (ACiDDraw, PabloDraw, etc.) to UTF-8. Escape codes and line endings are processed for terminal friendliness.
Stars: ✭ 32 (-34.69%)
Mutual labels:  console, ansi-colors
log4shelldetect
Rapidly scan filesystems for Java programs potentially vulnerable to Log4Shell (CVE-2021-44228) or "that Log4j JNDI exploit" by inspecting the class paths inside files
Stars: ✭ 40 (-18.37%)
Mutual labels:  log4j, log4j2
Pterm
✨ #PTerm is a modern go module to beautify console output. Featuring charts, progressbars, tables, trees, and many more 🚀 It's completely configurable and 100% cross-platform compatible.
Stars: ✭ 449 (+816.33%)
Mutual labels:  console, ansi-colors
log4shell-finder
Fastest filesystem scanner for log4shell (CVE-2021-44228, CVE-2021-45046) and other vulnerable (CVE-2017-5645, CVE-2019-17571, CVE-2022-23305, CVE-2022-23307 ... ) instances of log4j library. Excellent performance and low memory footprint.
Stars: ✭ 22 (-55.1%)
Mutual labels:  log4j, log4j2
Video To Ascii
It is a simple python package to play videos in the terminal using characters as pixels
Stars: ✭ 960 (+1859.18%)
Mutual labels:  console, ansi-colors
cloudrasp-log4j2
一个针对防御 log4j2 CVE-2021-44228 漏洞的 RASP 工具。 A Runtime Application Self-Protection module specifically designed for log4j2 RCE (CVE-2021-44228) defense.
Stars: ✭ 105 (+114.29%)
Mutual labels:  log4j, log4j2
Log4jPatcher
A mitigation for CVE-2021-44228 (log4shell) that works by patching the vulnerability at runtime. (Works with any vulnerable java software, tested with java 6 and newer)
Stars: ✭ 43 (-12.24%)
Mutual labels:  log4j, log4j2
HackLog4j
《HackLog4j-永恒之恶龙》致敬全宇宙最无敌的Java日志库!Tribute to the most invincible Java logging library in the universe!
Stars: ✭ 161 (+228.57%)
Mutual labels:  log4j, log4j2
ranger-cmus
Ranger Plugin for Integration with Cmus Audio Player
Stars: ✭ 20 (-59.18%)
Mutual labels:  console

TerminalConsoleAppender

TerminalConsoleAppender is a custom Log4j2 Appender that prints all messages to a JLine 3 Terminal. JLine can be used to extend the regular console using ANSI colors as well as command history or command completion.

Supported environments

Not all environments can support all the features. By default, TerminalConsoleAppender only enables JLine output and console colors when running in an environment with an attached terminal session (so usually only when starting the application from an interactive console session). It will automatically disable the features if the console output is redirected to a file or to another application (e.g. in web control panels).

Some unsupported environments may still support a subset of the features (e.g. ANSI console colors). In these cases, extra system properties exist to override the default behaviour. They can be added on the command line or in the application itself:

Name Description
-Dterminal.jline=<true/false> Enables/disables the extended JLine input (persistent input line, command completion)
-Dterminal.ansi=<true/false> Enables/disables the output of ANSI escapes codes (used for colors)
-Dterminal.keepMinecraftFormatting=true Output raw Minecraft formatting codes to the console output.

Usage

  1. Add a dependency on TerminalConsoleAppender:

    net.minecrell:terminalconsoleappender:1.3.0
    

    If you package all dependencies into a single JAR file, ensure that all transitive dependencies are included:

    org.jline:jline-terminal
    org.jline:jline-reader
    

    JLine 3 provides different native terminal implementations that are required for Windows support and extend the terminal with some advanced features on Linux. You can choose between JNA and Jansi. There should be no functional difference, so it is mostly a matter of preference.

    For full functionality, you need to add an explicit dependency on one of the terminal implementations:

    Jansi JNA
    Dependencies org.jline:jline-terminal-jansi:3.20.0 org.jline:jline-terminal-jna:3.20.0
    (org.fusesource.jansi:jansi) (net.java.dev.jna:jna)
    Size ~280KB ~1400KB
  2. Configure TerminalConsoleAppender in your Log4j configuration:

    <TerminalConsole name="Console">
        <PatternLayout pattern="[%d{HH:mm:ss} %level]: %msg%n"/>
    </TerminalConsole>

    The TerminalConsole appender replaces the regular Console appender in your configuration file.

    Note: To avoid JLine from blocking your application in some edge cases, it is recommended that you make use of Async Loggers or Async Appenders to write messages asynchronously.

Console input

The appender is designed to be used in an application with simultaneous input and output. JLine can extend your console with a persistent input line as well as command history and command completion.

TerminalConsoleAppender includes SimpleTerminalConsole as a base class handling console input with opinionated defaults. It also serves as a reference implementation if you would like to have a custom implementation.

To use it, extend SimpleTerminalConsole and implement the methods:

public class ExampleConsole extends SimpleTerminalConsole {

    @Override
    protected boolean isRunning() {
        // TODO: Return true if your application is still running
    }

    @Override
    protected void runCommand(String command) {
        // TODO: Run command
    }

    @Override
    protected void shutdown() {
        // TODO: Shutdown your application cleanly (e.g. because CTRL+C was pressed)
    }
    
}

You can then start reading commands. Note that this method won't return unless your application is stopping or an error occurred, so you should start it in a separate console thread.

new ExampleConsole().start();

This setup will automatically handle the persistent input line and command history for you. If you'd like to use command completion you need to implement JLine's Completer interface (or use one of the builtin completers). You can then set it using .completer(Completer) when building the LineReader.

Override the buildReader method in your ExampleConsole to set additional options:

    @Override
    protected LineReader buildReader(LineReaderBuilder builder) {
        return super.buildReader(builder
                .appName("Example") // TODO: Replace with your application name
                .completer(new ExampleCommandCompleter())
        );
    }

If you'd like to use a custom console input implementation, take a look at the source code of SimpleTerminalConsole to see how it works (as the name says, it's pretty simple!).

Colorizing console output

JLine will automatically render ANSI color escape codes in supported terminals under Windows, Mac and Linux. To use them, you need to instruct Log4j to insert them into log messages:

  • You can use the patterns included in Log4j in your PatternLayout, e.g. %highlight or %style. It is recommended to use the disableAnsi option together with the ${tca:disableAnsi} variable. This will ensure that ANSI colors are disabled automatically in unsupported environments or with the system properties mentioned above:

    <TerminalConsole>
        <PatternLayout pattern="%highlight{[%d{HH:mm:ss} %level]: %msg%n%xEx}" disableAnsi="${tca:disableAnsi}"/>
    </TerminalConsole>
  • You can use the simplified %highlightError pattern bundled with TerminalConsoleAppender. It will only mark errors red and warnings yellow and keep all other messages as-is. It will automatically disable itself in unsupported environments.

    <TerminalConsole>
        <PatternLayout pattern="%highlightError{[%d{HH:mm:ss} %level]: %msg%n%xEx}"/>
    </TerminalConsole>
  • You can implement custom PatternConverters that add colors to the logging output. TerminalConsoleAppender contains an example implementation for Minecraft formatting codes that replaces them with appropriate ANSI colors:

    <TerminalConsole>
        <PatternLayout pattern="[%d{HH:mm:ss} %level]: %minecraftFormatting{%msg}%n"/>
    </TerminalConsole>

    It can be also configured with the strip option to strip all formatting codes instead (e.g. for the log file):

    <TerminalConsole>
        <PatternLayout pattern="[%d{HH:mm:ss} %level]: %minecraftFormatting{%msg}{strip}%n"/>
    </TerminalConsole>

    NOTE: The MinecraftFormattingConverter is deprecated and may be removed in future versions of TerminalConsoleAppender. There are no plans to extend it with new features like RGB color codes. Please see issue #18 for details.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].