5

The Spring docs explicitly mention that spring only depends on on commons-logging. However, if i add a dependency to Spring Data MongoDb, gradle adds a dependency on slf4j.

org.springframework.data:spring-data-commons:1.5.1.RELEASE       
+--- org.springframework:spring-core:3.1.4.RELEASE (*)           
+--- org.springframework:spring-beans:3.1.4.RELEASE (*)          
+--- org.slf4j:slf4j-api:1.7.1                                   
\--- org.slf4j:jcl-over-slf4j:1.7.1

Does this mean I am forced to use SLF4j if i use spring data?

3 Answers 3

3

Yes, we have a compile time dependency on the Slf4j API as it's the de-facto standard logging API for Java and the one causing the least hassle of all the options available: JUL - I better don't leave a word on this one (see this one if you still need to be convinced), Commons Logging - runtime provider detection has proven a PITA.

We additionally require jcl-over-slf4j to provide a Commons Logging implementation to satisfy the Commons Logging dependency of the core Spring framework, a dependency it has to maintain for legacy reasons but would have not been introduced in the first place if Slf4j had been available back in the days.

So, yes. We're setting incentives to do the "right thing" (tm), read: "the way the Java community has agreed on at large". If you really want to stick to Commons Logging, simply add the slf4j-jcl bridge and you're set. If you want to remove the jcl-over-slf4j bridge, simply exclude the dependency.

7
  • 1
    the issue is not with slf4j per se, its with the fact that the documentation doesnt match the reality. The documentation says spring uses commons-logging and need to be updated if that is not the case.
    – pdeva
    May 26, 2013 at 17:21
  • YOu didn't mention that in the question :). Care to share a link to the places you found the misleading information so that I can fix it? May 27, 2013 at 13:47
  • 1
    This is the reference documentation for Spring Framework, not Spring Data… Jun 4, 2013 at 13:55
  • 1
    yes, but spring data documentation doesnt say it changes the logging frameowrks. a newcomer to spring like myself will assume the spring framework documentation holds true for spring data too, especially considering that most spring projects will indeed use spring data
    – pdeva
    Jun 4, 2013 at 17:18
  • 4
    either way, the spring data documentation should document which logging framework is being used by it (which it currently does not)
    – pdeva
    Jul 1, 2013 at 9:25
3

You can disable SLF4J logging in Spring Data by adding to pom.xml:

    <dependency>
        <groupId>org.springframework.data</groupId>
        <artifactId>spring-data-jpa</artifactId>
        <version>${spring-data-jpa.version}</version>
        <!-- Exclude slf4j logging in favor of log4j -->
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>jcl-over-slf4j</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>commons-logging</groupId>
        <artifactId>commons-logging</artifactId>
        <version>${commons-logging.version}</version>
    </dependency>
    <dependency>
        <groupId>log4j</groupId>
        <artifactId>log4j</artifactId>
        <version>${log4j.version}</version>
    </dependency>
2

SLF4J is only a Logging Facade that in spring case will delegate in commons logging, using the jcl-over-slf4j dependency.

If you want your app to use commons-logging you can simply exclude slf4j dependencies.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.