0

I have a logback-spring.xml file given and an application.properties file given below.

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={"appname":"myWebservice"}

This correctly creates logging output that contains the 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"}

However, if I use application.yml file:

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

Then I get:

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]

Over at http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-logging.html#boot-features-custom-log-configuration, section 26.6.2 specificly mentions application.properties, "The tag allows you to surface properties from the Spring Environment for use within Logback. This can be useful if you want to access values from your application.properties file in your logback configuration.". Most part of spring boot that reference application properties work with yaml equivalents. Is this feature an exception or have I misconfigured something? I'm using spring boot 1.3.8.

bruce szalwinski
  • 724
  • 1
  • 8
  • 27

1 Answers1

1

I think the embedded JSON is being loaded as YAML. Try using single quotes around the value:

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

A similar question was asked here

Community
  • 1
  • 1
Phil Webb
  • 8,119
  • 1
  • 37
  • 37