Java log4j 2 向控制台追加器添加多种颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21979699/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
log4j 2 adding multiple colors to console appender
提问by Rasika Perera
Hi I just downloaded and configured log4j-2. I am stuck on applying color codes to the SlowConsole
console appender. My console appender is like below.
嗨,我刚刚下载并配置了 log4j-2。我坚持将颜色代码应用于SlowConsole
控制台附加程序。我的控制台 appender 如下所示。
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n"/>
</Console>
<Console name="SlowConsole" target="SYSTEM_OUT">
<PatternLayout pattern="%highlight{%d{HH:mm:ss.SSS} %-5level %logger{36}.%M() @%L - %msg%n}{FATAL=red, ERROR=red, WARN=yellow, INFO=black, DEBUG=green, TRACE=blue}"/>
</Console>
<File name="File" fileName="C:\log\out.txt">
<PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %logger{36} - %msg%n" />
</File>
</Appenders>
<Loggers>
<logger name="org.abc.ea.web" level="ALL" additivity="false">
<!--Log4j for the WEB MODULE -->
<appender-ref ref="SlowConsole"/>
</logger>
<logger name="org.abc.ea.ejb" level="ALL" additivity="false">
<!--Log4j for the EJB MODULE -->
<appender-ref ref="SlowConsole"/>
</logger>
<Root level="ERROR">
<AppenderRef ref="Console"/>
<AppenderRef ref="File" />
</Root>
</Loggers>
</Configuration>
I have two questions,
我有两个问题,
I am new to log4j, is this the right way to write xml config file?
How can i add two color codes to each log level?
for example: DEBUG=green -> will output light green font, But i need it to be
dim
andbold
我是 log4j 的新手,这是编写 xml 配置文件的正确方法吗?
如何为每个日志级别添加两个颜色代码?
例如:DEBUG=green -> 将输出浅绿色字体,但我需要它
dim
并且bold
采纳答案by Rasika Perera
I think I found the solution. I downloaded log4j2-core-sources.jar and traced the source. You can write it as below;
我想我找到了解决方案。我下载了 log4j2-core-sources.jar 并跟踪了源。你可以写成如下;
<Console name="SlowConsole" target="SYSTEM_OUT">
<PatternLayout disableAnsi="false" pattern="%highlight{%d{HH:mm:ss.SSS} %-5level %logger{36}.%M() @%L - %msg%n}{FATAL=red blink, ERROR=red, WARN=yellow bold, INFO=black, DEBUG=green bold, TRACE=blue}"/>
</Console>
I think log4j2 documentationand its examples may need to be updated.
我认为 log4j2文档及其示例可能需要更新。
回答by Taher Khorshidi
1 : yes it's ok! and you can add some other options. see http://logging.apache.org/log4j/2.x/manual/configuration.html
1:是的,没关系!您可以添加一些其他选项。见http://logging.apache.org/log4j/2.x/manual/configuration.html
2 : you can use a HTMLLayout for your file appender.
2 :您可以为文件附加程序使用 HTMLLayout。
see http://logging.apache.org/log4j/2.x/manual/layouts.html
见http://logging.apache.org/log4j/2.x/manual/layouts.html
and http://www.tutorialspoint.com/log4j/log4j_htmllayout.htm
回答by B_St
When using the eclipse console, its nice to see errors in red and all other logs in black. You can do this using Filters:
使用 Eclipse 控制台时,很高兴看到错误显示为红色,所有其他日志显示为黑色。您可以使用过滤器执行此操作:
<Appenders>
<Console name="ConsoleStdOut" target="SYSTEM_OUT">
<ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="ACCEPT"/>
<PatternLayout pattern="%d{HH:mm:ss} [%t] %-5level %logger{36} - %msg%n" />
</Console>
<Console name="ConsoleStdErr" target="SYSTEM_ERR">
<ThresholdFilter level="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="%d{HH:mm:ss} [%t] %-5level %logger{36} - %msg%n" />
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="ConsoleStdOut" />
<AppenderRef ref="ConsoleStdErr" />
</Root>
</Loggers>
回答by Berthier Lemieux
For a log4j2 colored output that looks very close to Spring Boot's default logback console output:
对于看起来非常接近 Spring Boot 的默认 logback 控制台输出的 log4j2 彩色输出:
<Console name="ConsoleAppender" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} %highlight{${LOG_LEVEL_PATTERN:-%5p}}{FATAL=red blink, ERROR=red, WARN=yellow bold, INFO=green, DEBUG=green bold, TRACE=blue} %style{${sys:PID}}{magenta} [%15.15t] %style{%-40.40C{1.}}{cyan} : %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>
</Console>
回答by BaiJiFeiLong
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
<Appenders>
<Console name="Console" target="SYSTEM_OUT" follow="true">
<PatternLayout
pattern="%clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint} %clr{%5p} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n%xwEx"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
This worked in my Spring Boot application.
这在我的 Spring Boot 应用程序中有效。
But I am not sure if it will work without Spring Boot.
但我不确定它是否可以在没有 Spring Boot 的情况下工作。
回答by yuen26
Spring Boot style:
弹簧靴风格:
<Properties>
<Property name="LOG_PATTERN">
%d{yyyy-MM-dd HH:mm:ss.SSS} %highlight{${LOG_LEVEL_PATTERN:-%5p}}{FATAL=red, ERROR=red, WARN=yellow, INFO=green, DEBUG=green, TRACE=green} %style{${sys:PID}}{magenta} --- [%15.15t] %style{%-40.40c{1.}}{cyan} : %m%n%ex
</Property>
</Properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="${LOG_PATTERN}"/>
</Console>
</Appenders>