A clever way to unify loggers to SLF4J in Java / build.gradle entry / 2019

This article is for people who want to use SLF4J.

Update

Tips

About exclusion

Bulk exclusion can be done with the following syntax

configurations.all {
    exclude
}

Unification of dependent versions

[3 \ .2 . Using Spring Boot's dependency management in isolation](https://docs.spring.io/spring-boot/docs/current/gradle-plugin/reference/html/#managing-dependencies-using-in -isolation) is convenient

Even if you don't use Spring Boot, you can think of it as a Gradle Plugin and use it.

build.gradle


plugins {
	id 'org.springframework.boot' version '2.1.8.RELEASE' apply false
}

apply plugin: 'io.spring.dependency-management'

dependencyManagement {
	imports {
		mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
	}
}

with this

build.gradle


dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-web'
}

I get angry when I have multiple SLF4J jars About Class path contains multiple SLF4J bindings.

It seems to be an issue of IntelliJ, but if you are using SLF4J in the dependent library and you install another version of the library with runtime, there is a problem that both Jar are included in Classpath when Debug / Run from within IntelliJ. It seems. ..

Basically, it's okay if you have dependencyManagement.

TL;DR

Pattern 1

All the projects should have various log libraries> Output with SLF4J, and the necessary modules should be included, and the implementation of various log libraries should be excluded.

build.gradle


buildscript {
    repositories {
        maven {
            url "https://plugins.gradle.org/m2/"
        }
    }
    dependencies {
        classpath "org.springframework.boot:spring-boot-gradle-plugin:2.1.8.RELEASE"
    }
}

allprojects {
    apply plugin: "io.spring.dependency-management"

    repositories {
        mavenCentral()
    }

    dependencyManagement {
        imports {
            mavenBom org.springframework.boot.gradle.plugin.SpringBootPlugin.BOM_COORDINATES
        }
    }
}

apply plugin: 'java'

dependencies {
    // Used compile/coding time.
    implementation 'org.springframework.boot:spring-boot-starter-logging' //Actually starter-Often in web dependencies

    // Used Runtime
    runtimeOnly 'org.slf4j:log4j-over-slf4j'
    // log4j 1.Only x needs to be routed individually
    //Spring in 2017 for EOL in 2015-boot-starter-Also removed from logging
    // https://github.com/spring-projects/spring-boot/issues/11148

    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

configurations.all {
    exclude group: 'log4j' // = Old only log4j impl.
    exclude group: 'org.apache.logging.log4j', module: 'log4j-slf4j-impl'// = SLF4J > Log4J 2.x. (Apache Side)
    exclude group: 'org.slf4j', module: 'slf4j-log4j12' // = SLF4J > Log4J 1.x. (SLF4J Side)
    exclude group: 'org.slf4j', module: 'slf4j-jdk14' // = SLF4J > JDK14.
    exclude group: 'commons-logging', module: 'commons-logging-api' // Replaced by jcl-over-slf4j
    exclude group: 'commons-logging', module: 'commons-logging' // Implementation is not needed.
}

Pattern 2

It is also recommended to create a log-only project that includes log settings, etc.

Write later

build.gradle



log/build.gradle


Recommended Posts

A clever way to unify loggers to SLF4J in Java / build.gradle entry / 2019
Two ways to start a thread in Java + @
How to display a web page in Java
Code to escape a JSON string in Java
Try to create a bulletin board in Java
I wanted to make (a == 1 && a == 2 && a == 3) true in Java
How to create a Java environment in just 3 seconds
I tried to create a Clova skill in Java
How to create a data URI (base64) in Java
I tried to make a login function in Java
How to convert A to a and a to A using AND and OR in Java
How to convert a file to a byte array in Java
Try to solve a restricted FizzBuzz problem in Java
Convert a Java byte array to a string in hexadecimal notation
Easy way to check method / field list in Java REPL
[Beginner] I made a program to sell cakes in Java
I just wanted to make a Reactive Property in Java
How to store a string from ArrayList to String in Java (Personal)
Create a method to return the tax rate in Java
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I tried to convert a string to a LocalDate type in Java
How to develop and register a Sota app in Java
How to simulate uploading a post-object form to OSS in Java
I tried to make a client of RESAS-API in Java
Find a subset in Java
[Rails] A simple way to implement a self-introduction function in your profile
How to implement a job that uses Java API in JobScheduler
How to create a new Gradle + Java + Jar project in Intellij 2016.03
How to automatically operate a screen created in Java on Windows
Cast an array of Strings to a List of Integers in Java
How to solve the unknown error when using slf4j in Java
How to get JDK 11 on your mac in a comfortable way
3 Implement a simple interpreter in Java
Multithreaded to fit in [Java] template
I created a PDF in Java.
How to make a Java container
How to learn JAVA in 7 days
[Java] How to create a folder
Log output to file in Java
A simple sample callback in Java
How to use classes in Java?
How to name variables in Java
Try to implement Yubaba in Java
Get stuck in a Java primer
How to make a Java array
How to concatenate strings in java
How to test a private method in Java and partially mock that method
A standalone Java app that sends logs to CloudWatch Logs with slf4j / logback
[Personal memo] How to interact with a random number generator in Java
The story of forgetting to close a file in Java and failing
To create a Zip file while grouping database search results in Java
Even in Java, I want to output true with a == 1 && a == 2 && a == 3 (PowerMockito edition)
I wanted to implement a slide show in a fashionable way with slick.
I thought about the best way to create a ValueObject in Ruby
How to pass a proxy when throwing REST over SSL in Java
How to get the absolute path of a directory running in Java
I tried to make a talk application in Java using AI "A3RT"
I want to ForEach an array with a Lambda expression in Java
[Java] [For beginners] How to insert elements directly in a 2D array
Set up a Java GUI in a separate thread to keep the main