在logback-spring.xml中使用properties文件中的属性

- springProfile

该 <springProfile> 标签允许我们更加灵活配置文件,可选地包含或排除配置部分。元素中的任何位置均支持轮廓部分。使用该name属性指定哪个配置文件接受配置。可以使用逗号分隔列表指定多个配置文件。

在logback-spring.xml中
<springProfile name="dev">
    <!-- 开发环境时激活 -->
</springProfile>

<springProfile name="dev,test">
    <!-- 开发,测试的时候激活-->
</springProfile>

<springProfile name="!prod">
    <!-- 当 "生产" 环境时,该配置不激活-->
</springProfile>
在properties中使用 激活
spring.profiles.active=dev

- springProperty

1.该 <springProperty> 标签允许我们从Spring中显示属性,Environment 以便在Logback中使用。如果你想将 application.properties在回读配置中访问文件中的值,这将非常有用

2.标签的工作方式与Logback的标准 <property> 标签类似,但不是直接value 指定source属性(从Environment)指定。scope 如果需要将属性存储在local范围之外的其他位置,则可以使用该属性。如果您需要一个后备值,以防该属性未设置,则Environment可以使用该defaultValue属性。

properties中的属性

logPath=F:\\webbank\\log
exception-location=webbankError
<!-- 读取spring.application.name中的属性来生成日志文件名 -->

    <springProperty scope="context" name="LOG_HOME" source="logPath"/>
    <springProperty scope="context" name="EXCEPTION_LOCATION" source="exception-location"/>

    <!--打印到控制台-->
    <appender name="consoleLog"
              class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>
                %d [%thread] %-5level %logger{50}- %msg%n
            </pattern>
        </layout>
    </appender>

    <!-- 配置info日志 -->
    <appender name="fileInfoLog"
              class="ch.qos.logback.core.rolling.RollingFileAppender">
        <filter class="ch.qos.logback.classic.filter.LevelFilter">
            <level>ERROR</level>
            <onMatch>DENY</onMatch>
            <onMismatch>ACCEPT</onMismatch>
        </filter>
        <encoder>
            <pattern>
                %d [%thread] %-5level %logger{50}- %msg%n
            </pattern>
        </encoder>
        <!-- 设置滚动策略 -->
        <rollingPolicy
                class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- 路径 -->
            <fileNamePattern>
                ${LOG_HOME}/info.%d.log
            </fileNamePattern>
            <!--文件保留的天数-->
            <MaxHistory>365</MaxHistory>
        </rollingPolicy>
    </appender>
发布了92 篇原创文章 · 获赞 92 · 访问量 20万+

猜你喜欢

转载自blog.csdn.net/qq_34359363/article/details/104749341