escudero380
escudero380

Reputation: 586

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.

Upvotes: 6

Views: 4568

Answers (5)

For me it required to add line:

AnsiConsole.systemInstall();

After that colored output started to work in Windows 10 cmd but stopped to work in Eclipse console, so I call it on addition of special argument in my main method.

Upvotes: 1

byteunderflow
byteunderflow

Reputation: 161

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");

Upvotes: 1

user5182503
user5182503

Reputation:

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.

Upvotes: 1

Marc Magon
Marc Magon

Reputation: 848

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>

Upvotes: 1

Ak.tech
Ak.tech

Reputation: 81

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> 

Upvotes: 8

Related Questions