如何在application.yaml中设置logback.xml属性

时间:2017-03-07 12:21:50

标签: spring logging spring-boot yaml logback

我有logback.xml这样:

    <?xml version="1.0" encoding="UTF-8"?>
        <configuration scan="true" scanPeriod="2 seconds">

            <include resource="org/springframework/boot/logging/logback/base.xml"/>
            <jmxConfigurator/>

            <logger name="org.springframework" level="INFO"/>
            <logger name="org.springframework.web.filter.CommonsRequestLoggingFilter" level="DEBUG"/>
            <logger name="com.mypackage" level="WARN"/>

            <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
                <encoder>
                    <pattern>
                        %d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
                    </pattern>
                </encoder>
            </appender>

            <turboFilter class="ch.qos.logback.classic.turbo.DynamicThresholdFilter">
                <Key>dynamicLogLevel</Key>
                <DefaultThreshold>DEBUG</DefaultThreshold>
                <MDCValueLevelPair>
                    <value>dynamicLogLevelDEBUG</value>
                    <level>DEBUG</level>
                </MDCValueLevelPair>
                <!-- dynamicLogLevelERROR for testing -->
                <MDCValueLevelPair>
                    <value>dynamicLogLevelERROR</value>
                    <level>ERROR</level>
                </MDCValueLevelPair>
            </turboFilter>

 .....
</configuration>

我想设置logback.xmlapplication.yaml中存在的记录器级别,修补程序,滚动策略。

我已经完成了Doc,但没有找到足够的信息。

我正在使用spring-boot 1.4.4-RELEASE

3 个答案:

答案 0 :(得分:6)

springProperty标签可能就是您所需要的。 Spring Boot将自动过滤配置文件中的source属性,并使用属性中的相应值替换占位符。

<configuration scan="true" scanPeriod="2 seconds">
    <springProperty name="yourVar" source="key.of.your.yaml.property"/>

    ...${yourVar}...

您可以看到the details in the GitHub repository

答案 1 :(得分:3)

我找到了解决方案: 我的public class MediaStuff { private bool _closing = false; public void PlayAudio(string audioFilePath) { var thread = new Thread(() => { PlayAudioThreadProc(audioFilePath); }); thread.Start(); } private void PlayAudioThreadProc(string audioFilePath) { MediaPlayer mediaPlayer = CreateMediaPlayer(audioFilePath); mediaPlayer.Play(); while (!_closing) { System.Threading.Thread.Sleep(10); Dispatcher.Run(); } mediaPlayer.Stop(); mediaPlayer.MediaEnded -= MediaPlayer_MediaEnded; mediaPlayer.Close(); } private MediaPlayer CreateMediaPlayer(string audioFilePath) { var mediaPlayer = new MediaPlayer(); mediaPlayer.MediaEnded += MediaPlayer_MediaEnded; //This event is not getting fired. mediaPlayer.Open(new Uri(Path.GetFullPath(audioFilePath))); return mediaPlayer; } private void MediaPlayer_MediaEnded(object sender, EventArgs e) { //This part of code is supposed to start the media again after it is ended playing. var player = (MediaPlayer)sender; player.Position = TimeSpan.Zero; player.Play(); } public void Stop() { _closing = true; } } 位于某个文件夹中(例如application.yamlC:/configuration/application.yaml)。

c:/configuration/logback.xml中,我们可以使用以下方式访问logback.xml内容:

application.yaml

for ex:in <property file="${HOME}/configuration/application.yaml"/>

application.yaml

在logback.xml中访问它:

LOG_FILE : C:/logs

但是在

中处理值scanPeriod
<appender name="MY_APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${LOG_FILE}</file>

...

</appender>

希望它会有所帮助。

答案 2 :(得分:-1)

logging:
  level:
    org.springframework.web: ERROR
    com.pack.test: INFO
  pattern:
    file: "%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n"
  file: <logPath>/test.log