Skip to content

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

@robeatoz

Description

@robeatoz

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!

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)theme: observabilityAn issue related to observability and tracingtype: enhancementA general enhancement

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions