Error handling when Gradle dependency is using SLF4J

The library I added in Gradle's dependency uses a logging framework called SLF4J. You may see an error when you run the program. Here, make a note of how to deal with it.

 Failed to load class

If the library added as a dependency uses SLF4J, the following error may be thrown.

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.

This means that there is no logger that implements StaticLoggerBinder. In SLF4J, the log implementation to be used is specified by a class called StaticLoggerBinder. So let's add a logger (Log4J) that implements StaticLoggerBinder to the dependency.

build.gradle


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

 WARN No appenders could be found for logger

When I add a logger (Log4J) that implements StatucLoggerBinder to the dependency and execute the program, the following warning is issued.

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.

This means that Appender is not set for the class that outputs logs. So let's create a Log4J config file and place it in the project's src / main / resources.

</ i> Configuration file to be created

I want to output the log in the development environment, but in the production environment it may not be necessary to output the log, so Make a note of the settings for outputting logs and not outputting logs.

When outputting a log

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

When not outputting the log

log4j.properties


log4j.rootLogger=FATAL, null
log4j.appender.null=org.apache.log4j.varia.NullAppender

Recommended Posts

Error handling when Gradle dependency is using SLF4J
Error when using SnapKit
Error when using rails capybara
How to solve the unknown error when using slf4j in Java
Command when dependency is broken in Ubuntu 20.04
[Spring framework] HTTP request error handling by RestTemplate when using a proxy server
I get an error when adding a dependency
Suppress warning messages about SLF4J when using SSHJ
What is SLF4J?
What is Gradle?
Error handling when the maximum file size is exceeded when uploading a file with Spring Boot
[Swift] Error handling
[Ruby] Implement error handling / iteration processing using while method
No error message is displayed when create action fails