Spring Boot如何使用log4j.xml配置文件? [英] How to have Spring boot use a log4j.xml configuration file?

查看:110
本文介绍了Spring Boot如何使用log4j.xml配置文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Spring Boot应用程序,可以将其构建为jar文件.我在src/main/resources/log4j.xml中有一个如下所示的log4j.xml文件(来自log4j文档的基本示例文件):

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>

尽管日志只进入控制台(/tmp/logs/sample.log从未创建),因为log4j.xml文件被忽略了.

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

该文件显示在jar的根目录中,我认为这是正确的.要使此日志记录配置生效,我还需要做什么?

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?

如果有所不同,则说明该项目使用的是Gradle,而不是Maven.

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

推荐答案

这取决于您如何设置类路径. log4j是否绑定到类路径上的slf4j(如果仅使用vanilla spring-boot-starter,就不会这样)?这是spring-boot-starter-log4j和

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).

如果我是你,我只会使用logback.

If I were you I'd just use logback.

Spring Boot 1.4不支持log4j(仅支持log4j2).虽然在同一地方有一个示例.

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

这篇关于Spring Boot如何使用log4j.xml配置文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆