【问题标题】:How do I use an external log4j.xml configuration file using spring boot?如何使用 Spring Boot 使用外部 log4j.xml 配置文件?
【发布时间】:2015-11-11 17:43:45
【问题描述】:

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

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

【问题讨论】:

标签: spring logging log4j


【解决方案1】:

对于 Spring Boot 你可以放

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

到项目中的 src/main/resources/application.properties 文件。

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

【讨论】:

    【解决方案2】:

    其实瓦伦有答案:

    代替:

    -Dlog4j.configuration=file:/tmp/log4j.xml
    

    你应该使用:

    -Dlogging.config=/tmp/log4j.xml
    

    所以命令是:

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

    【讨论】:

      【解决方案3】:

      在 SpringBootServletInitializer 中你可以拥有

      @PropertySource(value="file:${YOUR JVM ARGS OF PROP DIR}application.properties")

      上面会将应用程序属性加载到环境中。

      在 application.properties 中定义一个属性

      logging.config.file={您的 XML 配置路径}/MyLog4jCnfg.xml

      您可以使用@Value 来获取相应的属性。
      请注意,您无法在 configure() 方法中访问这些属性。

      您需要使用 @PostConstruct initAfterStartup() 来访问您的

      @Value(logging.config.file)

      【讨论】:

        【解决方案4】:

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

        首先,确保外部文件与内部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,但是复制整个配置文件没有意义当您可以通过命令行添加时,仅适用于一个属性。

        【讨论】:

        • 谢谢你,这让我发疯了没有让它工作:)
        猜你喜欢
        • 1970-01-01
        • 2016-08-19
        • 1970-01-01
        • 1970-01-01
        • 2019-12-16
        • 2017-02-24
        • 2019-02-05
        • 2020-10-15
        • 2015-03-28
        相关资源
        最近更新 更多