-
Notifications
You must be signed in to change notification settings - Fork 123
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
I don't believe ScopedLoggingContexts should obviously accept null "no-op" arguments. #358
Comments
At the very least, passing "null" to And someone would need to check that passing EDIT: I just checked and it looks like it is NOT handled well. Having the no-op log level map pushes you into a non-optimal code path for every log statement. I had always envisaged code which want to add a log level map would look something like: DebugLevel dbgLevel = getDebugLevelFrom(request);
if (dbgLevel != null) {
LogLevelMap levelMap = getLevelMap(dbg);
Tags tags = Tags.of("request_id", getRequestIdFrom(request));
try (var context = ScopedLoggingContexts.newContext().withLogLevelMap(levelMap).withTags(tags).install()) {
logger.atFine().log("Enabling targeted debug logging (level=%s)", dbgLevel);
filterChain.doFilter(request, response);
}
} else {
// No debug level given, so no additional logging and no context created.
filterChain.doFilter(request, response);
} |
My recommendation is:
I.e. change: if (logLevelMap != null && !logLevelMap.isNoOp()) ... |
Thanks for catching this. I was initially dubious about allowing |
82362ff changes
I could also potentially see doing something like adding a no-op |
I'd still be interested to know where someone has a dynamically set level map which might be "no level map". I don't disbelieve it's possible but I did think about that use case and it always seemed better to just not create a context at all in those cases. |
See:
5aa0649#commitcomment-124981853
The problem with the commit which added this is that now you can pass in null (perhaps via a variable) and get unexpected runtime failures when building a logging context.
Will fail at runtime if both tags1 and tags2 are non zero (the API only lets you see one tags instance). However if tags1 is null, the code will not throw an exception.
When I designed this API I had explicitly made it so that only one call withTags/withLogLevel was allowed (this encourages people to pull together they data they want to add before making the call). With the new change, this breaks that behaviour.
Tags.empty()
exists.create(Level.OFF)
which could be pulled out into a static field).Having these methods accept null (and having that create different behaviour to passing the non-null no-op instance) is potentially confusing to users.
Only
withMetadata()
(which is defined to be able to be called more than once) arguably needs to handle null input, and that should just behave in exactly the same way aswith()
in the logging API.The text was updated successfully, but these errors were encountered: