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

docs: Improve String concatenation best practice #3553

Open
wants to merge 1 commit into
base: 2.x
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/site/antora/modules/ROOT/pages/manual/api.adoc
Original file line number Diff line number Diff line change
@@ -78,6 +78,8 @@ include::partial$manual/api-best-practice-exception-as-last-argument.adoc[]

include::partial$manual/api-best-practice-dont-use-string-concat.adoc[]

include::partial$manual/api-best-practice-dont-mix-concat-and-params.adoc[]

[#best-practice-supplier]
=== Use ``Supplier``s to pass computationally expensive arguments

Original file line number Diff line number Diff line change
@@ -121,6 +121,8 @@ include::partial$manual/api-best-practice-exception-as-last-argument.adoc[]

include::partial$manual/api-best-practice-dont-use-string-concat.adoc[]

include::partial$manual/api-best-practice-dont-mix-concat-and-params.adoc[]

[#install-app]
== How do I install Log4j Core to run my **application**?

Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
////
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
////

If you are mixing `String` concatenation and parameterized logging, you are doing something very wrong and dangerous!

* [ ] The format string of a parameterized statement should be a compile time constant!
An attacker could mangle your logs by inserting `{}` placeholders in the values!
Try these examples with `userId="{}\nbadUser"` and `reason="root logged in successfully"`
+
[source,java]
----
/* BAD! */ LOGGER.info("User " + userId + " logged out: {}", reason);
----

* [x] Use message parameters
+
[source,java]
----
/* GOOD */ LOGGER.info("User {} logged out: {}", userId, reason);
----
Original file line number Diff line number Diff line change
@@ -15,13 +15,8 @@
limitations under the License.
////

If you are using `String` concatenation while logging, you are doing something very wrong and dangerous!

* [ ] Don't use `String` concatenation to format arguments!
This circumvents the handling of arguments by message type and layout.
More importantly, **this approach is prone to attacks!**
Imagine `userId` being provided by the user with the following content:
`placeholders for non-existing args to trigger failure: {} {} \{dangerousLookup}`
* [ ] Don't use `String` concatenation to format arguments:
the log message will be formatted, even if the logger is not enabled, and you will suffer a performance penalty!
+
[source,java]
----
@@ -34,3 +29,10 @@ Imagine `userId` being provided by the user with the following content:
----
/* GOOD */ LOGGER.info("failed for user ID `{}`", userId);
----

* [x] Use message lambdas
+
[source,java]
----
/* GOOD */ LOGGER.info(() -> "failed for user ID: " + userId);
Copy link
Member

@vy vy Mar 21, 2025

Choose a reason for hiding this comment

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

@ppkarwasz, I think this is a foot gun. It'd be very hard to distinguish for inexperienced users, which is the chief audience of this guide, why it is fine to do string concatenation here, whereas before it was not. IMHO, this should not be supported by the API in the first place, but anyway. In short, unless you strongly object, can we remove this part, please?

----