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

Ich*_*aki 5 log4j spring-boot

我已经创建了一个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)
Run Code Online (Sandbox Code Playgroud)

所以我试图将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
Run Code Online (Sandbox Code Playgroud)

在我的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>
Run Code Online (Sandbox Code Playgroud)

我怎么解决这个问题?

sam*_*aml 1

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

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)