LoginSignup
28
17

More than 3 years have passed since last update.

Gradle依存関係がSLF4Jを使っているときのエラー対処法

Last updated at Posted at 2017-01-30

Gradle の依存関係で追加したライブラリが SLF4J というロギングフレームワークを使っていて、
プログラムの実行時にエラーが表示されることがあります。
ここでは、その対処法についてメモしておきます。

Failed to load class

依存関係で追加したライブラリが SLF4J を使っていた場合に、以下のようなエラーを吐くことがあります。

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

これは、StaticLoggerBinder を実装したロガーがありませんという意味です。
SLF4J では、使用するログ実装の指定を StaticLoggerBinder というクラスで行なっています。
なので、StaticLoggerBinder を実装したロガー(Log4J)を依存関係に追加しましょう。

build.gradle
dependencies {
    compile 'org.slf4j:slf4j-log4j12:1.7.21'
}

WARN No appenders could be found for logger

StatucLoggerBinder を実装したロガー(Log4J)を依存関係に追加しプログラムを実行すると、以下のような警告を吐きます。

log4j:WARN No appenders could be found for logger (xxxxxxx).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

これは、ログ出力を行うクラスに対して Appender の設定がされていないという意味です。
そこで、Log4J の設定ファイルを作成しプロジェクトの src/main/resources に配置しましょう。

■ 作成する設定ファイル

開発環境ではログを出力させたいが、本番環境ではログを出力させる必要がない場合などがあるため、
ログを出力する場合としない場合の設定をメモしておきます。

ログを出力させる場合

log4j.properties
log4j.rootLogger=DEBUG, console
log4j.logger.xxx=DEBUG, console

log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%d [%-5p-%c] %m%n

ログを出力させない場合

log4j.properties
log4j.rootLogger=FATAL, null
log4j.appender.null=org.apache.log4j.varia.NullAppender
28
17
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
28
17