logback-spring.xml中的springProperty替换在使用application.properties时有效,但在使用application.yml时无效

时间:2016-10-03 15:37:11

标签: spring-boot

我有一个logback-spring.xml文件,下面给出了一个application.properties文件。

的logback-spring.xml

<?xml version="1.0" encoding="UTF-8" ?>
<configuration>

    <springProperty scope="context" name="customFields" source="my.customFields"/>

    <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="net.logstash.logback.encoder.LogstashEncoder">
            <customFields>${customFields}</customFields>
            <providers>
                <message/>
                <loggerName>
                    <shortenedLoggerNameLength>20</shortenedLoggerNameLength>
                </loggerName>
                <logLevel/>
                <stackTrace>
                    <throwableConverter class="net.logstash.logback.stacktrace.ShortenedThrowableConverter">
                        <maxDepthPerThrowable>30</maxDepthPerThrowable>
                        <maxLength>1750</maxLength>
                        <shortenedClassNameLength>25</shortenedClassNameLength>
                        <rootCauseFirst>true</rootCauseFirst>
                    </throwableConverter>
                </stackTrace>
            </providers>
        </encoder>
    </appender>


    <root level="WARN">
        <appender-ref ref="CONSOLE"/>
    </root>

</configuration>

application.properties

my.customFields = {&#34;应用程序的名字&#34;:&#34;为MyWebService&#34;}

这正确创建包含customFields的日志记录输出:

{"@timestamp":"2016-09-30T19:32:47.828-05:00","@version":1,"message":"Closing org.springframework.web.context.support.GenericWebApplicationContext@3a4b0e5d: startup date [Fri Sep 30 19:32:45 CDT 2016]; root of context hierarchy","logger_name":"org.springframework.web.context.support.GenericWebApplicationContext","thread_name":"Thread-6","level":"INFO","level_value":20000,"HOSTNAME":"sea-szalwinb2-m.ds.ad.adp.com","customFields":"{\"appname\":\"myWebservice\"}","appname":"myWebservice"}

但是,如果我使用application.yml文件:

my:
  customFields: {"appname":"myWebservice"}

然后我得到:

Caused by: java.lang.IllegalStateException: Logback configuration error detected: 
ERROR in net.logstash.logback.composite.loggingevent.GlobalCustomFieldsJsonProvider@7ae0a9ec - Failed to parse custom fields [customFields_IS_UNDEFINED] com.fasterxml.jackson.core.JsonParseException: Unrecognized token 'customFields_IS_UNDEFINED': was expecting ('true', 'false' or 'null')
 at [Source: customFields_IS_UNDEFINED; line: 1, column: 51]

http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html#boot-features-custom-log-configuration处,第26.6.2节特别提到了application.properties,&#34;标签允许您从Spring环境中显示属性以便在Logback中使用。如果要在logback配置中访问application.properties文件中的值,这可能很有用。&#34;。引用应用程序属性的spring boot的大部分内容都与yaml等价物一起使用。此功能是异常还是我配置错误?我使用的是弹簧靴1.3.8。

1 个答案:

答案 0 :(得分:1)

我认为嵌入式JSON正在加载为YAML。尝试在值周围使用单引号:

my:
  customFields: '{"appname":"myWebservice"}'

提出类似的问题here