@@ -714,9 +714,9 @@ into logging calls to the SLF4J API, so if other libraries in your application u
714714API, then you have a single place to configure and manage logging.
715715
716716A common choice might be to bridge Spring to SLF4J, and then provide explicit binding
717- from SLF4J to Log4J . You need to supply 4 dependencies (and exclude the existing
718- `commons-logging`): the bridge, the SLF4J API, the binding to Log4J , and the Log4J
719- implementation itself. In Maven you would do that like this
717+ from SLF4J to Log4j . You need to supply several dependencies (and exclude the existing
718+ `commons-logging`): the bridge, the SLF4J implementation for Log4j , and the Log4j
719+ implementation itself. In Maven you would do that like this:
720720
721721[source,xml,indent=0]
722722[subs="verbatim,quotes,attributes"]
@@ -736,22 +736,22 @@ implementation itself. In Maven you would do that like this
736736 <dependency>
737737 <groupId>org.slf4j</groupId>
738738 <artifactId>jcl-over-slf4j</artifactId>
739- <version>1.5.8 </version>
739+ <version>1.7.22 </version>
740740 </dependency>
741741 <dependency>
742- <groupId>org.slf4j </groupId>
743- <artifactId>slf4j-api </artifactId>
744- <version>1.5.8 </version>
742+ <groupId>org.apache.logging.log4j </groupId>
743+ <artifactId>log4j- slf4j-impl </artifactId>
744+ <version>2.7 </version>
745745 </dependency>
746746 <dependency>
747- <groupId>org.slf4j </groupId>
748- <artifactId>slf4j-log4j12 </artifactId>
749- <version>1.5.8 </version>
747+ <groupId>org.apache.logging.log4j </groupId>
748+ <artifactId>log4j-api </artifactId>
749+ <version>2.7 </version>
750750 </dependency>
751751 <dependency>
752- <groupId>log4j</groupId>
753- <artifactId>log4j</artifactId>
754- <version>1.2.14 </version>
752+ <groupId>org.apache.logging. log4j</groupId>
753+ <artifactId>log4j-core </artifactId>
754+ <version>2.7 </version>
755755 </dependency>
756756 </dependencies>
757757----
@@ -771,47 +771,72 @@ Spring), because you only want one version of that API on the classpath.
771771
772772
773773[[overview-logging-log4j]]
774- ===== Using Log4J
774+ ===== Using Log4j
775+
776+ NOTE: Log4j 1.x is EOL and the following applies to Log4j 2
777+
775778Many people use http://logging.apache.org/log4j[Log4j] as a logging framework for
776779configuration and management purposes. It's efficient and well-established, and in fact
777780it's what we use at runtime when we build and test Spring. Spring also provides some
778781utilities for configuring and initializing Log4j, so it has an optional compile-time
779782dependency on Log4j in some modules.
780783
781- To make Log4j work with the default JCL dependency ( `commons-logging`) all you need to
782- do is put Log4j on the classpath, and provide it with a configuration file (
783- `log4j.properties` or `log4j.xml` in the root of the classpath). So for Maven users this
784- is your dependency declaration:
784+ To use Log4j with JCL, all you need to do is put Log4j on the classpath and provide
785+ it with a configuration file (`log4j2.xml`, `log4j2.properties`, or other
786+ http://logging.apache.org/log4j/2.x/manual/configuration.html[supported configuration
787+ formats]). For Maven users, the minimal dependencies needed are:
788+
785789
786790[source,xml,indent=0]
787791[subs="verbatim,quotes,attributes"]
788792----
789793 <dependencies>
790794 <dependency>
791- <groupId>org.springframework </groupId>
792- <artifactId>spring -core</artifactId>
793- <version>{spring-version} </version>
795+ <groupId>org.apache.logging.log4j </groupId>
796+ <artifactId>log4j -core</artifactId>
797+ <version>2.7 </version>
794798 </dependency>
795799 <dependency>
796- <groupId>log4j</groupId>
797- <artifactId>log4j</artifactId>
798- <version>1.2.14 </version>
800+ <groupId>org.apache.logging. log4j</groupId>
801+ <artifactId>log4j-jcl </artifactId>
802+ <version>2.7 </version>
799803 </dependency>
800804 </dependencies>
801805----
802806
803- And here's a sample log4j.properties for logging to the console :
807+ If you also wish to use SLF4J, the following dependencies are also needed :
804808
805- [literal]
806- [subs="verbatim,quotes"]
809+ [source,xml,indent=0]
810+ [subs="verbatim,quotes,attributes"]
811+ ----
812+ <dependencies>
813+ <dependency>
814+ <groupId>org.apache.logging.log4j</groupId>
815+ <artifactId>log4j-slf4j-impl</artifactId>
816+ <version>2.7</version>
817+ </dependency>
818+ </dependencies>
807819----
808- log4j.rootCategory=INFO, stdout
809820
810- log4j.appender.stdout=org.apache.log4j.ConsoleAppender
811- log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
812- log4j.appender.stdout.layout.ConversionPattern=%d{ABSOLUTE} %5p %t %c{2}:%L - %m%n
821+ Here is an example `log4j2.xml` for logging to the console:
813822
814- log4j.category.org.springframework.beans.factory=DEBUG
823+ [source,xml,indent=0]
824+ [subs="verbatim,quotes,attributes"]
825+ ----
826+ <?xml version="1.0" encoding="UTF-8"?>
827+ <Configuration status="WARN">
828+ <Appenders>
829+ <Console name="Console" target="SYSTEM_OUT">
830+ <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
831+ </Console>
832+ </Appenders>
833+ <Loggers>
834+ <Logger name="org.springframework.beans.factory" level="DEBUG"/>
835+ <Root level="error">
836+ <AppenderRef ref="Console"/>
837+ </Root>
838+ </Loggers>
839+ </Configuration>
815840----
816841
817842[[overview-native-jcl]]
@@ -823,11 +848,11 @@ excluding `commons-logging` from your application is not enough in most situatio
823848
824849To be clear about this: the problems reported are usually not with JCL per se, or even
825850with `commons-logging`: rather they are to do with binding `commons-logging` to another
826- framework (often Log4J ). This can fail because `commons-logging` changed the way they do
851+ framework (often Log4j ). This can fail because `commons-logging` changed the way they do
827852the runtime discovery in between the older versions (1.0) found in some containers and
828853the modern versions that most people use now (1.1). Spring does not use any unusual
829854parts of the JCL API, so nothing breaks there, but as soon as Spring or your application
830- tries to do any logging you can find that the bindings to Log4J are not working.
855+ tries to do any logging you can find that the bindings to Log4j are not working.
831856
832857In such cases with WAS the easiest thing to do is to invert the class loader hierarchy
833858(IBM calls it "parent last") so that the application controls the JCL dependency, not
0 commit comments