Comments (5)
Wow, thanks for looking at this.
Inside Google there's a quite sophisticated (but not error-prone) mechanism for doing migrations, and while the code itself is a bit hard to open-source, it does solve a lot of common problems (we decided to not just migrate people's log statements, but to canonicalize them in the process since there were so cases of "bad" or at least "questionable" usage).
e.g. Old style JDK log statements like:
Foo foo = ...;
int bar = ...;
logger.info("foo={0}, bar={1}", new Object[] { foo, bar })
would be fully rewritten to:
logger.atInfo().log("foo=%s, bar=%d", foo, bar);
And common errors (e.g. bad escaping or wrong number of placeholders) would be detected and, in some cases, automatically fixed. I'm happy to share the sort of checks and transformations we're doing if it might help you.
There are also some internal error-prone checks for Flogger correctness which we might be able to open-source if we haven't already (ensuring that once people have migrated, they conform to best practice).
from flogger.
Would welcome any insight into checks / transformations - thanks!
So far have managed to canonicalize various source APIs, such as:
someLogger.error("test message");
someLogger.error(DummyLog4J2Marker.INSTANCE, "test message");
someLogger.log(Level.ERROR, "test message");
someLogger.log(Level.ERROR, DummyLog4J2Marker.INSTANCE, "test message");
and
logger.error("test message");
logger.error(DummySlf4JMarker.INSTANCE, "test message");
logger.atInfo().log("1. Single parameter: %s","abc");
logger.atInfo().log("2. Escaped formatting anchor: \\{}");
logger.atInfo().log("3. Escaped anchor and single parameter: {} %s", "abc");
logger.atInfo().log("4. Escaped anchors and single parameter: {} %s {}", "abc");
logger.atInfo().log("5. Double-escaped anchor, single parameter: \\%s", "abc");
logger.atInfo().log("6. Double-escaped anchor, no parameter: \\\\{}");
logger.atInfo().log("7. Single parameter, double-escaped anchor: %s \\%s", "abc");
logger.atInfo().log("8. Percent sign: 5%% of %s", "abc");
and
someLogger.severe("test message");
someLogger.log(Level.SEVERE, "test message");
someLogger.log(CustomJULLevel.LEVEL_1, "test message");
someLogger.log(CustomJULLevel.LEVEL_2, "test message");
someLogger.log(CustomJULLevel.LEVEL_3, "test message");
into their canonical Flogger form, such as:
logger.atSevere().log( "test message" );
someLogger.at(CustomJULLevel.LEVEL_1).log( "test message" );
logger.atInfo().log("1. Single parameter: %s","abc");
logger.atInfo().log("2. Escaped formatting anchor: \\{}");
logger.atInfo().log("3. Escaped anchor and single parameter: {} %s", "abc");
logger.atInfo().log("4. Escaped anchors and single parameter: {} %s {}", "abc");
logger.atInfo().log("5. Double-escaped anchor, single parameter: \\%s", "abc");
logger.atInfo().log("6. Double-escaped anchor, no parameter: \\\\{}");
logger.atInfo().log("7. Single parameter, double-escaped anchor: %s \\%s", "abc");
logger.atInfo().log("8. Percent sign: 5%% of %s", "abc");
Current plan is to migrate (canonicalize) log statements, including transforming message format strings, leaving further refactorings / verifications to subsequent Flogger checks, such as:
- check validity of format string (parameter count, format specifiers);
- check/migrate format specifiers to appropriate types (e.g. %d vs %s)
- removal of redundant conditional statements around logging;
- migration of string-concatenated message format strings to use parameters;
- remove redundant toString() calls for parameters;
- move any expensive parameters (e.g. function calls) to be lazy;
...and likely others. It's likely that Google's Flogger checks overlap heavily here. Not finding any Flogger checks in open-source Error Prone (https://errorprone.info/bugpatterns); if those could be made open-source that would be great, no point in re-inventing them.
from flogger.
Just FYI, escaping in printf is done with '%', so:
logger.atInfo().log("7. Single parameter, double-escaped anchor: %s \\%s", "abc");
Looks wrong to me, I think it should be:
logger.atInfo().log("7. Single parameter, double-escaped anchor: %s %%s", "abc");
from flogger.
Thanks. Missing context: that example was from this SLF4J input:
logger.info("7. Single parameter, double-escaped anchor: {} \\\\{}", "abc");
...where SLF4J treats \{}
as escaping the format anchor and \\{}
as a preceding backslash before the format anchor, which is then replaced by %s
.
In all cases the to-be-migrated format string escapes %
as %%
prior to applying API-specific format string migration.
from flogger.
Ahh okay. It's just that as written:
logger.atInfo().log("7. Single parameter, double-escaped anchor: %s \\%s", "abc");
would trigger a "too few parameters" error in our checks and the runtime code, since it expects 2 arguments for the 2 placeholders.
Also, as regards existing checks, I think we do most of what you're suggesting already, so it's definitely not worth spending much time on that until I can get an answer about open-sourcing what we have.
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
- I don't believe ScopedLoggingContexts should obviously accept null "no-op" arguments. HOT 5
- 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.