0

I have few classes that i use for my spring batch job execution like one below. I want to append a constant string SPRING_BATCH_JOB to the names of loggers of all these classes so that it will make my log filtering easier. I tried passing topic to @Slf4j annotation (@Slf4j(topic = "SPRING_BATCH_LOG") ). It will just print this constant literal and will not print the class name. That is not exactly what i want to achieve.

For an instance, I want to print the log statement log.info("Creating a retry job execution") as {"@timestamp":"2023-03-21T05:34:24.345+00:00","message":"Creating a retry job execution","logger":"SPRING_BATCH_LOG : c.v.r.r.c. RetryOb","thread":"http-nio-8086-exec-2","level":"INFO"}.

Is there any way to achieve this ???

@Slf4j
@Service
@RequiredArgsConstructor
public class RetryOb {

  private final RetryJobService retryJobService;

  public void retry(retryRequest retryRequest) {
    log.info("Creating a retry Initiate job execution");
    retryService.startJob(retryRequest);
  }
}

1 Answer 1

0

Yeah, you can use MDC, see Baeldung's article.

Not all slf4j's binder/loggers support this feature. As far as I know logback one who supports it => Chapter 8: Mapped Diagnostic Context

If you want to suppress a class name tune a custom layout.

To get JSON output try to use logback's JSON appender.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.