How to have Spring boot use a log4j.xml configuration file?

☆樱花仙子☆ 提交于 2020-01-01 05:03:12

问题


I have a simple Spring Boot application that builds to a jar file. I have a log4j.xml file in src/main/resources/log4j.xml that looks like this (basic sample file from the log4j docs):

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">
    <appender name="stdout" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <!-- Pattern to output the caller's file name and line number -->
            <param name="ConversionPattern" value="%5p [%t] (%F:%L) - %m%n"/>
        </layout>
    </appender>
    <appender name="R" class="org.apache.log4j.RollingFileAppender">
        <param name="file" value="/tmp/logs/sample.log"/>
        <param name="MaxFileSize" value="100KB"/>
        <!-- Keep one backup file -->
        <param name="MaxBackupIndex" value="1"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="%p %t %c - %m%n"/>
        </layout>
    </appender>
    <root>
        <priority value="debug"/>
        <appender-ref ref="stdout"/>
        <appender-ref ref="R"/>
    </root>
</log4j:configuration>

Logging only goes to the console though (/tmp/logs/sample.log never gets created), as it the log4j.xml file is being ignored.

The file shows up in the root of the jar, which I assume is correct. What else do I need to do to have this logging configuration picked up?

If it makes any difference, the project is using Gradle, not Maven.


回答1:


It depends how you set up your classpath. Is the log4j binding to slf4j on your classpath (it won't be if you just use the vanilla spring-boot-starter)? The is a spring-boot-starter-log4j and a sample showing how to use it (in Maven, but Gradle has the same features).

If I were you I'd just use logback.

N.B. Spring Boot 1.4 does not support log4j (only log4j2). There's a sample for that in the same place though.




回答2:


If we use Spring-boot and at the same time also trying to use logging explicitly using log4j, the spring-boot in built logging takes higher precedence and hence log4j configuration is never read. As stated above, the best solution in this case is to exclude logging from the below two spring-boot dependencies.

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</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-test</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>


来源:https://stackoverflow.com/questions/23296477/how-to-have-spring-boot-use-a-log4j-xml-configuration-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!