SpringBoot外部化log4j配置 [英] Springboot externalizing log4j configuration

查看:74
本文介绍了SpringBoot外部化log4j配置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在springboot应用程序中,我有一个jar,然后是带有application.properties,applicationContext.xml和log4j ... properties文件的子目录配置.

In a springboot application, I have a single jar, and then a subdirectory config with application.properties, applicationContext.xml, and log4j...properties file.

我正在尝试将log4j配置外部化.通过这种方式将application.properties外部化.

I am trying to externalize the log4j config. The application.properties is externalized this way.

但是,当springboot运行时,它使用jar文件中的log4j配置文件.通过使用-Dlog4j.debug选项,我可以看到log4j首先使用我的外部文件,但是当spring启动时,它将覆盖jar中的log4j设置.

But, when springboot runs it is using the log4j config file from within the jar file. By using the -Dlog4j.debug option I can see that log4j uses my external file first, but when spring starts it overrides the log4j setting with the one in the jar.

这是启动示例(包含所有选项)

here is an example startup (with all options)

java -Dlog4j.debug 
   -Dlogging.config="file:/opt/config/log4j-qa.properties" 
   -Dlog4j.configuration="file:/opt/config/log4j-qa.properties" 
   -jar /opt/myjarName.jar 
   --spring.config.location=/opt/config/

在首次启动时出现log4j状态log4j:从URL文件读取配置:/opt/config/log4j-qa.properties

on first startup log4j states log4j: Reading configuration from URL file:/opt/config/log4j-qa.properties

然后在springboot上启动log4j:从URL jar中读取配置:file:/opt/dms-events-api.jar!/log4j-qa.properties

then on springboot start log4j: Reading configuration from URL jar:file:/opt/dms-events-api.jar!/log4j-qa.properties

但是我希望它只读取外部文件:/opt/config/log4j-qa.properties

but I want it to read only the external file file:/opt/config/log4j-qa.properties

推荐答案

有两个问题:

  1. 用于外部化的配置:-在Spring Boot jar中尝试并在低于1的水平下工作

  1. Configuration for externalise: - Tried and works below one for me in spring boot jar

-Dlog4j.configuration=file:/Users/test/Any-Folder/log4j.properties

  • Spring日志记录接管了-为此,您需要排除日志记录模块.PFB Gradle 构建的配置.

  • Spring logging takes over - for that you need to exclude the logging module. PFB the config for Gradle build.

    configurations {
         all*.exclude module : 'spring-boot-starter-logging'   
    }
    

  • 这篇关于SpringBoot外部化log4j配置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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