Skip to content

Conversation

@carterkozak
Copy link
Contributor

This allows LoggerContext lookups to avoid searching for the calling
class to discover a classloader when the ContextSelector implementation
is declared to be agnostic to the class loader.

Custom LoggerContextFactory and ContextSelector implementations
should be updated to override the new isClassLoaderDependent method.

@carterkozak carterkozak requested a review from rgoers March 16, 2021 19:39
: null;
return anchor == null
? LogManager.getContext()
: getContext(StackLocatorUtil.getCallerClass(anchor));
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the case where the context selector is classloader dependent, I imagine we could merge the anchor = StackLocatorUtil.getCallerClass(FQCN, PACKAGE) and StackLocatorUtil.getCallerClass(anchor) calls into a single sweep, potentially cutting the work in half.

Copy link
Member

@rgoers rgoers Mar 21, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You would have to make a new method that gets the caller of the caller. It might be more efficient. In Java 8 you could continue searching from the last index. In Java 9+ you could continue processing the stream.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's what I had in mind. We could provide an overload which takes an additional integer parameter of additional frames to skip.
I'd prefer to implement that optimization separately from this change, and also implement another ContextSelector equivalent to BasicContextSelector for fully asynchronous logging so folks don't have to write their own to avoid classloader lookups.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can iterate on this PR with each component in an isolated commit, that may be easiest to review. Let me know if you think the current approach adding boolean isClassLoaderDependent() is reasonable.

Copy link
Member

@rgoers rgoers Mar 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the optimization should be done separately. I'm not exactly sure what you mean by iterating on this PR. I would suggest merging this one and then do the other work as follow-up items later. So long as what you merge works we should be fine. What you have here looks fine to me.

* Single-application instances should prefer this implementation over the {@link AsyncLoggerContextSelector}
* due the the reduced overhead avoiding classloader lookups.
*/
public class BasicAsyncLoggerContextSelector implements ContextSelector {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still need to add a commit to mention this class in the documentation.

This allows LoggerContext lookups to avoid searching for the calling
class to discover a classloader when the ContextSelector implementation
is declared to be agnostic to the class loader.

Custom LoggerContextFactory and ContextSelector implementations
should be updated to override the new `isClassLoaderDependent` method.
…r instance

Previously we walked the stack twice, once to find the
`org.slf4j.LoggerFactory` interaction, and a second time to
discover the application code which called it. Now we use a new
API to skip a frame at the end to find the final result in a single
interaction.
The BasicAsyncLoggerContextSelector is equivalent to the
AsyncLoggerContextSelector without ClassLoader introspection
and associated overhead.
@carterkozak
Copy link
Contributor Author

Merged support for isClassLoaderDependent into release-2.x and master.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants