且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

spring boot,logback和logging.config属性

更新时间:2022-12-18 11:33:57

我找到了一个解决方案,我理解为什么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仅在类路径或环境中查找变量(参见 http://docs.spring.io/spring-boot/docs/0.5.0.M3/api/org/springframework/boot/context/initializer/LoggingApplicationContextInitializer.html )。

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.