将log4j.properties文件放在spring启动应用程序中的位置

时间:2016-07-28 15:36:09

标签: spring-boot log4j

我创建了一个log4j.properties文件,并将其作为application.properties文件放在resources文件夹中,在此文件中我有这一行: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>

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:1)

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

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

答案 1 :(得分:0)

两个问题:

  1. 是否需要排除slf4j-log4j12?
  2. 我认为应该将spring-boot-starter-logging排除在spring-boot-starter之外。
  3. 那不是完整的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>
    

答案 2 :(得分:0)

最简单的方法是使用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位于项目的根目录中(不在资源下)。

相关问题