Repository navigation
Fix Log4j1 to SLF4J migration of mapped diagnostic context (MDC): convert non-String values to Strings and migrate Hashtable to Map. - #296
Conversation
…vert non-String values to Strings and migrate Hashtable to Map. Co-authored-by: mdepaula <dipaula.matheus01@gmail.com>
Code review — high effort (self-review of this branch)Reviewed the two new recipes + YAML/CSV wiring + tests. Six findings, most severe first. Correctness
Cleanup
Checked and cleared: |
- returnsGetContext(): stop descending into nested/local class declarations so a return inside a nested class no longer retypes the enclosing method - parameter-sync: only rewrite method-type entries still typed Hashtable, leaving unrelated Map parameters' (parameterized) attribution intact - assignment pre-scan: handle this.field = MDC.getContext() (J.FieldAccess) so field declarations assigned via qualified access are retyped Add regression tests for each.
Fixes pushed (db48d83)Addressed the three clear correctness findings, with a regression test each:
Deferred, with rationale:
All 20 tests in the four affected suites pass. |
- Pre-scan and returnsGetContext accumulate via .reduce(tree, seed) instead of visiting a pre-allocated mutable holder - Return the retyped node directly instead of reassign-then-return in visitVariableDeclarations and the return-type retype - Extract syncRetypedParameters so the parameter sync is its own immediate-return helper, keeping both method-declaration retypes as early returns without dropping the both-apply case
timtebeek
left a comment
There was a problem hiding this comment.
Thanks a lot for the help here! Took a bit of time to trickle through some small improvements, but looks ok otherwise!
|
Thank you again! It looks like you're using a helpful Openrewrite AI skill. I wonder if it's public? I'd like to catch as much as I can before raising PRs. |
…rom 3.29.1 to 3.30.0 [skip ci] Bumps [org.openrewrite.recipe:rewrite-logging-frameworks](https://github.com/openrewrite/rewrite-logging-frameworks) from 3.29.1 to 3.30.0. Release notes *Sourced from [org.openrewrite.recipe:rewrite-logging-frameworks's releases](https://github.com/openrewrite/rewrite-logging-frameworks/releases).* > 3.30.0 > ------ > > What's Changed > -------------- > > * Allow StringFormatToParameterizedLogging to migrate format strings without specifiers. by [`@motlin`](https://github.com/motlin) in [openrewrite/rewrite-logging-frameworks#295](https://redirect.github.com/openrewrite/rewrite-logging-frameworks/pull/295) > * Fix Log4j1 to SLF4J migration of mapped diagnostic context (MDC): convert non-String values to Strings and migrate Hashtable to Map. by [`@motlin`](https://github.com/motlin) in [openrewrite/rewrite-logging-frameworks#296](https://redirect.github.com/openrewrite/rewrite-logging-frameworks/pull/296) > * Suppress OWASP findings for intentional log4j 1.x dependency by [`@timtebeek`](https://github.com/timtebeek) in [openrewrite/rewrite-logging-frameworks#297](https://redirect.github.com/openrewrite/rewrite-logging-frameworks/pull/297) > > **Full Changelog**: <openrewrite/rewrite-logging-frameworks@v3.29.2...v3.30.0> > > 3.29.2 > ------ > > What's Changed > -------------- > > * OpenRewrite v8.86.0: <https://github.com/openrewrite/rewrite> > > **Full Changelog**: <openrewrite/rewrite-logging-frameworks@v3.29.1...v3.29.2> Commits * [`c357a72`](openrewrite/rewrite-logging-frameworks@c357a72) OpenRewrite recipe best practices * [`3f0e4a0`](openrewrite/rewrite-logging-frameworks@3f0e4a0) Add OWASP suppression for intentional log4j 1.x dependency ([#297](https://redirect.github.com/openrewrite/rewrite-logging-frameworks/issues/297)) * [`0677d12`](openrewrite/rewrite-logging-frameworks@0677d12) Fix Log4j1 to SLF4J migration of mapped diagnostic context (MDC): convert non... * [`aab44dd`](openrewrite/rewrite-logging-frameworks@aab44dd) OpenRewrite recipe best practices * [`068d1bf`](openrewrite/rewrite-logging-frameworks@068d1bf) Allow StringFormatToParameterizedLogging to migrate format strings without sp... * [`1883eb7`](openrewrite/rewrite-logging-frameworks@1883eb7) Remove out-of-date OWASP suppressions * See full diff in [compare view](openrewrite/rewrite-logging-frameworks@v3.29.1...v3.30.0)
What's changed?
Added Log4j 1.x MDC migration support to the SLF4J recipes:
StringMDC.put(String, Object)values withString.valueOf(...)before migrating to SLF4J.MDC.getContext()toMDC.getCopyOfContextMap().Hashtabledeclarations/returns/assignments toMap<String, String>.What's your motivation?
Log4j 1.x and SLF4J differ in MDC API types: Log4j accepts arbitrary object values and returns
HashtablefromgetContext(), while SLF4J expectsStringvalues and returns aMap. A direct type migration can leave uncompilable code.Anything in particular you'd like reviewers to focus on?
Working on the Log4j 1.x recipes can be tricky because they work through Log4j 2.x as a stepping stone. I want to make sure I'm not breaking anything for Log4j 2.x users, but don't use it myself.
Anyone you would like to review specifically?
@timtebeek probably?