Spring Boot中的Log4j.properties

32
如何在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文件在src/main/resources中,如何将其映射到application.properties中的logging.config属性。

请建议所有必要的更改。

提前感谢您的帮助。

4个回答

50

如果你想让Spring Boot使用log4j而不是其自带的默认日志(logback),则必须排除默认日志并在pom.xml文件中包含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中定义的属性

例如,您想在log4j.properties中使用在application.properties中定义的log.file.path属性

那么您需要在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文件中


1
我按照你的建议添加了代码,但是出现了错误:对于构件{org.springframework.boot:spring-boot-starter-log4j:null:jar}:版本不能为空。(org.apache.maven.plugins:maven-resources-plugin:2.6:resources:default-resources:process-resources),这个问题在另一个SO问题中有提到。 - Sabir Khan
2
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j</artifactId> </dependency> 为使其工作,我需要添加一个版本号, <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-log4j</artifactId> <version>1.2.8.RELEASE</version> </dependency> - borys86
我有很多 spring-boot-starter-*,但没有完全匹配的。 - Hassan Faghihi
1
spring-boot-starter-log4j2可以在boot 1.5.x上工作,无需显式版本。 - Daniel Hári

8
为了在你的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-log4j2</artifactId>
    </dependency>

如果您希望在log4j.properties文件中使用在application.properties中定义的log4j属性,需要在application.properties文件中添加以下属性。 logging.config = src/main/resources/log4j2.properties
然后,Spring Boot将从log4j2.properties文件中读取属性。
在log4j2.properties文件中添加以下属性。
name=PropertiesConfig
appenders = console, file

appender.console.type = Console
appender.console.name = ConsoleAppender
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{MM:dd HH:mm:ss.SSS} [%t] [%level] [%logger{36}] - %msg%n

appender.file.type = File
appender.file.name = FileAppender
appender.file.fileName=/home/ubuntu/application.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern= %d{MM:dd HH:mm:ss.SSS} [%t] [%level] [%logger{36}] - %msg%n

loggers=file
logger.file.name=com.project
logger.file.level = debug
logger.file.appenderRefs = file
logger.file.appenderRef.file.ref = FileAppender

rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = ConsoleAppender

有没有其他的解决方案,不需要在application.properties文件中指定'logging.config = src/main/resources/log4j2.properties' ..?

注意:我正在使用的Spring Boot版本是2.1.3.RELEASE

参考资料:https://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html


1
我猜测你的pom.xml文件没有设置正确的log4j依赖项。如果你这样做,它应该会自动工作。请参见他们在spring-boot-sample-actuator-log4j中的示例。他们的项目包括spring-boot-starter-log4j工件,这将允许从当前src/main/resources位置获取你的log4j.properties,并且你不再需要在属性中使用logging.*条目。也有可能你需要排除他们的spring-boot-starter-logging,因为我看到他们也在这样做。如果这似乎无法解决问题,请在我可以看到的位置上载你的POM文件。请告诉我这是否对你有效。

1
链接未找到,返回404错误。 - Sabir Khan
1
看起来他们已经把那个移除了。这似乎是最新的 https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-actuator-log4j2 无论如何,上面的答案更好,因为他们解释了如何进行排除。 - Rob Baily

0
如果有人遇到同样的问题,那是因为我没有 spring-boot-starter 依赖项,所以我需要将其添加到 spring-boot-starter-web 依赖项中,这就是它的样子:
<!-- Spring Boot Starter Web -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <!-- Excluding logback dependencies to use l4j2 -->
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-logging</artifactId>
        </exclusion>
    </exclusions>
</dependency>

然后您需要添加Log4j2依赖项:

<!-- Add Log4j2 Dependency -->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

你的log4j2.properties文件将从现在开始用于日志配置。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接