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
+37-27
Original file line number
Diff line number
Diff line change
@@ -80,10 +80,9 @@ It will:
80
80
* Write only some of the messages, using a **filter** (e.g. filter by severity, content, etc.)
81
81
82
82
[#install]
83
-
== How do to install Log4j?
83
+
== Prerequisites
84
84
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.
85
+
We will need a **BOM** (Bill of Materials) to manage the versions of the dependencies.
87
86
In addition, we will need the `log4j-api` dependency itself.
88
87
89
88
[tabs]
@@ -133,9 +132,9 @@ dependencies {
133
132
To write logs, you need a `Logger` instance which you will retrieve from the `LogManager`.
134
133
The `Logger` instance is thread-safe and reusable.
135
134
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.
135
+
Second, you can use the `Logger` instance to write logs by using methods like `info()`, `warn()`, `error()`, etc.
136
+
These methods are named after the log levels they represent, a way to categorize log events by severity.
137
+
The log message can also contain placeholders written as `{}` that will be replaced by the arguments passed to the method.
139
138
140
139
[source,java]
141
140
----
@@ -159,10 +158,10 @@ public class DbTableService {
159
158
The generated **log event** will be enriched with the **log level** (i.e., `WARN`),
160
159
but also timestamp, class & method name, line number, and several other information.
161
160
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`.
161
+
As already mentioned Log levels are used to categorize log events by severity and control the verbosity of the logs.
162
+
Log4j knows various levels, but the most common are `DEBUG`, `INFO`, `WARN`, and `ERROR`.
164
163
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.
164
+
Previously we used `LOGGER.warn()` to log a warning message, which could mean that something is not right, but the application can continue.
166
165
Log levels have a priority, and `WARN` is less severe than `ERROR`.
167
166
168
167
Exceptions are often also errors.
@@ -266,23 +265,30 @@ Imagine `userId` being provided by the user with the following content:
266
265
/* GOOD */ LOGGER.info("failed for user ID `{}`", userId);
267
266
----
268
267
269
-
[#integrating-log4j]
270
-
== Integrating Log4j
268
+
[#basic-log4j-architecture]
269
+
== Basic Log4j Architecture
270
+
271
+
In a nutshell, Log4j operates with two main parts: the API and the Core.
272
+
With this structure, Log4j allows you to log events using the API and route them through the Core.
271
273
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.
274
+
Optionally you can also route the log events through other logging frameworks
275
+
like SLF4J or JUL (Java Util Logging) by adding a bridge.
275
276
276
277
[#log4j-api]
277
278
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.
279
+
This is the interface that you use in your application to log events.
280
+
It needs to be available at compile-time and no configuration is needed.
281
+
By using it, you ensure that your application can write logs but is not tied to a specific logging implementation.
280
282
283
+
[#log4j-core]
281
284
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.
284
-
So your dependencies and their dependencies too.
285
-
While deploying your application, you need to provide a **logging implementation** along with its configuration to consume all generated log events.
285
+
The Log4j Core is a logging implementation that processes log events.
286
+
It is responsible for filtering, routing, encoding, and appending log events.
287
+
This needs to be available at runtime and requires configuration by the user.
288
+
When you deploy your application, you must also deploy the Log4j Core or any
289
+
other logging implementation along with its configuration to consume all generated log events
290
+
291
+
The following sections show you examples of how you can get started quickly with Log4j.
286
292
287
293
[#config-app]
288
294
== How do I configure Log4j to run my **application**?
@@ -300,7 +306,7 @@ Please skip to the xref:#config-lib[] instead.
300
306
As mentioned, Log4j is using a logging API.
301
307
First of all, add the `log4j-core` **runtime** dependency to our application.
302
308
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`.
309
+
This is the most secure way to format log events and should preferred over the default `PatternLayout`, at least for production deployments.
304
310
305
311
[tabs]
306
312
====
@@ -350,14 +356,15 @@ dependencies {
350
356
<1> Note that the logging implementation and bridges are only needed at runtime.
351
357
352
358
Now it is time to configure Log4j and instruct how the log events should be routed.
353
-
Save the following XML document to `src/**main**/resources/log4j2.xml`.
354
359
355
360
The xref:manual/json-template-layout.adoc[JSON Template Layout] is used to encode log events in JSON.
356
361
Once encoded xref:manual/appenders.adoc[Appenders] are responsible for writing log events to the console, file, socket, database, etc.
357
362
358
363
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
364
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.
360
365
366
+
Save the following XML document to `src/**main**/resources/log4j2.xml`.
367
+
361
368
.An example `src/**main**/resources/log4j2.xml`
362
369
[source,xml]
363
370
----
@@ -386,13 +393,16 @@ Finally, the `<root>` logger defines that log events of level `WARN` and higher
386
393
<1> xref:manual/appenders.adoc[Appenders] are responsible for writing log events to their target
387
394
<2> xref:manual/appenders.adoc#ConsoleAppender[Console Appender] writes logs to the console.
388
395
<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.
396
+
<4> Log events from `com.mycompany` and its sub-packages, at `INFO` level or higher, are consumed.
390
397
<5> Unless specified otherwise, log events of level `WARN` and higher will be consumed.
391
398
<6> Unless specified otherwise, log events will be forwarded to the `console` appender defined earlier.
392
399
393
400
If you want to configure Log4j for tests, you are strongly advised to use a different Log4j configuration.
394
401
Continue to xref:#config-test[]
395
402
403
+
[#integrating-log4j]
404
+
== Integrating Log4j with SLF4J
405
+
396
406
In many cases, you might have a library that logs through SLF4J.
397
407
Due to the separation of Log4js API and Core, you can add a bridge to forward SLF4J calls to the Log4j API.
398
408
This way, SLF4J calls will be processed by Log4j Core too.
@@ -503,9 +513,8 @@ dependencies {
503
513
}
504
514
----
505
515
====
506
-
<1> Note that the logging implementation and bridges are only needed for tests!
507
-
<2> SLF4J is another widely used logging API.
508
-
`log4j-slf4j2-impl` forwards SLF4J calls to Log4j API, which effectively gets processed by Log4j Core too.
516
+
<1> Note that the logging implementation and bridges are only needed for tests.
517
+
<2> `log4j-slf4j2-impl` forwards SLF4J calls to the Log4j API.
509
518
510
519
Next, you need a `src/**test**/resources/log4j2-test.xml`.
511
520
See xref:#config-test[]
@@ -515,14 +524,15 @@ See xref:#config-test[]
515
524
516
525
For tests, prefer a human-readable layout with increased verbosity.
517
526
While it is not recommended to use the `PatternLayout` in production for security reasons, it is a good choice for tests.
518
-
Save the following XML document to `src/**test**/resources/log4j2-test.xml`.
519
527
520
528
The xref:manual/layouts.adoc#PatternLayout[Pattern Layout] is used for formatting strings in a specific way.
521
529
In the below case, it will include the timestamp, thread name, log level, class name, and the message and
522
530
print it to the Console.
523
531
Very similar to the earlier configuration, the `<logger>` defines what should be logged on
524
532
which level and the `<root>` logger serves as a default configuration.
525
533
534
+
Save the following XML document to `src/**test**/resources/log4j2-test.xml`.
535
+
526
536
.An example `src/**test**/resources/log4j2-test.xml`
0 commit comments