Conversation
Add example-log4j2, showing how a server interceptor can put per-call values into the Log4j 2 ThreadContext so that they are appended to every log statement the service makes. This is the non-io.grpc.Context scoped case, since logging frameworks read from thread-local storage. The server uses LogManager plus a log4j2.xml whose pattern includes %X, rather than constructing an internal SimpleLogger by hand, so the contextual values are actually visible in the output. Supersedes grpc#5835, which was reviewed but went stale. Relative to that change: the directory is example-log4j2 rather than examples-log4j2 so that the loop over examples/example-* in buildscripts/kokoro/unix.sh picks it up; the BUILD.bazel copied from the ALTS example and the vendored gradlew files are dropped; the README explains what the example illustrates and names the scripts the build actually produces; and the gRPC, protoc and Log4j 2 versions are current.
Establish the ThreadContext around next.startCall() as well as in the listener callbacks, so that interceptors further down the chain see the values while their own interceptCall() runs. This was raised on grpc#5835 and left unresolved there. Also resolve the client's host name once instead of on every RPC, bump Log4j 2 to 2.26.1, and correct the log4j2.xml comment, which described a bare %X rather than the explicit keys the pattern actually uses.
AgraVator
marked this pull request as ready for review
September 14, 2026 09:55
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds
example-log4j2, showing how a server interceptor can put per-call values into the Log4j 2ThreadContextso they end up on every log line the service writes. Logging frameworks read from thread-local storage rather thanio.grpc.Context, so the interceptor has to set the values on whichever thread runs each callback.This revives #5835 by @carl-mastrangelo, which was reviewed but never merged; the example is his. It picks up the review comments that were left open there: the directory is named
example-log4j2so the Kokoro loop finds it, theBUILD.bazelcopied from the ALTS example and the vendored gradle wrapper are gone, the README says what the example is for and names the right scripts, and the versions are current.It also sets the context around
next.startCall(), not just in the listener callbacks, so interceptors further down the chain see it too. That was the gap @Nipel-Crumple raised on #5835.The server uses
LogManagerand alog4j2.xmlwith%Xin the pattern instead of building aSimpleLoggerby hand, since that's what a reader would actually copy.