【问题标题】:where to put the log4j.properties file in a spring boot application将 log4j.properties 文件放在 Spring Boot 应用程序中的位置
【发布时间】:2016-07-28 15:36:09
【问题描述】:

我创建了一个log4j.properties 文件,并将它作为application.properties 文件放在资源文件夹中,在这个文件中我有这一行:logging.config=log4j.properties,它表示我的 log4j 属性文件的名称,但是当我运行我的应用程序时,我的控制台上出现了这个错误:

Logging system failed to initialize using configuration from 'log4j.properties'
java.io.FileNotFoundException: C:\Users\***\Desktop\CapRecrute\log4j.properties (The system cannot find the file specified)

所以我尝试将logging.config 属性更改为src\\main\\resources\\log4j.properties,然后又遇到了另一个错误:

java.lang.IllegalStateException: Could not initialize Logback logging from src\main\resources\log4j.properties ... Caused by: ch.qos.logback.core.LogbackException: Unexpected filename extension of file [file:/C:/Users/Aimad%20MAJDOU/Desktop/CapRecrute/src/main/resources/log4j.properties]. Should be either .groovy or .xml

在我的 pom 文件中我有这个:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</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>
            <exclusions>
                <exclusion>
                    <groupId>org.slf4j</groupId>
                    <artifactId>slf4j-log4j12</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

我该如何解决这个问题?

【问题讨论】:

标签: spring-boot log4j


【解决方案1】:

最简单的就是使用Spring的Logback。您只需将这两行添加到application.properties

logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR

并确保没有其他与日志记录相关的行。

如果你想要 log4j,那么在你的 pom.xml 中进行配置:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
</dependency>
<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>

然后在你的 paplication.properties 中:

logging.path=/tmp/logs/
logging.file=/tmp/logs/myapplog.log
logging.config=log4j.properties

还要确保 log4j 位于项目的根目录中(不在资源下)。

【讨论】:

  • 嘿我,你真棒。节省了我很多时间
【解决方案2】:

Spring boot 默认需要一个 logback 文件,并且您已经提供了 log4j 配置。如果您添加 log4j 依赖项,您应该会发现它有效。如果你使用的是 maven,你可以这样做:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>

【讨论】:

  • 感谢您的帮助,但我的 pom 文件中已经有了 log4j 依赖项
【解决方案3】:

两个问题:

  1. 是否需要排除 slf4j-log4j12?
  2. 我认为应该将 spring-boot-starter-logging 的排除应用于 spring-boot-starter。

这不是完整的 POM。我假设 spring-boot-starter 包括在内,对吧?

如果您删除 slf4j-log4j12 排除项并将 spring-boot-starter-logging 移动到 spring-boot-starter 您的文件应该看起来像文档中的示例。

http://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html

<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-tomcat</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j</artifactId>
</dependency>

【讨论】:

    猜你喜欢
    • 2012-04-18
    • 2015-11-11
    • 2021-11-17
    • 2014-12-21
    • 2017-06-20
    • 2020-11-24
    • 2017-10-23
    • 2018-11-25
    • 2015-10-19
    相关资源
    最近更新 更多