Spring boot Static inner class not initialised when application starts as a jar
Asked Answered
C

1

6

When I start up the my spring boot application via intellij, the following class initialised properly and the log can be seen. but if I run agains the jar from the build, MyCurrentTraceContext seem not initialised and I can't see the log in the output either. I do need this class with my customised logic to run put of some argument into MDC. Any advice?

@Configuration
@ConditionalOnProperty(value="spring.sleuth.enabled", matchIfMissing=true)
@AutoConfigureBefore(TraceAutoConfiguration.class)
public class MyLogConfiguration extends SleuthLogAutoConfiguration {
    private static final Logger LOGGER = (Logger) LoggerFactory.getLogger(WtrLogConfiguration.class);

    @Configuration
    @ConditionalOnClass(MDC.class)
    @EnableConfigurationProperties(SleuthSlf4jProperties.class)
    protected static class MySlf4jConfiguration extends Slf4jConfiguration {

        @Bean
        @ConditionalOnProperty(value = "spring.sleuth.log.slf4j.enabled", matchIfMissing = true)
        @ConditionalOnMissingBean
        @Override
        public CurrentTraceContext slf4jSpanLogger() {
            LOGGER.info("************ OVER WRITTING WTIH WtrCurrentTraceContext*******");
            return new MyCurrentTraceContext(Slf4jCurrentTraceContext.create());
        }
    }
}
Cryptology answered 23/8, 2018 at 22:51 Comment(7)
you need to give some more detailsDiseuse
Basically, when start the spring boot using java -jar xxx.jar. It never runs into public CurrentTraceContext slf4jSpanLogger(){}, but it does when start the spring boot via intellij (start via the main method in the springboot class)Cryptology
I see these ConditionalOnProperty. Are you setting system properties or environment variables or program arguments in Intellij that you don't set when you run the jar?Gulp
@Simon Martinelli, I don't have this value in set anywhere. but since it has been set "matchIfMissing=true" in the annotation, I assume there is not need to set in other place?Cryptology
It is very strange that I can see the logs showing follow. but not sure the static class is not initialised. "definition for bean 'slf4jSpanLoggerBPP' with a different definition: replacing org.springframework.cloud.sleuth.log.SleuthLogAutoConfiguration$Slf4jConfiguration with xxxx/MyLogConfiguration$MySlf4jConfiguration.classCryptology
It should run normally. You definitely set your values somewhereCementum
do you have a different profile or any run-time arguments set when running jar???Tusk
C
0

Please check with

@ComponentScan

in top of your starter class to set the path you need to be scanned while project starts. In your case that would be :

@ComponentScan("myPackagePath.MyLogConfiguration.MySlf4jConfiguration.*")

Craven answered 29/8, 2018 at 11:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.