Spring-cloud-sleuth: Spring App Name is not resolved properly

Created on 8 Jul 2019  路  7Comments  路  Source: spring-cloud/spring-cloud-sleuth

Having next logback custom config:
`

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

<property name="CONSOLE_LOG_PATTERN" value="${spring.application.name} ${LOG_LEVEL_PATTERN} %n"/>

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
    <encoder>
        <pattern>${CONSOLE_LOG_PATTERN}</pattern>
        <charset>utf8</charset>
    </encoder>
</appender>



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

`

I got next in console:
cm DEBUG [-,,,]

So, as you can see application name is taken from spring config correctly, but TraceEnvironmentPostProcessor generetes wrong format for log level pattern in situation when Zipkin service name is absent but Spring App Name is present.
map.put("logging.pattern.level", "%5p [${spring.zipkin.service.name:" + "${spring.application.name:-}},%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-Span-Export:-}]");
https://github.com/spring-cloud/spring-cloud-sleuth/blob/e6b78d267fca2d7dfe5e19e571dc30dff7b00bd4/spring-cloud-sleuth-core/src/main/java/org/springframework/cloud/sleuth/autoconfig/TraceEnvironmentPostProcessor.java#L51

You missed - after spring.zipkin.service.name: So, correct format is:
%5p [${spring.zipkin.service.name:-${spring.application.name:-}},%X{X-B3-TraceId:-},%X{X-B3-SpanId:-},%X{X-Span-Export:-}]

P.S. Truly speaking, I don't understand why you did implement that post processor. Why should people use formatting like that? I think it is better to pass variables to MDC as it happens right now and let people configure logging as they want.

bug

Most helpful comment

so, still use bootstrap.yml.? I use latest deps now. and it ignores the application.yaml it reads the application name from the bootstrap.yml.

 id 'org.springframework.boot' version '2.2.5.RELEASE'
   implementation("org.springframework.cloud:spring-cloud-starter-sleuth:2.2.2.RELEASE")

All 7 comments

Do you have a small sample that reproduces the issue?

P.S. Truly speaking, I don't understand why you did implement that post processor. Why should people use formatting like that? I think it is better to pass variables to MDC as it happens right now and let people configure logging as they want.

Because it's proper, default formatting. It's set as default, so if you set your own logging.pattern.level to sth else we will not override it.

@marcingrzejszczak, just put logback config I copied here in in any spring boot app example.

@marcingrzejszczak, I don't know why, but it takes value from bootstrap.yml. Just remove it and you will see the issue. I have simple spring boot applicaiton, we don't have bootstrap.yml and don't need it. And the reason why it doesn't work - you have broken format string. Please read carefully Logback manual about MDC variables:

If the value is null, then the default value specified after the :- operator is output

Operator is :- not : . This is wrong, please, fix it.

Thanks @apolischuk for your kind comment. I will take a look into it once the issue is triaged and prioritized. If you have spare time please be my guest to apply a fix on a pull request, preferably with a test that would break with the current implementation.

Thanks for your response @marcingrzejszczak , Not sure how to write test here... This is input data for Logback, so I assume this is the correct place for tests and I'm sure they have it ))). If you remove bootstrap.yml or just imagine that developers don't configure service name at all, you will see [-,,,]. Have you ever wondered why we have dash here? Thats how Logback resolves it. If everything was fine we would see [,,,] output.

so, still use bootstrap.yml.? I use latest deps now. and it ignores the application.yaml it reads the application name from the bootstrap.yml.

 id 'org.springframework.boot' version '2.2.5.RELEASE'
   implementation("org.springframework.cloud:spring-cloud-starter-sleuth:2.2.2.RELEASE")
Was this page helpful?
0 / 5 - 0 ratings