Skip to content

Commit 261b894

Browse files
committed
worked in few comments
1 parent 4753940 commit 261b894

File tree

1 file changed

+37
-27
lines changed

1 file changed

+37
-27
lines changed

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

+37-27
Original file line numberDiff line numberDiff line change
@@ -80,10 +80,9 @@ It will:
8080
* Write only some of the messages, using a **filter** (e.g. filter by severity, content, etc.)
8181
8282
[#install]
83-
== How do to install Log4j?
83+
== Prerequisites
8484
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.
8786
In addition, we will need the `log4j-api` dependency itself.
8887
8988
[tabs]
@@ -133,9 +132,9 @@ dependencies {
133132
To write logs, you need a `Logger` instance which you will retrieve from the `LogManager`.
134133
The `Logger` instance is thread-safe and reusable.
135134
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.
139138
140139
[source,java]
141140
----
@@ -159,10 +158,10 @@ public class DbTableService {
159158
The generated **log event** will be enriched with the **log level** (i.e., `WARN`),
160159
but also timestamp, class & method name, line number, and several other information.
161160
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`.
164163
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.
166165
Log levels have a priority, and `WARN` is less severe than `ERROR`.
167166
168167
Exceptions are often also errors.
@@ -266,23 +265,30 @@ Imagine `userId` being provided by the user with the following content:
266265
/* GOOD */ LOGGER.info("failed for user ID `{}`", userId);
267266
----
268267
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.
271273
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.
275276
276277
[#log4j-api]
277278
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.
280282
283+
[#log4j-core]
281284
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.
286292
287293
[#config-app]
288294
== How do I configure Log4j to run my **application**?
@@ -300,7 +306,7 @@ Please skip to the xref:#config-lib[] instead.
300306
As mentioned, Log4j is using a logging API.
301307
First of all, add the `log4j-core` **runtime** dependency to our application.
302308
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.
304310
305311
[tabs]
306312
====
@@ -350,14 +356,15 @@ dependencies {
350356
<1> Note that the logging implementation and bridges are only needed at runtime.
351357
352358
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`.
354359
355360
The xref:manual/json-template-layout.adoc[JSON Template Layout] is used to encode log events in JSON.
356361
Once encoded xref:manual/appenders.adoc[Appenders] are responsible for writing log events to the console, file, socket, database, etc.
357362
358363
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.
359364
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.
360365
366+
Save the following XML document to `src/**main**/resources/log4j2.xml`.
367+
361368
.An example `src/**main**/resources/log4j2.xml`
362369
[source,xml]
363370
----
@@ -386,13 +393,16 @@ Finally, the `<root>` logger defines that log events of level `WARN` and higher
386393
<1> xref:manual/appenders.adoc[Appenders] are responsible for writing log events to their target
387394
<2> xref:manual/appenders.adoc#ConsoleAppender[Console Appender] writes logs to the console.
388395
<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.
390397
<5> Unless specified otherwise, log events of level `WARN` and higher will be consumed.
391398
<6> Unless specified otherwise, log events will be forwarded to the `console` appender defined earlier.
392399
393400
If you want to configure Log4j for tests, you are strongly advised to use a different Log4j configuration.
394401
Continue to xref:#config-test[]
395402
403+
[#integrating-log4j]
404+
== Integrating Log4j with SLF4J
405+
396406
In many cases, you might have a library that logs through SLF4J.
397407
Due to the separation of Log4js API and Core, you can add a bridge to forward SLF4J calls to the Log4j API.
398408
This way, SLF4J calls will be processed by Log4j Core too.
@@ -503,9 +513,8 @@ dependencies {
503513
}
504514
----
505515
====
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.
509518
510519
Next, you need a `src/**test**/resources/log4j2-test.xml`.
511520
See xref:#config-test[]
@@ -515,14 +524,15 @@ See xref:#config-test[]
515524
516525
For tests, prefer a human-readable layout with increased verbosity.
517526
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`.
519527
520528
The xref:manual/layouts.adoc#PatternLayout[Pattern Layout] is used for formatting strings in a specific way.
521529
In the below case, it will include the timestamp, thread name, log level, class name, and the message and
522530
print it to the Console.
523531
Very similar to the earlier configuration, the `<logger>` defines what should be logged on
524532
which level and the `<root>` logger serves as a default configuration.
525533
534+
Save the following XML document to `src/**test**/resources/log4j2-test.xml`.
535+
526536
.An example `src/**test**/resources/log4j2-test.xml`
527537
[source,xml]
528538
----

0 commit comments

Comments
 (0)