如何使用spring boot使用外部log4j.xml配置文件?

Mar*_*inh 2 logging spring log4j

我创建了一个spring boot项目,并希望在我的jar中使用外部log4j.xml配置文件.我正在使用的命令行是这样的:

java -Dlog4j.debug -Dlog4j.configuration=file:/tmp/log4j.xml -jar /tmp/project.jar
Run Code Online (Sandbox Code Playgroud)

通过查看调试,看起来它实际上正确地加载了log4j.xml,但是在加载log4j.xml之后不久,它就会在spring-boot jar文件中加载log4j.properties,它会覆盖我的log4j.xml.有没有办法忽略spring-boot jar文件中的log4j.properties?

log4j: Using URL [file:/tmp/log4j.xml] for automatic log4j configuration.
log4j: Preferred configurator class: org.apache.log4j.xml.DOMConfigurator
log4j: System property is :null
log4j: Standard DocumentBuilderFactory search succeded.
log4j: DocumentBuilderFactory is: com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl
log4j: debug attribute= "null".
log4j: Ignoring debug attribute.
log4j: reset attribute= "false".
log4j: Threshold ="null".
log4j: Retreiving an instance of org.apache.log4j.Logger.
log4j: Setting [com.test] additivity to [true].
log4j: Level value for com.test is  [trace].
log4j: com.test level set to TRACE
log4j: Retreiving an instance of org.apache.log4j.Logger.
log4j: Setting [org.springframework.core] additivity to [true].
log4j: Level value for org.springframework.core is  [info].
log4j: org.springframework.core level set to INFO
log4j: Retreiving an instance of org.apache.log4j.Logger.
log4j: Setting [org.springframework.beans] additivity to [true].
log4j: Level value for org.springframework.beans is  [info].
log4j: org.springframework.beans level set to INFO
log4j: Retreiving an instance of org.apache.log4j.Logger.
log4j: Setting [org.springframework.context] additivity to [true].
log4j: Level value for org.springframework.context is  [info].
log4j: org.springframework.context level set to INFO
log4j: Retreiving an instance of org.apache.log4j.Logger.
log4j: Setting [org.springframework.web] additivity to [true].
log4j: Level value for org.springframework.web is  [info].
log4j: org.springframework.web level set to INFO
log4j: Level value for root is  [warn].
log4j: root level set to WARN
log4j: Class name: [org.apache.log4j.RollingFileAppender]
log4j: Setting property [file] to [/var/log/app/app.log].
log4j: Setting property [maxFileSize] to [5000KB].
log4j: Setting property [maxBackupIndex] to [5].
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%d{yyyy-MM-dd'T'HH:mm:ss.SSSZZZZ} %-5.5p [%-15.15t][%30.30c{2}#%17.17M]: %m%n].
log4j: setFile called: /var/log/app/app.log, true
log4j: setFile ended
log4j: Adding appender named [R] to category [root].
log4j: Reading configuration from URL jar:file:/tmp/project.jar!/lib/spring-boot-1.2.2.RELEASE.jar!/org/springframework/boot/logging/log4j/log4j.properties
log4j: Parsing for [root] with value=[INFO, CONSOLE].
log4j: Level token is [INFO].
log4j: Category root set to INFO
log4j: Parsing appender named "CONSOLE".
log4j: Parsing layout options for "CONSOLE".                                                                                                                                                                                                                                 
Run Code Online (Sandbox Code Playgroud)

wal*_*len 7

我知道已经有一段时间了,但我刚遇到同样的问题,所以也许我可以提供帮助.

首先,确保外部文件的名称与内部文件的名称不同log4j.xml.作为一个例子,我们将其命名log4j-tmp.xml.现在除了log4j.configuration你必须logging.config在你的命令行中定义:

java -Dlog4j.debug -Dlogging.config=file:/tmp/log4j-tmp.xml -Dlog4j.configuration=file:/tmp/log4j-tmp.xml -jar /tmp/project.jar
Run Code Online (Sandbox Code Playgroud)

你也可以复制你application.properties的jar外部(Spring Boot将搜索外部配置文件并使用它们代替jar)并logging.config=path/to/log4j-tmp.xml在其中定义,但是当你可以添加它时,没有必要为一个属性复制整个配置文件通过命令行.