Search code examples
springspring-bootlogstashconfiglogback

Logback Pattern doesn't being resolved by application. Instead, var_IS_UNDEFINED


I'm trying to fix a bug with my logback-spring.xml file, where I do have a logback configuration for sending logs to logstash server, and trying to pass some additional parameters such as applicationName. Although, it seems like logback cannot resolve my application variable and just putting varName_IS_UNDEFINED.

This is my logback-spring.xml code

<property name="CONSOLE_LOG_PATTERN"
          value="%clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m | %mdc %n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}"/>
<property resource="application.yml"/>
    <include resource="org/springframework/boot/logging/logback/base.xml"/>
    <property resource="application.yml"/>

    <springProfile name="test,prod">
        <springProperty scope="system" name="swarmTaskName" source="swarm.task.name" defaultValue="N.A."/>
        <springProperty scope="system" name="logstashEndpoint" source="logstash.endpoint"
                        defaultValue="localhost:1234"/>
        <appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
            <destination>${logstashEndpoint}</destination>
            <encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
                <providers>
                    <mdc/> <!-- MDC variables on the Thread will be written as JSON fields -->
                    <context/> <!--Outputs entries from logback's context -->
                    <version/> <!-- Logstash json format version, the @version field in the output -->
                    <logLevel/>
                    <loggerName/>
                    <pattern>
                        <pattern> <!-- we can add some custom fields to be sent with all the log entries. -->
                            {      <!--make filtering easier in Logstash -->
                            "appName": "${spring.application.name}",<!--or searching with Kibana -->
                            }
                        </pattern>
                    </pattern>
                    <threadName/>
                    <message/>
                    <logstashMarkers/> <!-- Useful so we can add extra information for specific log lines as Markers -->
                    <arguments/> <!--or through StructuredArguments -->
                    <stackTrace/>
                </providers>
            </encoder>
        </appender>
    </springProfile>

<!-- Root logger writes to file, console and sends the data to Logstash -->
<root level="INFO">
    <appender-ref ref="FILE"/>
    <appender-ref ref="CONSOLE"/>
    <springProfile name="test,prod">
        <appender-ref ref="LOGSTASH"/>
    </springProfile>
</root>

<logger name="org.springframework.cache" level="DEBUG"/>
What should I do to fix this problem? Thanks!

Solution

  • use springProperty,it is dependency spring,like this :

    <springProperty scope="context" name="appname" source="spring.application.name"/>