当前位置: 首页 > 知识库问答 >
问题:

spring boot、logback和logging.config属性

邢献
2023-03-14

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

您可以在Spring Boot文档(这里)中读到。上面写着:

可以通过在类路径上包含适当的库来激活各种日志记录系统,并通过在类路径的根目录中或Spring环境属性logging.config指定的位置提供适当的配置文件来进一步定制这些日志记录系统。(但是请注意,由于日志记录是在创建ApplicationContext之前初始化的,所以不可能从Spring@配置文件中的@PropertySources控制日志记录。系统属性和常规的Spring Boot外部配置文件可以正常工作。)

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

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

我认为日志记录是所有使用spring boot的项目都遇到过的常见问题。我的上述方法是否正确?我有其他可行的解决方案,但我发现它们没有那么优雅(在logback.xml文件或命令行属性中使用Janino进行条件解析)。

共有1个答案

夹谷浩宕
2023-03-14

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

解决方案及说明:

当初始化日志记录时,spring Boot只查找类路径或环境变量。

<configuration>
    <include resource="logback-${spring.profiles.active}.xml"/>
</configuration>
<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>

参考单据:

  • 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依赖(这里的设置),请参阅条件文档。通过这种方法,我们还可以同时检查多个活动配置文件。例如(我没有测试这个解决方案,所以如果它不起作用请评论):

<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>
 类似资料:
  • 问题内容: 我正在使用logback库在spring boot项目中实现日志记录。我想根据我的spring配置文件(属性“ spring.pofiles.active”)加载不同的日志记录配置文件。我有3个文件:logback-dev.xml,logback-inte.xml和logback-prod.xml。我正在使用Spring Boot版本1.2.2.RELEASE。 如你在Spring B

  • 问题内容: 我正在使用logback库在spring boot项目中实现日志记录。我想根据我的spring配置文件(属性“ spring.pofiles.active”)加载不同的日志记录配置文件。我有3个文件:logback-dev.xml,logback- inte.xml和logback-prod.xml。我正在使用Spring Boot版本1.2.2.RELEASE。 如您在Spring

  • 因此,登录不能解析BuferredIO和ImmediateFlush属性。我试图查看他们的文档,但它似乎过时了,因为它说这些属性存在+我发现,例如对于,它没有属性,而根据文档,它应该有。 我找不到任何信息,如果登录仍然支持以下属性现在。你能帮助和建议一些想法,我如何才能达到相同的结果,与以前的财产?也许他们在别的地方:) 非常感谢!:) Logback版本为1.1.7(在迁移之前)

  • 我们的软件使用api(filenet p8),需要配置log4j。我们使用logBack和Spring Boot。我注意到,要在Spring Boot中使用log4j,我们必须排除logBack。这是不可能的。有没有办法在Spring Boot中并行运行log4j和logBack?谢啦

  • 我在使用springboot 1.5.4时遇到了两个问题,我无法解决它们。 问题 1. 我在src/main/resources中配置了logback-spring.xml,当spring boot启动时,将创建两个目录,一个以“应用程序名”启动,另一个以bootstrap启动.. 我很困惑为什么创建bootstrap日志文件目录,spring boot创建了两个日志目录,顺便说一下,spring

  • 嗨,我想在Spring Boot应用程序中使用。我想从文件检索数据库连接属性。然而,它似乎并不认识它们。请记住,我使用的是,所以还不能使用。 我使用的配置如下: