Gradle | Spring boot dependencies are not excluding
Asked Answered
F

6

17

I'm trying to get log4j to work in a project that I'm working on. I added the relevant log4j dependencies in build.gradle and excluded the Spring boot starter logging so it can work.

This worked fine when I used Maven as the build tool, but once I switched to Gradle it's not working at all (excepts the logging from Spring boot starter). Here are the dependencies in my build.gradle

dependencies {
    compile('org.springframework.boot:spring-boot-starter-data-jpa')
    compile('org.springframework.boot:spring-boot-starter-web'){
        exclude module: 'org.springframework.boot:spring-boot-starter-logging'
    }
    compile('org.springframework.boot:spring-boot-starter-log4j')
    compile('org.springframework.boot:spring-boot-starter-data-rest')
    compile('org.springframework.boot:spring-boot-starter-actuator')
    compile('org.postgresql:postgresql:9.3-1101-jdbc41')
    compile('org.scala-lang:scala-library:2.10.4')
    testCompile('org.springframework.boot:spring-boot-starter-test') {
        exclude module: 'commons-logging'
    }
    providedCompile('org.springframework.boot:spring-boot-starter-tomcat')
}

But as you can clearly see in the dependency tree the spring-boot-starter-logging is still there. I'm guessing that this is the issue why the logging isn't working.

Here's the dependency tree:

+--- org.springframework.boot:spring-boot-starter-data-jpa: -> 1.2.1.RELEASE
|    +--- org.springframework.boot:spring-boot-starter:1.2.1.RELEASE
|    |    +--- org.springframework.boot:spring-boot:1.2.1.RELEASE
|    |    |    +--- org.springframework:spring-core:4.1.4.RELEASE
|    |    |    \--- org.springframework:spring-context:4.1.4.RELEASE
|    |    |         +--- org.springframework:spring-aop:4.1.4.RELEASE
|    |    |         |    +--- aopalliance:aopalliance:1.0
|    |    |         |    +--- org.springframework:spring-beans:4.1.4.RELEASE
|    |    |         |    |    \--- org.springframework:spring-core:4.1.4.RELEASE
|    |    |         |    \--- org.springframework:spring-core:4.1.4.RELEASE
|    |    |         +--- org.springframework:spring-beans:4.1.4.RELEASE (*)
|    |    |         +--- org.springframework:spring-core:4.1.4.RELEASE
|    |    |         \--- org.springframework:spring-expression:4.1.4.RELEASE
|    |    |              \--- org.springframework:spring-core:4.1.4.RELEASE
|    |    +--- org.springframework.boot:spring-boot-autoconfigure:1.2.1.RELEASE
|    |    |    \--- org.springframework.boot:spring-boot:1.2.1.RELEASE (*)
|    |    +--- org.springframework.boot:spring-boot-starter-logging:1.2.1.RELEASE

Here's my log4j.properties file

log4j.rootLogger=INFO, fileout, CONSOLE
PID=????
LOG_PATTERN=[%d{yyyy-MM-dd HH:mm:ss.SSS}] log4j%X{context} - ${PID} %5p [%t] --- %c{1}: %m%n


# CONSOLE is set to be a ConsoleAppender using a PatternLayout.
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=${LOG_PATTERN}

# Log4j configurations for with file appender
log4j.category.org.hibernate.validator.internal.util.Version=WARN
log4j.category.org.apache.coyote.http11.Http11NioProtocol=WARN
log4j.category.org.apache.tomcat.util.net.NioSelectorPool=WARN
log4j.category.org.apache.catalina.startup.DigesterFactory=ERROR

log4j.appender.fileout=org.apache.log4j.RollingFileAppender
log4j.appender.fileout.File=sampleLog.log
log4j.appender.fileout.MaxFileSize=1024KB
log4j.appender.fileout.MaxBackupIndex=1
log4j.appender.fileout.layout=org.apache.log4j.PatternLayout
log4j.appender.fileout.layout.conversionPattern=${LOG_PATTERN}

UPDATE

I managed to fix the exclusion of jar file dependency. But the logging is still not working, log4j.properties is also in the WAR distribution under classes.

UPDATE 02

It worked issue is with the my IDE (STS)

Fortis answered 26/2, 2015 at 4:47 Comment(0)
L
55

All the spring-boot-starter-* projects are dependent on spring-boot-starter project, which in turn in dependent on spring-boot-starter-logging. I was able to remove this dependency by adding the following line in configurations section:

configurations {
    compile.exclude module: 'spring-boot-starter-logging'
}
Lyndseylyndsie answered 26/2, 2015 at 6:8 Comment(3)
It worked, spring boot logging dependency has removed, but the logging still not works.Fortis
Is the sampleLog.log file generated with no contents or is the file itself is not created?Lyndseylyndsie
On the off chance you're using another module named spring-boot-start-logging, this more explicit syntax also works: configurations { compile.exclude group:"org.springframework.boot", module: 'spring-boot-starter-logging' }Decrepitude
A
2

You have excluded the spring-boot-starter-logging module from spring-boot-starter-web, but as you clearly can see from the dependency tree, the module spring-boot-starter-data-jpa also has a dependency to spring-boot-starter-logging, so you also have to exclude it from there (and from all other spring-boot-starter-* dependencies, since all of them depend on spring-boot-starter-logging).

Adigranth answered 26/2, 2015 at 6:8 Comment(0)
T
1

I have removed all logging dependencies using the below code

configurations {
    all*.exclude module : 'spring-boot-starter-logging'
}
Tattoo answered 19/1, 2022 at 10:37 Comment(1)
The only solution that worked for me!Grabble
A
0

In my gradle / kotlin configuration I was able to exclude it this way:

configurations.implementation {
    exclude(module = "spring-boot-starter-logging")
}
Allopatric answered 22/8, 2023 at 13:50 Comment(0)
M
0

In my case this helped:

configurations.implementation {
    exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
}
Man answered 17/9, 2023 at 11:43 Comment(0)
L
0

The pattern from all suggestions is correct, but in my case, this was the working scenario:

configurations {
    all {
        exclude group: 'org.springframework.boot', module: 'spring-boot-starter-logging'
        }
}

And the version 3.3.0 of log4J2:

implementation group: 'org.springframework.boot', name: 'spring-boot-starter-log4j2', version: '3.3.0'
Luann answered 22/7 at 18:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.