gpt4 book ai didi

spring-boot - Spring boot 中的 Log4j.properties

转载 作者:行者123 更新时间:2023-12-03 12:05:15 25 4
gpt4 key购买 nike

如何在 Spring Boot 中加载自定义 Log4j.properties 文件

我在 application.properties 中的代码在这里

logging.file=E:/Apps_Tek/apps-webservices-log/apps-webservices.log
logging.level.*=INFO
logging.config=log4j.properties

我在 log4j.properties 中的代码在这里
log4j.rootLogger=INFO,ConsoleAppender,FileAppender

log4j.appender.ConsoleAppender=org.apache.log4j.ConsoleAppender
log4j.appender.ConsoleAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.ConsoleAppender.layout.ConversionPattern=%-7p %d [%t] %c [%X{userName}] [%X{accessToken}] - %m%n

log4j.appender.FileAppender=org.apache.log4j.RollingFileAppender
log4j.appender.FileAppender.File=E:/Apps_Tek/apps-webservices-log/apps-webservices.log
log4j.appender.FileAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.FileAppender.layout.ConversionPattern=%-7p %d [%t] %c [%X{userName}] [%X{accessToken}] - %m%n

但是我没有得到任何预期的输出,即 spring boot 没有加载 log4j.properties 文件。 Spring Boot 有自己的默认日志记录。

log4j.properties 文件位于 src/main/resources
我的问题是如何将 log4j.properties 文件与 application.properties 中的 logging.config 属性映射,如果它在 src/main/resources 中。

请建议所有必需的更改。

提前感谢您的任何帮助。

最佳答案

如果您希望 spring boot 使用 log4j 而不是它自己的默认日志记录(logback),那么您必须排除默认日志记录并在您的 pom.xml 中包含 spring boot 的 log4j 依赖项:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j</artifactId>
</dependency>

这样将查找位于 src/main/resources 中的 log4j.properties 文件.

此外,如果您希望使用在 log4j.properties 文件中的 application.properties 中定义的属性

for example, you want to have log.file.path defined in your application.properties and use it on log4j.properties



然后你必须在你的 pom.xml: 中定义过滤
<filters>
<filter>src/main/resources/application.properties</filter>
</filters>
<resources>
<resource>
<directory>src/main/resources</directory>
<includes>
<include>log4j.properties</include>
</includes>
<filtering>true</filtering>
</resource>
</resources>

这样你就可以拥有:
log4j.appender.file.File=${log.file.path}/${project.artifactId}.log

在您的 log4j.properties文件

关于spring-boot - Spring boot 中的 Log4j.properties,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29225777/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com