[英]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.我尝试使用Windows 10 命令行在控制台上打印彩色消息,但没有成功。 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:根据Log4j 2 文档,我应该将Jansi jar 添加到我的打印应用程序并将属性
log4j.skipJansi
设置为false
以在 Windows 上启用 ANSI 支持。请你检查并说出我做错了什么:
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!");
}
}
<?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>
log4j-core-2.12.1.jar
log4j-api-2.12.1.jar
jansi-1.18.jar
@echo off
javac -cp lib\*;. LoggerTest.java
java -cp lib\*;. LoggerTest
pause
exit
However, the output in the command line is still not coloured.但是,命令行中的 output 仍然没有着色。 This is what I see:
这是我看到的:
So, the message contains ANSI escape codes, but they are not interpreted by command line.因此,该消息包含 ANSI 转义码,但它们不被命令行解释。 Though, if I try to copy/paste this output and echo it directly using another bat-file, then I see the coloured message.
但是,如果我尝试复制/粘贴此 output 并使用另一个 bat 文件直接回显它,那么我会看到彩色消息。
It worked for when I add disableAnsi="false"
propriety to <PatternLayout>
当我将
disableAnsi="false"
<PatternLayout>
添加到<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>
Try executing this command: "reg add HKCU\Console /v VirtualTerminalLevel /t REG_DWORD /d 1"尝试执行此命令:“reg add HKCU\Console /v VirtualTerminalLevel /t REG_DWORD /d 1”
In Java:在 Java 中:
Runtime.getRuntime().exec("reg add HKCU\\Console /v VirtualTerminalLevel /t REG_DWORD /d 1");
I had to include both Jansi and Jcabi for cross-OS rendering -我必须同时包含 Jansi 和 Jcabi 以进行跨操作系统渲染 -
<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>
This is answer how to enable colors with latest log4j on windows 7:这是如何在 Windows 7 上使用最新的 log4j 启用颜色的答案:
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.关键是将
-DLOG4J_SKIP_JANSI=false
传递给 JVM。 Solution was found in this user guide .在本用户指南中找到了解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.