Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add threadId to PatternLayout #710

Closed
rob93c opened this issue Oct 9, 2023 · 5 comments · May be fixed by #711
Closed

Add threadId to PatternLayout #710

rob93c opened this issue Oct 9, 2023 · 5 comments · May be fixed by #711
Assignees
Labels
Milestone

Comments

@rob93c
Copy link
Contributor

rob93c commented Oct 9, 2023

Given the project already supports printing the current thread name, it should also be able to retrieve the threadId.

This would be useful when dealing with virtual threads, I noticed that ones created via Executors.newVirtualThreadPerTaskExecutor() don't have a name and they would be shown as an empty string:

image

@danishnawab
Copy link

Unlike platform threads, virtual threads do not return a name unless explicitly set. From the javadoc:

Virtual threads do not have a thread name by default. The getName method returns the empty string if a thread name is not set.

Given that libraries and frameworks (e.g. Spring) create virtual threads but don't explicitly set a name, the logs don't contain any thread name which affects the troubleshooting experience.
The ability to include threadIds in logback log messages would help deal with this problem.

Another solution could perhaps be to synthesize a thread name, something like:

public String getThreadName() {
    if (this.threadName == null) {
        final Thread currentThread = Thread.currentThread();
        if (currentThread.getName() != null) {
            this.threadName = currentThread.getName();
        } else if (currentThread.isVirtual()) { // replace with the reflective call when building with older JDKs
            this.threadName = "virtual-thread-" + currentThread.threadId();
        } else {
            this.threadName = "unnamed-" + currentThread.threadId();
        }
    }

    return this.threadName;
}

@ceki any thoughts on this?

@ceki
Copy link
Member

ceki commented Feb 2, 2024

@danishnawab Thank you for the proposal. See commit 5a36133

@danishnawab
Copy link

That's amazing @ceki, thank you!

@getaceres
Copy link

It seems that the build failed for some (apparently) temporary errors. Could this be retried? It would be very useful to have this now that Spring Boot has support for virtual threads.

@mkurz
Copy link
Contributor

mkurz commented Feb 26, 2024

This is part of 1.5 now. I think this can be closed?

@ceki ceki self-assigned this Feb 26, 2024
@ceki ceki added the DONE label Feb 26, 2024
@ceki ceki added this to the 1.5.0 milestone Feb 26, 2024
@ceki ceki closed this as completed Feb 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants