'Log4j 2: How to enable ANSI colours on Windows console?

I try to use Windows 10 command line to print colored messages on console, but with no success. According to the Log4j 2 documentation, I should add the Jansi jar to my print application and set property log4j.skipJansi to false for enabling ANSI support on Windows. Could you, please, check and say what I did wrong:

  1. Following code is my current work:
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class LoggerTest {
    private static final Logger logger = LogManager.getLogger();

    public static void main(String[] args) throws Exception {
        logger.info("Hello!");
    }
}
  1. Following code is Log4j 2 configuration file:
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>

    <Properties>
        <Property name="log4j.skipJansi" value="false"/>
    </Properties>

    <Appenders>
        <Console name="ConsoleAppender" target="SYSTEM_OUT">
            <PatternLayout>
                <disableAnsi>false</disableAnsi>
                <Pattern>%style{%d [%t] %c %p: %m}{yellow}%n%ex</Pattern>
            </PatternLayout>
        </Console>
    </Appenders>

    <Loggers>
        <Root level="INFO">
            <AppenderRef ref="ConsoleAppender"/>
        </Root>
    </Loggers>

</Configuration>
  1. The contents of \lib directory:
log4j-core-2.12.1.jar
log4j-api-2.12.1.jar
jansi-1.18.jar
  1. And the Windows compileAndRun.bat file:
@echo off
javac -cp lib\*;. LoggerTest.java
java -cp lib\*;. LoggerTest
pause 
exit 

However, the output in the command line is still not coloured. This is what I see:

enter image description here

So, the message contains ANSI escape codes, but they are not interpreted by command line. Though, if I try to copy/paste this output and echo it directly using another bat-file, then I see the coloured message.



Solution 1:[1]

It worked for when I add disableAnsi="false" propriety to <PatternLayout>

 <Appenders>
    <Console name="ConsoleAppender" target="SYSTEM_OUT">
        <PatternLayout
            pattern="%highlight{%p} %logger{-2} - %m{FATAL=red blink,ERROR=red, WARN=yellow bold, INFO=green, DEBUG=green bold}%n" disableAnsi="false"/>
        </Console>
    </Appenders> 

Solution 2:[2]

This is answer how to enable colors with latest log4j on windows 7:

openjdk 14
log4j 2.14.1
jansi 1.18

Configuration:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %highlight{%-5level}{FATAL=red blink, ERROR=red bold, WARN=yellow bold, INFO=magenta, DEBUG=green, TRACE=blue} %logger{36} - %msg%n"/>
        </Console>
    </Appenders>
    <Loggers>
        <Root level="debug">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

The key point is to pass -DLOG4J_SKIP_JANSI=false to JVM. Solution was found in this user guide.

Solution 3:[3]

Try executing this command: "reg add HKCU\Console /v VirtualTerminalLevel /t REG_DWORD /d 1"

In Java:

Runtime.getRuntime().exec("reg add HKCU\\Console /v VirtualTerminalLevel /t REG_DWORD /d 1");

Solution 4:[4]

I had to include both Jansi and Jcabi for cross-OS rendering -


        <dependency>
            <groupId>org.fusesource.jansi</groupId>
            <artifactId>jansi</artifactId>
            <version>1.18</version>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.jcabi</groupId>
            <artifactId>jcabi-log</artifactId>
            <version>LATEST</version>
            <optional>true</optional>
        </dependency>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Ak.tech
Solution 2 Pavel_K
Solution 3 IOExeeption
Solution 4 Marc Magon