0

I would like to add TraceId to all log lines. I do that easily by:

  1. Add traceId to MDC
MDC.put("TRACE_ID", sessionAware.getTraceId()+": ");
  1. Update file-pattern in my application.properties (by adding: "%X{TRACE_ID}"):
logging.pattern.file=-%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}} ${LOG_LEVEL_PATTERN:-%5p} ${PID:- } --- [%t] %-40.40logger{39} : %X{TRACE_ID}%m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}

But I would like my CustomSpringBootStarter to set the file pattern. But it doesn't take effect when I update property "logging.pattern.file" from my CustomSpringBootStarter. Does anyone know the solution to this problem?

I have tried to set the "logging.pattern.file" property in the CustomSpringBootStarter's application.properties but it does not work.

1 Answers1

0

I found a solution to the problem, very much inspired by this question.

I created the below EnvironmentPostProcessor in my CustomSpringBootStarter:

public class LifeLogBackConfiguration implements EnvironmentPostProcessor {
@Override
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
    Map<String, String> properties = Collections.unmodifiableMap(Map.of(
            "logging.pattern.file", LogConstants.FILE_LOG_PATTERN_LIFE,
            "logging.pattern.console", LogConstants.CONSOLE_LOG_PATTERN_LIFE));

    PropertySource propertySource = new OriginTrackedMapPropertySource("LogPatternsLife", properties, true);
    environment.getPropertySources().addLast(propertySource);
}

}

,and registered it in: resources/META-INF/spring.factories

org.springframework.boot.env.EnvironmentPostProcessor=dk.topdanmark.life.lifespringbootstarter.log.LifeLogBackConfiguration

That solved the problem for me.