|
15 | 15 | limitations under the License.
|
16 | 16 | ////
|
17 | 17 |
|
18 |
| -= Learn Log4j in 5 minutes! |
| 18 | += Log4j explained in 5 minutes |
19 | 19 |
|
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. |
22 | 22 | If you are looking for a more detailed read, please see {logging-services-url}/what-is-logging.html[What is logging?].
|
23 | 23 |
|
24 | 24 | [#what]
|
25 |
| -== What is logging and Log4j? |
| 25 | +== What is logging? |
26 | 26 |
|
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. |
28 | 29 |
|
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] |
30 | 50 | ----
|
31 | 51 | 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> |
33 | 53 | db.truncate(tableName);
|
34 | 54 | }
|
35 | 55 | ----
|
| 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? |
36 | 72 |
|
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. |
38 | 75 |
|
39 |
| -But we can do way better than a `printf()` statement! |
| 76 | +It will: |
40 | 77 |
|
41 | 78 | * Enhance the message with additional information (timestamp, class & method name, line number, host, severity, etc.)
|
42 | 79 | * Write the message differently, using a different **layout** (CSV, JSON, etc.)
|
43 | 80 | * Write the message to a different medium, using a different **appender** (file, socket, database, queue, etc.)
|
44 | 81 | * Write only some of the messages, using a **filter** (e.g. filter by severity, content, etc.)
|
45 | 82 |
|
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**: |
48 | 84 |
|
49 | 85 | Log4j API::
|
50 | 86 | The logging API your code (programmatically) logs through.
|
|
0 commit comments