栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Spring Boot,logback和logging.config属性

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Spring Boot,logback和logging.config属性

我找到了解决方案,并且我理解了为什么spring不使用

application.properties
文件中定义的’logging.config’属性。

解决方案和说明:

初始化日志记录时,spring boot仅查找classpath或环境变量。

我使用的解决方案是包括一个父logback.xml文件,该文件根据spring概要文件包含了正确的日志记录配置文件。

logback.xml:

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

logback- [profile] .xml (在本例中为logback-dev.xml):

<included>    <!-- put your appenders -->    <appender name="CONSOLE" >    <!-- enprers are assigned the type     ch.qos.logback.classic.enprer.PatternLayoutEnprer by default -->       <enprer><pattern>%d{ISO8601} %p %t %c{0}.%M - %m%n</pattern><charset>utf8</charset>        </enprer>    </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>

注意: 启动应用程序时,必须在命令行参数中设置“ spring.profiles.active”。EG for
JVM属性:

-Dspring.profiles.active=dev

编辑(多个活动配置文件)
:为了避免出现多个文件,我们可以使用需要Janino依赖的条件处理(在此处进行设置),请参阅条件文档。使用此方法,我们还可以同时检查多个活动配置文件。EG(我没有测试此解决方案,因此如果无法解决,请发表评论):

<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答案。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/405505.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号