You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardexpand all lines: src/site/antora/modules/ROOT/pages/5min.adoc
+133-60
Original file line number
Diff line number
Diff line change
@@ -19,7 +19,6 @@
19
19
20
20
This document aims to guide you through the most important aspects of logging with Log4j.
21
21
It is not a comprehensive guide, but it should give you a good starting point.
22
-
If you are looking for a more detailed read, please see {logging-services-url}/what-is-logging.html[What is logging?].
23
22
24
23
[#what]
25
24
== What is logging?
@@ -80,20 +79,12 @@ It will:
80
79
* Write the message to a different medium, using a different **appender** (file, socket, database, queue, etc.)
81
80
* Write only some of the messages, using a **filter** (e.g. filter by severity, content, etc.)
82
81
83
-
Log4j is essentially composed of a **logging API** and its **implementation**:
84
-
85
-
Log4j API::
86
-
The logging API your code (programmatically) logs through.
87
-
This needs to be available at compile-time and no configuration is needed.
88
-
89
-
Log4j Core::
90
-
The logging implementation is responsible for filtering, routing, encoding, and appending log events.
91
-
This needs to be available at runtime and configured by the user.
92
-
93
-
[#logging]
94
-
== How do I write logs using Log4j?
82
+
[#install]
83
+
== How do to install Log4j?
95
84
96
-
Add the `log4j-api` dependency to your application:
85
+
Add the necessary `log4j-api` dependencies to your application. We will need
86
+
a **BOM** (Bill of Materials) to manage the versions of the dependencies.
87
+
In addition, we will need the `log4j-api` dependency itself.
97
88
98
89
[tabs]
99
90
====
@@ -136,7 +127,15 @@ dependencies {
136
127
----
137
128
====
138
129
139
-
And start logging:
130
+
[#logging]
131
+
== How do I write logs using Log4j?
132
+
133
+
To write logs, you need a `Logger` instance which you will retrieve from the `LogManager`.
134
+
The `Logger` instance is thread-safe and reusable.
135
+
136
+
Second, you can use the `Logger` instance to write logs by using methods like `info`, `warn`, `error`, etc.
137
+
We will come to them in a moment.
138
+
The string can also contain placeholders written as `{}` that will be replaced by the arguments passed to the method.
140
139
141
140
[source,java]
142
141
----
@@ -155,10 +154,20 @@ public class DbTableService {
155
154
}
156
155
----
157
156
<1> This is a thread-safe, reusable `Logger` instance.
158
-
The associated class will be captured at initialization – no need for a `getLogger(DbTableService.class)`.
159
-
<2> The parameter placeholders `{}` in the message will be automatically replaced with the value of `tableName` and the generated **log event** will be enriched with **level** (i.e., `WARN`), timestamp, class & method name, line number, and several other information.
157
+
<2> The placeholder `{}` in the message will be replaced with the value of `tableName`
158
+
159
+
The generated **log event** will be enriched with the **log level** (i.e., `WARN`),
160
+
but also timestamp, class & method name, line number, and several other information.
161
+
162
+
Log levels are used to categorize log events by severity and control the verbosity of the logs.
163
+
Log4j knows various levels, but the most common are `DEBUG`, `WARN`, and `ERROR`.
164
+
With them, you can filter out less important logs and focus on the most critical ones.
165
+
Previously we used `LOGGER.warn` to log a warning message, which could mean that something is not right, but the application can continue.
166
+
Log levels have a priority, and `WARN` is less severe than `ERROR`.
160
167
161
-
Make sure to log exceptions that have diagnostics value:
168
+
Exceptions are often also errors.
169
+
In this case, we might use the `ERROR` log level.
170
+
Make sure to log exceptions that have diagnostics value - we can simply pass the exception as the last argument to the log method.
162
171
163
172
[source,java]
164
173
----
@@ -170,15 +179,10 @@ try {
170
179
throw new IOException("failed truncating table: " + tableName, exception);
171
180
}
172
181
----
173
-
<1> Notice the `error()` method?
174
-
Yup, the level is set to `ERROR`.
175
-
+
176
-
What about the `exception` in the last argument?
177
-
Wait a second!
178
-
There is one placeholder in the format (i.e., `{}`), but there are two parameters passed in arguments: `tableName` and `exception`!
179
-
What the heck?
180
-
Yep, you guessed it right!
181
-
Log4j API will attach the last extra argument of type `Throwable` in a separate field to the generated log event.
182
+
<1> By using `error()` instead of `warn()`, we signal that the operation failed.
183
+
184
+
While there is only one placeholder in the message, we pass two arguments: `tableName` and `exception`.
185
+
Log4j will attach the last extra argument of type `Throwable` in a separate field to the generated log event.
182
186
183
187
[#pitfalls]
184
188
=== Common pitfalls
@@ -262,20 +266,41 @@ Imagine `userId` being provided by the user with the following content:
262
266
/* GOOD */ LOGGER.info("failed for user ID `{}`", userId);
263
267
----
264
268
265
-
[#config-app]
266
-
== How do I configure Log4j to run my **application**?
269
+
[#integrating-log4j]
270
+
== Integrating Log4j
271
+
272
+
Log4j is composed of two main parts, the API and the Core.
273
+
With this distinction, you can log through the API and route the log events through the Core.
274
+
If you prefer, you can also route the log events through other logging frameworks like SLF4J.
267
275
268
-
Your code logs through a logging API.
276
+
[#log4j-api]
277
+
Log4j API::
278
+
The logging API your code (programmatically) logs through.
279
+
This needs to be available at compile-time and no configuration is needed.
280
+
281
+
Log4j Core::
282
+
The logging implementation is responsible for filtering, routing, encoding, and appending log events.
283
+
This needs to be available at runtime and configured by the user.
269
284
So your dependencies and their dependencies too.
270
285
While deploying your application, you need to provide a **logging implementation** along with its configuration to consume all generated log events.
271
286
287
+
[#config-app]
288
+
== How do I configure Log4j to run my **application**?
289
+
290
+
The following section describes, how an application can be configured to use Log4j.
291
+
It will add a configuration and some other artifacts to your application.
292
+
The configuration shown here enhances the security and usability of your application.
293
+
272
294
[IMPORTANT]
273
295
====
274
296
Are you implementing not an **application**, but a **library**?
275
297
Please skip to the xref:#config-lib[] instead.
276
298
====
277
299
278
-
Add the `log4j-core` **runtime** dependency to your application:
300
+
As mentioned, Log4j is using a logging API.
301
+
First of all, add the `log4j-core` **runtime** dependency to our application.
302
+
Second, it is highly recommended to add the `log4j-layout-template-json` **runtime** dependency to encode log events in JSON.
303
+
This is the most secure way to format log events and should preferred over the default `PatternLayout`.
279
304
280
305
[tabs]
281
306
====
@@ -284,8 +309,7 @@ Maven::
284
309
[source,xml,subs="+attributes"]
285
310
----
286
311
<project>
287
-
288
-
<!-- Assuming you already have the `dependencyManagement > dependencies > dependency` entry for `log4j-bom` -->
<1> Note that the logging implementation and bridges are only needed at runtime!
339
-
<2> SLF4J is another widely used logging API.
340
-
`log4j-slf4j2-impl` forwards SLF4J calls to Log4j API, which effectively gets processed by Log4j Core too.
350
+
<1> Note that the logging implementation and bridges are only needed at runtime.
341
351
342
352
Now it is time to configure Log4j and instruct how the log events should be routed.
343
-
Save the following XML document to `src/**main**/resources/log4j2.xml`:
353
+
Save the following XML document to `src/**main**/resources/log4j2.xml`.
354
+
355
+
The xref:manual/json-template-layout.adoc[JSON Template Layout] is used to encode log events in JSON.
356
+
Once encoded xref:manual/appenders.adoc[Appenders] are responsible for writing log events to the console, file, socket, database, etc.
357
+
358
+
The `<logger>` defines, that log events generated by classes in the `com.mycompany` package (incl. its sub-packages) and that are of level `INFO` and higher (i.e., `WARN`, `ERROR`, `FATAL`) will be consumed.
359
+
Finally, the `<root>` logger defines that log events of level `WARN` and higher will be consumed unless specified otherwise. It serves as a default configuration.
344
360
345
361
.An example `src/**main**/resources/log4j2.xml`
346
362
[source,xml]
@@ -367,16 +383,62 @@ Save the following XML document to `src/**main**/resources/log4j2.xml`:
367
383
368
384
</Configuration>
369
385
----
370
-
<1> xref:manual/appenders.adoc[Appenders] are responsible for writing log events to the console, file, socket, database, etc.
371
-
<2> xref:manual/appenders.adoc#ConsoleAppender[Console Appender] is used to write logs to the console.
372
-
<3> xref:manual/json-template-layout.adoc[JSON Template Layout] is used to encode log events in JSON.
373
-
<4> Log events generated by classes in the `com.mycompany` package (incl. its subpackages) and that are of level `INFO` and higher (i.e., `WARN`, `ERROR`, `FATAL`) will be consumed.
386
+
<1> xref:manual/appenders.adoc[Appenders] are responsible for writing log events to their target
387
+
<2> xref:manual/appenders.adoc#ConsoleAppender[Console Appender] writes logs to the console.
388
+
<3> xref:manual/json-template-layout.adoc[JSON Template Layout] encodes log events in JSON.
389
+
<4> Log events generated by classes in the `com.mycompany` package (incl. its sub-packages) that are of level `INFO` and higher will be consumed.
374
390
<5> Unless specified otherwise, log events of level `WARN` and higher will be consumed.
375
391
<6> Unless specified otherwise, log events will be forwarded to the `console` appender defined earlier.
376
392
377
-
You are strongly advised to use a different Log4j configuration for tests.
393
+
If you want to configure Log4j for tests, you are strongly advised to use a different Log4j configuration.
378
394
Continue to xref:#config-test[]
379
395
396
+
In many cases, you might have a library that logs through SLF4J.
397
+
Due to the separation of Log4js API and Core, you can add a bridge to forward SLF4J calls to the Log4j API.
398
+
This way, SLF4J calls will be processed by Log4j Core too.
399
+
400
+
It is similarly easy: just add the new dependency `log4j-slf4j2-impl to your application.
0 commit comments