【发布时间】:2022-02-16 20:19:06
【问题描述】:
我使用的是 Java 11、Spring Boot 2.4.1、Intellij Idea Community Edition 2019.2。 我正在使用 Log4j2 并尝试从 log4j2.xml 配置文件更改控制台中日志级别的颜色。 如果我从 public static void main(String[] args) 运行我的应用程序,我会成功,但是如果我运行 我的终端应用程序:'mvn spring-boot:run',然后它不起作用。
我的 log4j2.xml:
<Configuration strict="true">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{DEFAULT} %highlight{%-5p}{DEBUG=blue,INFO=black,WARN=yellow} [%c{1}.%M():%L %t] [${bundle:application:spring.application.name}] %m%n"
disableAnsi="false" charset="UTF-8"/>
</Console>
</Appenders>
<Loggers>
<Root level="INFO">
<AppenderRef ref="Console"/>
</Root>
<Logger name="com.example" level="TRACE" includeLocation="true" additivity="false">
<AppenderRef ref="Console"/>
</Logger>
<Logger name="org.hibernate" level="ERROR" additivity="false">
<AppenderRef ref="Console"/>
</Logger>
<Logger name="org.hibernate.SQL" level="ERROR" additivity="false">
<AppenderRef ref="Console"/>
</Logger>
<Logger name="org.springframework" level="INFO" includeLocation="true" additivity="false">
<AppenderRef ref="Console"/>
</Logger>
</Loggers>
</Configuration>
如果我使用 public static void main(String[] args) 运行,我有例如DEBUG 日志和单词 DEBUG 的颜色为蓝色:
2021-03-08 19:04:56,357 调试 [MyApplication.logStarting():56 main] [My-App-name] 使用 Spring Boot v2.4.1、Spring v5.3.2 运行
如果我在终端中使用 'mvn spring-boot:run' 运行,则没有颜色且日志错误:
2021-03-08 19:15:16,450 ←[34mDEBUG←[m [MyApplication.logStarting():56 main] [My-App-name] 使用 Spring Boot v2.4.1、Spring v5.3.2 运行
我该如何解决? 如果您需要任何澄清,请告诉我,提前谢谢!
编辑:
项目结构(这是我的 sanbox 应用程序,我在其中训练不同的东西)。您还可以在这里看到我如何将 log4j2 连接到 pom.xml 中的项目: enter image description here
我在 pom.xml 中的构建部分: enter image description here
日志,如果运行 public static void main: enter image description here
日志,如果运行 'mvn spring-boot:run'(2 张图片): enter image description here | enter image description here
我尝试使用另一种颜色为“%-5p”的方法:
- 模式:
<PatternLayout pattern="%d{DEFAULT} %clr{%-5p}{magenta} [%c{1}.%M():%L %t] [${bundle:application:spring.application.name}] %m%n" charset="UTF-8"/> - 添加到 application.properties:
spring.output.ansi.enabled=ALWAYS
但它仍然只适用于从 'main' 运行,而不适用于终端。
【问题讨论】:
-
您能否在项目结构中添加两个完整的启动方法日志。
-
您使用的是 Windows 吗?
-
@tmarouane 当然,已添加。
-
@SimonMartinelli 是的,Windows 10 x64
-
您的项目是否包含 jansi jar?
标签: java spring spring-boot log4j2