Skip to content

Commit

Permalink
Improve 5min.adoc formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
vy committed May 2, 2024
1 parent 210c9de commit abbd774
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions src/site/antora/modules/ROOT/pages/5min.adoc
Expand Up @@ -152,14 +152,14 @@ Let's try to walk through the most common ones.
[#pitfal-toString]
==== Don't use `toString()`
* [ ] `Object#toString()` is redundant in arguments
* [ ] Don't use `Object#toString()` in arguments, it is redundant!
+
[source,java]
----
/* BAD! */ LOGGER.info("userId: {}", userId.toString());
----
* [x] Underlying message type and layout will deal with arguments
* [x] Underlying message type and layout will deal with arguments:
+
[source,java]
----
Expand All @@ -169,18 +169,15 @@ Let's try to walk through the most common ones.
[#pitfall-exception]
==== Pass exception as the last extra argument
Using `Throwable#printStackTrace()` or `Throwable#getMessage()` while logging?
Please, don't!
* [ ] Don't call `Throwable#printStackTrace()`.
* [ ] Don't call `Throwable#printStackTrace()`!
This not only circumvents the logging, but can also leak sensitive information!
+
[source,java]
----
/* BAD! */ exception.printStackTrace();
----
* [ ] Don't use `Throwable#getMessage()`.
* [ ] Don't use `Throwable#getMessage()`!
This prevents the log event from getting enriched with the exception.
+
[source,java]
Expand All @@ -189,14 +186,15 @@ This prevents the log event from getting enriched with the exception.
/* BAD! */ LOGGER.info("failed for user ID `{}`: {}", userId, exception.getMessage());
----
* [ ] This bloats the log message with duplicate exception message
* [ ] Don't provide both `Throwable#getMessage()` and `Throwable` itself!
This bloats the log message with duplicate exception message.
+
[source,java]
----
/* BAD! */ LOGGER.info("failed for user ID `{}`: {}", userId, exception.getMessage(), exception);
----
* [x] Pass exception as the last extra argument
* [x] Pass exception as the last extra argument:
+
[source,java]
----
Expand All @@ -209,8 +207,9 @@ This prevents the log event from getting enriched with the exception.
If you are using `String` concatenation while logging, you are doing something very wrong and dangerous!
* [ ] Circumvents the handling of arguments by message type and layout.
More importantly, this code is prone to attacks!
* [ ] 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 user with the following content:
`placeholders for non-existing args to trigger failure: {} {} \{dangerousLookup}`
+
Expand Down Expand Up @@ -253,7 +252,7 @@ Maven::
<dependency>
<!-- The logging implementation (i.e., Log4j Core) -->
<!-- Logging implementation (Log4j Core) -->
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
Expand Down Expand Up @@ -449,7 +448,7 @@ Save the following XML document to `src/**test**/resources/log4j2-test.xml`:
== What is next?
Installation::
While shared dependency management snippets should get you going, it can also be challenging depending on your use case.
While shared dependency management snippets should get you going, your case might necessitate a more intricate setup.
Are you dealing with a Spring Boot application?
Is it running in a Java EE container?
Do you need to take into account other logging APIs such as JUL, JPL, JCL, etc.?
Expand Down

0 comments on commit abbd774

Please sign in to comment.