Skip to content

Commit 69169e6

Browse files
committed
explaining snippets before the snippet, simpler wording
1 parent a0c2e8d commit 69169e6

File tree

1 file changed

+47
-11
lines changed

1 file changed

+47
-11
lines changed

src/site/antora/modules/ROOT/pages/5min.adoc

+47-11
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,72 @@
1515
limitations under the License.
1616
////
1717
18-
= Learn Log4j in 5 minutes!
18+
= Log4j explained in 5 minutes
1919
20-
Do you need a crash course on Log4j?
21-
You have come to the right place!
20+
This document aims to guide you through the most important aspects of logging with Log4j.
21+
It is not a comprehensive guide, but it should give you a good starting point.
2222
If you are looking for a more detailed read, please see {logging-services-url}/what-is-logging.html[What is logging?].
2323
2424
[#what]
25-
== What is logging and Log4j?
25+
== What is logging?
2626
27-
Logging is the action of publishing diagnostics information at certain points of a program execution:
27+
Logging is the action of publishing diagnostics information at certain points of a program execution.
28+
It means you can write messages to a log file or console to help you understand what your application is doing.
2829
29-
[source,java]
30+
The simplest way to log in Java is to use `System.out.println()`, like this:
31+
32+
[source, java]
33+
----
34+
private void truncateTable(String tableName) {
35+
System.out.println("Truncating table"); <1>
36+
db.truncate(tableName);
37+
}
38+
----
39+
<1> The information that a table is being truncated is written to the console.
40+
41+
This is already useful, but the reader of this message does not know what table is being truncated.
42+
Usually, we would like to include the table name in the message, which quickly leads
43+
developers to use the `System.out.format` (or similar) methods.
44+
Log4j helps with formatting strings as we will see later, but for now, let's see how to work without it.
45+
46+
The following code shows how this method could be used to provide more context information.
47+
`%s` will be replaced with the value of `tableName`, and `%n` will be replaced with a new line.
48+
49+
[source, java]
3050
----
3151
private void truncateTable(String tableName) {
32-
System.out.format("[WARN] Truncating table `%s`!%n", tableName);
52+
System.out.format("[WARN] Truncating table `%s`%n", tableName); <1>
3353
db.truncate(tableName);
3454
}
3555
----
56+
<1> `format` writes the message to the console, replacing `%s` with the value of `tableName`.
57+
58+
If the developer decides the truncate the table "fruits", the output of this code will look like this:
59+
60+
[source]
61+
----
62+
[WARN] Truncating table `fruits`
63+
----
64+
65+
This provides observability into an application's runtime and we can follow the execution flow.
66+
67+
However, there are several drawbacks with the above approach and this is where Log4j comes in.
68+
Log4j will help you to write logs in a more structured way, with more information, and with more flexibility.
69+
70+
[#why]
71+
== Why should I use Log4j?
3672
37-
This provides observability into an application's runtime.
73+
Log4j is a versatile, industrial-grade Java logging framework, maintained by many contributors.
74+
It can help us with common logging tasks and lets us focus on the application logic.
3875
39-
But we can do way better than a `printf()` statement!
76+
It will:
4077
4178
* Enhance the message with additional information (timestamp, class & method name, line number, host, severity, etc.)
4279
* Write the message differently, using a different **layout** (CSV, JSON, etc.)
4380
* Write the message to a different medium, using a different **appender** (file, socket, database, queue, etc.)
4481
* Write only some of the messages, using a **filter** (e.g. filter by severity, content, etc.)
4582
46-
Log4j is a versatile, industrial-grade Java logging framework delivering all these and more in one product.
47-
It is essentially composed of a **logging API** and its **implementation**:
83+
Log4j is essentially composed of a **logging API** and its **implementation**:
4884
4985
Log4j API::
5086
The logging API your code (programmatically) logs through.

0 commit comments

Comments
 (0)