Skip to content
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

Fix incorrect variable placeholder for Strings.format calls #91531

Merged
merged 1 commit into from
Nov 14, 2022

Conversation

ywangd
Copy link
Member

@ywangd ywangd commented Nov 12, 2022

The curly bracket placeholder works for LoggerMessageFormat.format and ParameterizedMessage.format, but Not for Strings.format which requires Java's string format syntax. This PR fixes the incorrect usages.

Relates: #86549, #87924

PS: I labelled this >non-issue because the changes are all for debug/trace level loggings.

The curly bracket placeholder works for LoggerMessageFormat.format and
ParameterizedMessage.format, but Not for Strings.format which requires
Java's string format syntax. This PR fixes the incorrect usages.
@ywangd ywangd added >non-issue :Core/Infra/Logging Log management and logging utilities v8.6.0 labels Nov 12, 2022
@ywangd ywangd requested review from pgomulka and n1v0lg November 12, 2022 00:19
@elasticsearchmachine elasticsearchmachine added the Team:Core/Infra Meta label for core/infra team label Nov 12, 2022
@elasticsearchmachine
Copy link
Collaborator

Pinging @elastic/es-core-infra (Team:Core/Infra)

@ywangd
Copy link
Member Author

ywangd commented Nov 12, 2022

@pgomulka We have choice to use either of the following four static format methods

  1. LoggerMessageFormat.format
  2. ParameterizedMessage.format
  3. Strings.format
  4. String.format

The first two support {} as variable placeholder while the last two do not. It is rather confusing at times, especially when the methods are statically imported and used without their class qualifier. Do we need to do something so that misuse of placeholders is less likely in future?

@pgomulka
Copy link
Contributor

@ywangd thank you for fixing this. Ideally we should use org.elasticsearch.core.Strings.format. Do you think you could also change the imports?
That method already populates Locale.ROOT and handles exceptions when format is incorrect.

Agree we need some static analysis tool to fix the incorrect usages of logger formatting. Unfortunately ESLoggerUsageChecker does not check this at the moment. I have this on a list as a possible refactoring during the tech debt week.

Copy link
Contributor

@n1v0lg n1v0lg left a comment

Choose a reason for hiding this comment

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

Thank you for fixing this!

I think replacing static imports i.e., format() with Strings.format(...) would be nice (I think this is what @pgomulka is also suggesting).

@ywangd
Copy link
Member Author

ywangd commented Nov 14, 2022

@pgomulka All usages of the format method in this PR is org.elasticsearch.core.Strings.format. Could you please clarify what you want me to do? Are you suggesting another sweep across the code base to change any usage of java.lang.String.format to org.elasticsearch.core.Strings.format? Or are you suggesting we should avoid static imports and the format method should be called like Strings.format(...)?

Agree we need some static analysis tool to fix the incorrect usages of logger formatting. Unfortunately ESLoggerUsageChecker does not check this at the moment. I have this on a list as a possible refactoring during the tech debt week.

It's great to know this is on your (team)'s radar. If we don't want use java.lang.String.format, maybe we can also add it to the list of forbiddenApis.

@pgomulka
Copy link
Contributor

@ywangd appologies, I noticed the String.format usage in JavaModulePrecommitTask and I thought that this PR has a mixture of different .format methods. All is good. thanks

@ywangd ywangd merged commit 7b71c94 into elastic:main Nov 14, 2022
weizijun added a commit to weizijun/elasticsearch that referenced this pull request Nov 15, 2022
* main: (163 commits)
  [DOCS] Edits frequent items aggregation (elastic#91564)
  Handle providers of optional services in ubermodule classloader (elastic#91217)
  Add `exportDockerImages` lifecycle task for exporting docker tarballs (elastic#91571)
  Fix CSV dependency report output file location in DRA CI job
  Fix variable placeholder for Strings.format calls (elastic#91531)
  Fix output dir creation in ConcatFileTask (elastic#91568)
  Fix declaration of dependencies in DRA snapshots CI job (elastic#91569)
  Upgrade Gradle Enterprise plugin to 3.11.4 (elastic#91435)
  Ingest DateProcessor (small) speedup, optimize collections code in DateFormatter.forPattern (elastic#91521)
  Fix inter project handling of generateDependenciesReport (elastic#91555)
  [Synthetics] Add synthetics-* read to fleet-server (elastic#91391)
  [ML] Copy more settings when creating DF analytics destination index (elastic#91546)
  Reduce CartesianCentroidIT flakiness (elastic#91553)
  Propagate last node to reinitialized routing tables (elastic#91549)
  Forecast write load during rollovers (elastic#91425)
  [DOCS] Warn about potential overhead of named queries (elastic#91512)
  Datastream unavailable exception metadata (elastic#91461)
  Generate docker images and dependency report in DRA ci job (elastic#91545)
  Support cartesian_bounds aggregation on point and shape (elastic#91298)
  Add support for EQL samples queries (elastic#91312)
  ...

# Conflicts:
#	x-pack/plugin/rollup/src/main/java/org/elasticsearch/xpack/downsample/RollupShardIndexer.java
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
:Core/Infra/Logging Log management and logging utilities >non-issue Team:Core/Infra Meta label for core/infra team v8.6.0
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants