Comments (5)
At the very least, passing "null" to withTags()
/ withLogLevelMap()
should be a synonym for passing Tags.empty()
/ LogLevelMap.create(OFF)
to avoid accidentally allowing methods to (sometimes) be callable twice.
And someone would need to check that passing LogLevelMap.create(OFF)
is properly short-circuited when contexts are merged.
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);
}
from flogger.
My recommendation is:
- Revert the change to
withTags()
and document thatTags.empty()
exists. - Add a
LogLevelMap.noOp()
singleton getter method and document that inwithLogLevelMap()
- Add a
isNoOp()
method inLogLevelMap
and test that when merging maps and (importantly) when deciding whether to flip the static flag which makes every log statement slower.
I.e. change:
https://github.com/google/flogger/blob/master/grpc/src/main/java/com/google/common/flogger/grpc/GrpcContextData.java#L139
to:
if (logLevelMap != null && !logLevelMap.isNoOp()) ...
from flogger.
Thanks for catching this. I was initially dubious about allowing null
like this until I saw that there were in fact other methods in the same class doing that, but I hadn't thought about Tags.empty()
etc. as an alternative. I'll check with the person who sent the change about whether there's some particular reason that wouldn't work for them, but I imagine we can roll this back and make the changes you describe.
from flogger.
82362ff changes withTags
back to requiring a non-null
argument and makes withLogLevelMap
once again not callable twice.
null
is currently still allowed for the LogLevelMap
, in part because as you point out it's important to avoid using a LogLevelMap
at all when it isn't needed, even if it's a no-op instance, and those code paths are already handled by null
. Having to check for both null
and isNoOp()
feels a bit awkward and error-prone (though we could mitigate that some with a helper method).
I could also potentially see doing something like adding a no-op LogLevelMap
, but only checking it in withLogLevelMap
and translating it to null
immediately (while still disallowing a null
argument and not allowing it to be called twice).
from flogger.
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.
from flogger.
Related Issues (20)
- Architecture diagram for flogger
- Extensibility How to HOT 9
- Better document Flogger's status (active, maintained, in use at Google) HOT 5
- Support for java.util.ResourceBundle HOT 9
- Add tags from thread context to log HOT 17
- Use java.time.Duration for atMostEvery in LoggingApi. HOT 2
- Flag --incompatible_disable_starlark_host_transitions will break Flogger in Bazel 7.0 HOT 6
- Release schedule?
- Default JavaDocs generated for Flogger confusingly suggest it's deprecated (it isn't). HOT 2
- How to handle ScopedLoggingContext for new Threads / ThreadPools? HOT 1
- Provide a log writer to produce JSON log items according to Elastic Common Schema HOT 8
- Incorrect caller details logged with Flogger in spring boot HOT 2
- Error: 'JavaInfo' value has no field or method 'transitive_deps' HOT 5
- Error: 'JavaInfo' value has no field or method 'transitive_deps' HOT 5
- Next release HOT 2
- ThreadlocalRandom instead of ThreadLocal HOT 8
- java failed error executing Javac command from target //api:checks HOT 5
- test
- `LogData.getTemplateContext()` should be `@NullableDecl`
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from flogger.