Skip to content

Instantly share code, notes, and snippets.

@wreulicke
Created March 31, 2023 17:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wreulicke/7d6f74328d54e98b74593a359737d653 to your computer and use it in GitHub Desktop.
Save wreulicke/7d6f74328d54e98b74593a359737d653 to your computer and use it in GitHub Desktop.
slf4j経由でLog4j2を使う時のbuild.gradleとmain
dependencies {
implementation platform('org.apache.logging.log4j:log4j-bom:2.20.0')
implementation 'org.slf4j:slf4j-api:2.0.7'
// Platform Logging API => slf4j
implementation 'org.slf4j:slf4j-jdk-platform-logging:2.0.7'
// java.util.Logging => slf4j
implementation 'org.slf4j:jul-to-slf4j:2.0.7'
// Apache Commons Logging => slf4j
implementation 'org.slf4j:jcl-over-slf4j:2.0.7'
// slf4jのlog4j2の実装
implementation 'org.apache.logging.log4j:log4j-slf4j2-impl'
// log4j2 本体
implementation 'org.apache.logging.log4j:log4j-core'
// log4j1 => log4j2
implementation 'org.apache.logging.log4j:log4j-1.2-api'
// ここでは使っていないが、上じゃなくてこっちでもいい
// log4j1 => slf4j
// implementation 'org.slf4j:log4j-over-slf4j:2.0.7'
}
configurations.all {
// inspired by https://qiita.com/kazuki-ma/items/13c426e4dccf5cc4d843
exclude group: "log4j" // = Old Log4j (1.x) implementation. Replaced by log4j-over-slf4j.
exclude module: "slf4j-log4j12" // = SLF4J -> Log4J Implementation by SLF4J. Not used (or cyclic dependencies lol).
exclude module: "slf4j-jdk14" // = SLF4J -> JDK14 Binding. Not used.
exclude module: "slf4j-jcl" // = SLF4J -> Commons Logging. Not used.
exclude module: "commons-logging" // Because bridged by org.slf4j:jcl-over-slf4j.
exclude module: "commons-logging-api" // Replaced by org.slf4j:jcl-over-slf4j.
}
import org.slf4j.bridge.SLF4JBridgeHandler;
public class Main {
public static void main(String[] args) {
// java.util.loggingのロガーをslf4jに入れ替えている
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment