Skip to content

[java] Fix #7216: GuardLogStatement: accept logstash structured arguments - #7225

Merged
UncleOwen merged 6 commits into
pmd:mainfrom
princejain756:fix-guard-log-statement-structured-args
Nov 8, 2026
Merged

UncleOwen merged 6 commits into
pmd:mainfrom
princejain756:fix-guard-log-statement-structured-args

Conversation

@princejain756

Copy link
Copy Markdown
Contributor

Describe the PR

log.debug("user={}", value("user", user)) was flagged by GuardLogStatement although the logstash StructuredArguments helpers only wrap the argument for the structured encoder; the string rendering stays deferred exactly like plain {} parameter substitution, so a level guard buys nothing.

The rule now treats any argument of type net.logstash.logback.argument.StructuredArgument as low overhead, which covers value, keyValue, entries, defer and user factories returning the same type. Arbitrary method calls are still flagged.

A test-scoped logstash-logback-encoder dependency was added so the regression test can resolve the argument type, matching the existing typeresolution test deps.

Related issues

Ready?

  • Added unit tests for fixed bug/feature (GuardLogStatement XML: value/keyValue/entries unflagged, a plain helper call still flagged)
  • Passing all unit tests (46/46 GuardLogStatementTest, red-verified without the rule change: 4 violations instead of 1)
  • Complete build using ./mvnw verify at the project root (module compile, test, checkstyle and pmd:check run locally for pmd-java)
  • A changelog entry under java-bestpractices
  • Commit message explains the why

@zbynek zbynek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

It's not clear to me how pedantic this rule should be -- calling a constructor for even a lightweight object still creates overhead.

Assuming we're okay with that constructor call (as the linked issue suggests), the rule should still not allow value("user", slowMethodToGetUser()) as an argument.

@princejain756

Copy link
Copy Markdown
Contributor Author

Good catch @zbynek. Updated isStructuredLoggingArgument so that when a StructuredArgument is produced by a method or constructor call (InvocationNode), all arguments passed into that call must also pass isLowOverheadParam (plus a test case for value("user", computeName(user))).

@zbynek zbynek left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The logic looks good now, thanks. Please resolve the release notes merge conflict.

@princejain756

Copy link
Copy Markdown
Contributor Author

Resolved the release notes conflict against main.

@pmd-actions-helper

pmd-actions-helper Bot commented Oct 25, 2026 •

Copy link
Copy Markdown
Contributor

Documentation Preview

Compared to main:
This changeset changes 0 violations,
introduces 0 new violations, 0 new errors and 0 new configuration errors,
removes 0 violations, 16 errors and 9 configuration errors.
There are 0 changed duplications, 0 new duplications and 0 removed duplications.
There are 0 changed CPD errors, 0 new CPD errors and 0 removed CPD errors.

Regression Tester Report

(comment created at 2026-11-08 20:35:49+00:00 for c20c126)

@princejain756
princejain756 force-pushed the fix-guard-log-statement-structured-args branch 2 times, most recently from 2fc01cb to cf5e380 Compare November 5, 2026 23:44
@zbynek
zbynek requested a review from adangel November 5, 2026 23:57
)

log.debug("user={}", value("user", user)) was flagged although its
argument cost is the same as plain parameter substitution: the logstash
helper call returns a StructuredArgument whose string rendering is
deferred to the encoder.

Treat arguments of type net.logstash.logback.argument.StructuredArgument
as low overhead, covering value, keyValue, entries and user helpers
returning the same type. Arbitrary method calls still require a guard.

Adds logstash-logback-encoder as a test-scoped dependency so the
regression test resolves the argument type.
@princejain756
princejain756 force-pushed the fix-guard-log-statement-structured-args branch from cf5e380 to 39f72b0 Compare November 8, 2026 17:27
@UncleOwen UncleOwen added this to the 7.30.0 milestone Nov 8, 2026

@UncleOwen UncleOwen left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thanks

@UncleOwen
UncleOwen merged commit 63850a0 into pmd:main Nov 8, 2026
13 checks passed
@adangel adangel changed the title [java] GuardLogStatement: accept logstash structured arguments [java] Fix #7216: GuardLogStatement: accept logstash structured arguments Nov 12, 2026
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.

[java] GuardLogStatement: False positive on SLF4J parameterised calls whose arguments are wrapped with logstash value(...)

3 participants