本文介绍了spring boot,logback和logging.config属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用logback库实现了一个spring boot项目的日志记录。我想根据我的spring配置文件(属性'spring.pofiles.active')加载不同的日志配置文件。我有3个文件:logback-dev.xml,logback-inte.xml和logback-prod.xml。我使用的是春季启动版本1.2.2.RELEASE。

I am implement logging in a spring boot project with logback library. I want to load different logging configuration files according to my spring profiles (property 'spring.pofiles.active'). I have 3 files : logback-dev.xml, logback-inte.xml and logback-prod.xml. I am using spring boot version 1.2.2.RELEASE.

正如您可以阅读spring boot文档()。它说:

As you can read in spring boot documentation (here). It says:

所以我尝试在application.properties文件中设置'logging.config'属性:

So I tried to set 'logging.config' property in my application.properties file:

logging.config=classpath:/logback-${spring.profiles.active}.xml

但是,当我启动我的应用程序时,我的logback- {profile} .xml未加载...

But when i start my application, my logback-{profile}.xml is not loaded...

我认为日志记录是一个所有使用spring boot的项目遇到的常见问题。我想知道我是否在正确的方向,因为我有其他解决方案,但我发现它们不优雅(在logback.xml文件或命令行属性中使用Janino进行条件解析)。

I think logging is a common problem that all projects using spring boot have encountered. I want to know if I am in the right direction or not because I have other solutions that works too but i find them not elegant (conditional parsing with Janino in logback.xml file or command line property).

推荐答案

我找到了一个解决方案,我理解为什么spring不关心application.properties文件中定义的'logging.config'属性。

I found a solution and I understood why spring doesn't take care about my 'logging.config' property defined in application.properties file.

解决方案和解释:

初始化日志记录时,spring boot仅在类路径或环境中查找变量(参见)。

When initialize logging, spring boot only looks in classpath or environments variables ( see http://docs.spring.io/spring-boot/docs/0.5.0.M3/api/org/springframework/boot/context/initializer/LoggingApplicationContextInitializer.html).

我找到的最佳解决方案是包含一个父logback.xml文件,该文件将根据我的春季配置文件包含正确的日志配置文件。

The best solution I found is to inclued a parent logback.xml file that will included the right logging config file according to my spring profile.

logback.xml:

<configuration>
    <include resource="logback-${spring.profiles.active}.xml"/>
</configuration>

logback- [profile] .xml (在这种情况下,logback- dev.xml):

logback-[profile].xml (in this case, logback-dev.xml) :

<included>

    <!-- put your appenders -->
    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <!-- encoders are assigned the type
     ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
       <encoder>
           <pattern>%d{ISO8601} %p %t %c{0}.%M - %m%n</pattern>
           <charset>utf8</charset>
        </encoder>
    </appender>

    <!-- put your loggers here -->
    <logger name="org.springframework.web" additivity="false" level="INFO">
        <appender-ref ref="CONSOLE" />
    </logger>

    <!-- put your root here -->
    <root level="warn">
        <appender-ref ref="CONSOLE" />
    </root>

</included>

注意:
'prring.profiles.active'有在启动应用程序时在命令行参数中设置。
EG for JVM属性: -Dspring.profiles.active = dev

参考文档:





  • http://docs.spring.io/spring-boot/docs/current/reference/html/howto-logging.html
  • http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html
  • http://docs.spring.io/spring-boot/docs/0.5.0.M3/api/org/springframework/boot/context/initializer/LoggingApplicationContextInitializer.html

编辑(多个活动配置文件)
为了避免多重文件,我们可以使用需要Janino依赖的条件处理(),参见。
使用此方法,我们还可以同时检查多个活动配置文件。 EG(我没有测试这个解决方案,如果它不起作用就发表评论):

Edit (multiple active profiles) :In order to avoid multiple files, we could use conditional processing which requires Janino dependency (setup here), see conditional documentation.With this method, we can also check for multiple active profiles at the same time. E.G (I did not test this solution, put comment if it does not work):

<configuration>

    <if condition='"${spring.profiles.active}".contains("profile1")'>
        <then>
         <!-- do whatever you want for profile1 -->
        </then>
    </if>

    <if condition='"${spring.profiles.active}".contains("profile2")'>
        <then>
         <!-- do whatever you want for profile2 -->
        </then>
    </if>

    <!-- common config -->

</configuration>

有关条件处理的另一个例子,请参阅javasenior答案。

See javasenior answer for another example of conditional processing.

这篇关于spring boot,logback和logging.config属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-18 23:10