如何在Spring Boot中使用外部的log4j.xml配置文件?

3

我创建了一个Spring Boot项目,并希望使用外部的log4j.xml配置文件与我的jar包一起使用。我使用的命令行如下:

java -Dlog4j.debug -Dlog4j.configuration=file:/tmp/log4j.xml -jar /tmp/project.jar

从调试信息来看,它似乎正确加载了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".                                                                                                                                                                                                                                 

请看这里 => https://dev59.com/l4Lba4cB1Zd3GeqPYwQb - floh.mueller
可能是Spring Boot log4j文件外部到jar?的重复问题。 - mattm
4个回答

12

我知道已经有一段时间了,但我刚刚遇到了相同的问题,也许我可以帮忙。

首先,请确保外部文件的名称与内部的 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

你也可以将你的application.properties文件复制到jar包外面(Spring Boot会搜索外部配置文件并使用它们,而不是jar包内的配置文件),并在其中定义logging.config=path/to/log4j-tmp.xml,但是仅为了一个属性而复制整个配置文件没有意义,因为你可以通过命令行添加它。


谢谢你,这让我疯狂了很久,一直没能让它工作 :) - Sverrir Sigmundarson

2
实际上,Walen已经给出了答案:
而不是:
-Dlog4j.configuration=file:/tmp/log4j.xml

你应该使用:

-Dlogging.config=/tmp/log4j.xml

因此,命令是:

java -Dlog4j.debug -Dlogging.config=file:/tmp/log4j.xml -jar /tmp/project.jar

2

对于Spring Boot,您可以放置

logging.config=${user.home}/projectName/log4j.xml

将以下内容添加到您的项目中的src/main/resources/application.properties文件中:

其中${user.home}是通配符。在Linux中,它指向/home/user/,而在Windows环境中则指向C:\Users\user目录。此配置参数同样适用于Spring Boot默认记录器Logback。


-1
在SpringBootServletInitializer中,您可以使用以下代码将应用程序属性加载到环境中:
@PropertySource(value="file:${YOUR JVM ARGS OF PROP DIR}application.properties")
在application.properties文件中定义一个属性: logging.config.file={YOUR PATH TO XML CONFIG}/MyLog4jCnfg.xml
您可以使用@Value注释获取相应的属性。请注意,您无法在configure()方法中访问这些属性。
您需要使用@PostConstruct initAfterStartup()来访问您的属性。

@Value(logging.config.file)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接