spring-projects / spring-projects/spring-framework

LoggingErrorHandler logs exception without trace context for exceptions in @Scheduled methods

Open
#35,364 1 comment 0 reactions 1 assignee View on GitHub

@bclozel is already working on this.

Since Aug 21, 2025.

in: core status: waiting-for-internal-feedback type: enhancement
Dominant language
Java
Stars
60.2k
Forks
38.8k
Avg merge
5d 2h
Merged PRs (30d)
27

Description

Affected versions

Spring Boot 3.5.5 and 4.0.0-M2

Environment

Java 21
Windows 11 and Linux

Summary

In a Spring Boot application with observation enabled, a @Scheduled method logs with a trace ID as expected. However, when this method throws an exception, the exception is logged by TaskUtils.LoggingErrorHandler in the same thread without including the trace ID.

This inconsistency makes it difficult to correlate scheduled task failures with their corresponding traces or with earlier logs from the same task execution.

Expected behavior

All corresponding logs from the scheduled task, within and outside the @Scheduled method, are logged consistently with the same trace ID for proper correlation and observability.

Example Logs
2025-08-21T14:16:12.183+02:00  INFO 25048 --- [   scheduling-1] [68a70e0cfc771a5ef1c53fbefe459aa4-f1c53fbefe459aa4] de.example.Scheduler                     : scheduled with trace-id: 68a70e0cfc771a5ef1c53fbefe459aa4
2025-08-21T14:16:12.186+02:00 ERROR 25048 --- [   scheduling-1] [                                                 ] o.s.s.s.TaskUtils$LoggingErrorHandler    : Unexpected error occurred in scheduled task
Minimal reproducible example
@SpringBootApplication
@EnableScheduling
public class Application {

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
}

@Component
@RequiredArgsConstructor
@Slf4j
class Scheduler {

    final Tracer tracer;

    @Scheduled(fixedRate = 10_000)
    public void scheduledWithTracer() {
        log.info("scheduled with trace-id: {}", tracer.currentTraceContext().context().traceId());
        throw new IllegalStateException("intended exception");
    }

}
Repo with fully reproducible example

https://github.com/robeatoz/scheduler-with-trace-id

If I can help you in any way, please let me know.
Thank you for your fantastic work!

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.