@@ -72,27 +72,78 @@ In the case of Log4j, the API is called _Log4j API_, and its reference implement
72
72
[#logging-bridge]
73
73
Logging bridge::
74
74
Logging implementations accept input from a single logging API of their preference; Log4j Core from Log4j API, Logback from SLF4J, etc.
75
- A logging bridge is a simple logging implementation of a logging API that forward all messages to a foreign logging API.
75
+ A logging bridge is a simple logging implementation of a logging API that forwards all messages to a foreign logging API.
76
76
Logging bridges allow a logging implementation to accept input from other logging APIs that are not their primary logging API.
77
77
For instance, `log4j-slf4j2-impl` _bridges_ SLF4J calls to Log4 API and effectively enables Log4j Core to accept input from SLF4J.
78
78
79
79
With this in mind, the type of software you are writing determines whether you should be installing a logging API, implementation, or bridge:
80
80
81
81
Libraries::
82
- only require a logging API and delegate the choice of the implementation to applications.
82
+ They only require a logging API and delegate the choice of the implementation to applications.
83
83
If a logging implementation is required by tests of the library, it should be in the appropriate test scope.
84
84
85
85
Applications::
86
- need a logging implementation, but also bridges of each of the major logging APIs to support log statements from the libraries they use.
86
+ They need a logging implementation, but also bridges of each of the major logging APIs to support log statements from the libraries they use.
87
87
For instance, your application might be logging against Log4j API and one of its dependencies against SLF4J.
88
88
In this case, you need to install `log4j-core` and `log4j-slf4j2-impl`.
89
89
(This is an example, we will elaborate on this case more in <<impl-core>>.)
90
90
91
+ To make things a little bit more tangible, consider the following visualization of a typical Log4j Core installation with bridges for an application:
92
+
93
+ .Visualization of a typical Log4j Core installation with SLF4J, JUL, and JPL bridges.
94
+ [ditaa]
95
+ ....
96
+ /-----------------------------------------------------------------------------------\
97
+ | |
98
+ | +------------+ +----------+ +----------+ |
99
+ | | | | | | | |
100
+ | | v | v | v |
101
+ | +-----+-----+ +---------+ +---------+ +----+----+ +-------+ +----+----+ +-------+ |
102
+ | | | |{d}c1FF | | | | | |{d}c1FF| | | |{d}c1FF| |
103
+ | |Application| |Log4j API| |Library 1| |Library 2| | SLF4J | |Library 3| | JUL | |
104
+ | | | | | | | | | | | | | | | |
105
+ | +-----------+ +--+------+ +----+----+ +---------+ +---+---+ +---------+ +---+---+ |
106
+ | : ^ | : : |
107
+ | | | | | | |
108
+ | Compile time | | | | | |
109
+ \------------------|---|---------|----------------------|---------------------|-----/
110
+ | | | | |
111
+ | | | /-----/ /-----------/
112
+ | | | | |
113
+ /----------/ \------+--|----------+-----|---------\ |
114
+ | | | | | | |
115
+ /-------|---------------------|--|----------|-----|---------|-----|-----------------\
116
+ | | | | | | | | Runtime |
117
+ | v : | : v : v |
118
+ | +----------+ +-----+------+ +----+---------+ +---+--------+ |
119
+ | |cGRE | |cYEL | |cYEL | |cYEL | |
120
+ | |Log4j Core| |JPL to Log4j| |SLF4J to Log4j| |JUL to Log4j| |
121
+ | | | | | | | | | |
122
+ | +----------+ +------------+ +--------------+ +------------+ |
123
+ | |log4j2.xml| ^ | |
124
+ | +----------+ | | |
125
+ | | | |
126
+ | | | |
127
+ | /-------------+--|--/ |
128
+ | | | | |
129
+ | +----------+-------------|--|---------------------------------------------------+ |
130
+ | |JRE v : | |
131
+ | | +----+--+ | |
132
+ | | |{d}c1FF| | |
133
+ | | | JPL | | |
134
+ | | | | | |
135
+ | | +-------+ | |
136
+ | | | |
137
+ | +-------------------------------------------------------------------------------+ |
138
+ | |
139
+ \-----------------------------------------------------------------------------------/
140
+ ....
141
+
91
142
[#requirements]
92
143
== Requirements
93
144
94
145
The Log4j 2 runtime requires a minimum of Java {java-target-version}.
95
- See xref:index .adoc#older-releases[older releases ] for the latest releases supporting Java 6 and 7.
146
+ See xref:download .adoc#older[the Download page ] for older releases supporting Java 6 and 7.
96
147
97
148
[#build-tool]
98
149
== Configuring the build tool
@@ -195,8 +246,8 @@ Maven::
195
246
[source,xml]
196
247
----
197
248
<dependency>
198
- <groupId>org.apache.logging.log4j</groupId>
199
- <artifactId>log4j-api</artifactId>
249
+ <groupId>org.apache.logging.log4j</groupId>
250
+ <artifactId>log4j-api</artifactId>
200
251
</dependency>
201
252
----
202
253
@@ -215,6 +266,7 @@ Log4j provides several modules to facilitate deployment of different logging imp
215
266
216
267
`log4j-core`::
217
268
The reference implementation.
269
+ Log4 Core primarily accepts input from Log4j API.
218
270
Refer to <<impl-core>> for the installation instructions.
219
271
220
272
`log4j-to-jul`::
@@ -269,12 +321,16 @@ Maven::
269
321
[source,xml]
270
322
----
271
323
<dependencies>
272
- <dependency>
273
- <groupId>org.apache.logging.log4j</groupId>
274
- <artifactId>log4j-core</artifactId>
275
- <scope>runtime</scope>
276
- </dependency>
277
- <!-- Logging bridges will follow... -->
324
+
325
+ <!-- Logging implementation (Log4j Core) -->
326
+ <dependency>
327
+ <groupId>org.apache.logging.log4j</groupId>
328
+ <artifactId>log4j-core</artifactId>
329
+ <scope>runtime</scope>
330
+ </dependency>
331
+
332
+ <!-- Logging bridges will follow... -->
333
+
278
334
</dependencies>
279
335
----
280
336
@@ -291,6 +347,17 @@ runtimeOnly 'org.apache.logging.log4j:log4j-core'
291
347
==== Installing bridges
292
348
293
349
If either your application or one if its dependencies logs against a logging API that is different from Log4j API, you need to xref:#logging-bridge[bridge] that API to Log4j API.
350
+
351
+ [TIP]
352
+ ====
353
+ **Do you need bridges?
354
+ And if so, which ones?**
355
+
356
+ * If you have any direct or transitive dependency on `org.slf4j:slf4j-api`, you need xref:#impl-core-bridge-slf4j[the SLF4J-to-Log4j bridge].
357
+ * If you have any direct or transitive dependency on `commons-logging:commons-logging`, you need xref:#impl-core-bridge-jcl[the JCL-to-Log4j bridge].
358
+ * If it is a standalone application (i.e., not running in a Java EE container), you will probably need xref:#impl-core-bridge-jul[JUL-to-Log4j] and xref:#impl-core-bridge-jpl[JPL-to-Log4j] bridges.
359
+ ====
360
+
294
361
Following sections explain installation of Log4j-provided bridges.
295
362
296
363
[#impl-core-bridge-slf4j]
@@ -306,9 +373,9 @@ Maven::
306
373
----
307
374
<!-- SLF4J-to-Log4j bridge -->
308
375
<dependency>
309
- <groupId>org.apache.logging.log4j</groupId>
310
- <artifactId>log4j-slf4j2-impl</artifactId>
311
- <scope>runtime</scope>
376
+ <groupId>org.apache.logging.log4j</groupId>
377
+ <artifactId>log4j-slf4j2-impl</artifactId>
378
+ <scope>runtime</scope>
312
379
</dependency>
313
380
----
314
381
@@ -333,9 +400,9 @@ Maven::
333
400
----
334
401
<!-- JUL-to-Log4j bridge -->
335
402
<dependency>
336
- <groupId>org.apache.logging.log4j</groupId>
337
- <artifactId>log4j-jul</artifactId>
338
- <scope>runtime</scope>
403
+ <groupId>org.apache.logging.log4j</groupId>
404
+ <artifactId>log4j-jul</artifactId>
405
+ <scope>runtime</scope>
339
406
</dependency>
340
407
----
341
408
@@ -369,9 +436,9 @@ Maven::
369
436
----
370
437
<!-- JPL-to-Log4j bridge -->
371
438
<dependency>
372
- <groupId>org.apache.logging.log4j</groupId>
373
- <artifactId>log4j-jpl</artifactId>
374
- <scope>runtime</scope>
439
+ <groupId>org.apache.logging.log4j</groupId>
440
+ <artifactId>log4j-jpl</artifactId>
441
+ <scope>runtime</scope>
375
442
</dependency>
376
443
----
377
444
@@ -399,11 +466,11 @@ Maven users should add an entry to the `<dependencyManagement>` section of their
399
466
[source,xml,subs="+attributes"]
400
467
----
401
468
<dependencyManagement>
402
- <dependency>
403
- <groupId>commons-logging</groupId>
404
- <artifactId>commons-logging</artifactId>
405
- <version>{commons-logging-version}</version>
406
- </dependency>
469
+ <dependency>
470
+ <groupId>commons-logging</groupId>
471
+ <artifactId>commons-logging</artifactId>
472
+ <version>{commons-logging-version}</version>
473
+ </dependency>
407
474
</dependencyManagement>
408
475
----
409
476
@@ -431,21 +498,24 @@ Maven::
431
498
[source,xml]
432
499
----
433
500
<dependencies>
434
- <dependency>
435
- <groupId>org.springframework.boot</groupId>
436
- <artifactId>spring-boot-starter</artifactId>
437
- <exclusions>
438
- <exclusion>
439
- <groupId>org.springframework.boot</groupId>
440
- <artifactId>spring-boot-starter-logging</artifactId>
441
- </exclusion>
442
- </exclusions>
443
- </dependency>
444
- <dependency>
501
+
502
+ <dependency>
503
+ <groupId>org.springframework.boot</groupId>
504
+ <artifactId>spring-boot-starter</artifactId>
505
+ <exclusions>
506
+ <exclusion>
445
507
<groupId>org.springframework.boot</groupId>
446
- <artifactId>spring-boot-starter-log4j2</artifactId>
447
- <scope>runtime</scope>
448
- </dependency>
508
+ <artifactId>spring-boot-starter-logging</artifactId>
509
+ </exclusion>
510
+ </exclusions>
511
+ </dependency>
512
+
513
+ <dependency>
514
+ <groupId>org.springframework.boot</groupId>
515
+ <artifactId>spring-boot-starter-log4j2</artifactId>
516
+ <scope>runtime</scope>
517
+ </dependency>
518
+
449
519
</dependencies>
450
520
----
451
521
@@ -454,11 +524,11 @@ Gradle::
454
524
[source,groovy]
455
525
----
456
526
configurations {
457
- all.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
527
+ all.exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
458
528
}
459
529
460
530
dependencies {
461
- runtimeOnly group: 'org.springframework.boot', module: 'spring-boot-starter-log4j2'
531
+ runtimeOnly group: 'org.springframework.boot', module: 'spring-boot-starter-log4j2'
462
532
}
463
533
----
464
534
@@ -488,16 +558,19 @@ log4j2.xml::
488
558
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
489
559
xsi:schemaLocation="https://logging.apache.org/xml/ns
490
560
https://logging.apache.org/xml/ns/log4j-config-2.xsd">
561
+
491
562
<appenders>
492
563
<Console name="CONSOLE">
493
564
<PatternLayout pattern="%d [%t] %5p %c{1.} - %m%n"/><!--1-->
494
565
</Console>
495
566
</appenders>
567
+
496
568
<loggers>
497
569
<root level="INFO">
498
570
<AppenderRef ref="CONSOLE"/>
499
571
</root>
500
- </Loggers>
572
+ </loggers>
573
+
501
574
</Configuration>
502
575
----
503
576
@@ -578,10 +651,10 @@ log4j2.json::
578
651
[source,xml,subs="+attributes"]
579
652
----
580
653
<dependency>
581
- <groupId>com.fasterxml.jackson.core</groupId>
582
- <artifactId>jackson-databind</artifactId>
583
- <version>{jackson-version}</version>
584
- <scope>runtime</scope>
654
+ <groupId>com.fasterxml.jackson.core</groupId>
655
+ <artifactId>jackson-databind</artifactId>
656
+ <version>{jackson-version}</version>
657
+ <scope>runtime</scope>
585
658
</dependency>
586
659
----
587
660
@@ -590,10 +663,10 @@ log4j2.yaml::
590
663
[source,xml,subs="+attributes"]
591
664
----
592
665
<dependency>
593
- <groupId>com.fasterxml.jackson.dataformat</groupId>
594
- <artifactId>jackson-dataformat-yaml</artifactId>
595
- <version>{jackson-version}</version>
596
- <scope>runtime</scope>
666
+ <groupId>com.fasterxml.jackson.dataformat</groupId>
667
+ <artifactId>jackson-dataformat-yaml</artifactId>
668
+ <version>{jackson-version}</version>
669
+ <scope>runtime</scope>
597
670
</dependency>
598
671
----
599
672
@@ -653,20 +726,24 @@ Maven::
653
726
[source,xml,subs="+attributes"]
654
727
----
655
728
<dependencies>
729
+
656
730
<!-- Log4j-to-JUL bridge -->
657
- <dependency>
658
- <groupId>org.apache.logging.log4j</groupId>
659
- <artifactId>log4j-to-jul</artifactId>
660
- <scope>runtime</scope>
661
- </dependency>
662
- <!-- SLF4J-to-JUL bridge -->
663
- <dependency>
664
- <groupId>org.slf4j</groupId>
665
- <artifactId>slf4j-jdk14</artifactId>
666
- <version>{slf4j-version}</version>
667
- <scope>runtime</scope>
668
- </dependency>
669
- <!-- ... -->
731
+ <dependency>
732
+ <groupId>org.apache.logging.log4j</groupId>
733
+ <artifactId>log4j-to-jul</artifactId>
734
+ <scope>runtime</scope>
735
+ </dependency>
736
+
737
+ <!-- SLF4J-to-JUL bridge -->
738
+ <dependency>
739
+ <groupId>org.slf4j</groupId>
740
+ <artifactId>slf4j-jdk14</artifactId>
741
+ <version>{slf4j-version}</version>
742
+ <scope>runtime</scope>
743
+ </dependency>
744
+
745
+ <!-- ... -->
746
+
670
747
</dependencies>
671
748
----
672
749
@@ -695,18 +772,22 @@ Maven::
695
772
[source,xml]
696
773
----
697
774
<dependencies>
698
- <dependency>
699
- <groupId>ch.qos.logback</groupId>
700
- <artifactId>logback-classic</artifactId>
701
- <version>{logback-version}</version>
702
- <scope>runtime</scope>
703
- </dependency>
704
- <!-- Log4j-to-SLF4J bridge -->
705
- <dependency>
706
- <groupId>org.apache.logging.log4j</groupId>
707
- <artifactId>log4j-to-slf4j</artifactId>
708
- <scope>runtime</scope>
709
- </dependency>
775
+
776
+ <!-- Logging implementation (Logback) -->
777
+ <dependency>
778
+ <groupId>ch.qos.logback</groupId>
779
+ <artifactId>logback-classic</artifactId>
780
+ <version>{logback-version}</version>
781
+ <scope>runtime</scope>
782
+ </dependency>
783
+
784
+ <!-- Log4j-to-SLF4J bridge -->
785
+ <dependency>
786
+ <groupId>org.apache.logging.log4j</groupId>
787
+ <artifactId>log4j-to-slf4j</artifactId>
788
+ <scope>runtime</scope>
789
+ </dependency>
790
+
710
791
</dependencies>
711
792
----
712
793
0 commit comments