scalaslf4jscala-scriptscala-cli

In Scala script, avoid "Failed to load class org.slf4j.impl.StaticLoggerBinder"


When running my Scala script, I'm seeing a warning:

> scala upload.scala

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.

Here is my script:

//> using scala "3"
//> using dep "software.amazon.awssdk:s3:2.31.10"

import software.amazon.awssdk.services.s3.S3Client

@main
def main() = S3Client.builder()

Question: How can I avoid this warning?


Solution

  • You have to select a logger implementation that follows the slf4j-api and add it as a dependency to your script. Doing something like the following should make the message not being shown

    //> using scala "3"
    //> using dep "software.amazon.awssdk:s3:2.31.11"
    //> using dep "ch.qos.logback:logback-classic:1.5.18"
    
    import software.amazon.awssdk.services.s3.S3Client
    
    @main
    def main() = S3Client.builder()
    

    The message you are seeing is just a warning. It means that there is no logger implementation for SLF4j. This is because SLF4j is just a facade.

    From home page of SLF4J

    The Simple Logging Facade for Java (SLF4J) serves as a simple facade or abstraction for various logging frameworks (e.g. java.util.logging, logback, log4j) allowing the end user to plug in the desired logging framework at deployment time.


    If you don't want to log anything, instead of a logger implementation, you can use slf4j-nop.

    //> using scala "3"
    //> using dep "software.amazon.awssdk:s3:2.31.11"
    //> using dep org.slf4j:slf4j-nop:jar:2.0.17
    
    import software.amazon.awssdk.services.s3.S3Client
    
    @main
    def main() = S3Client.builder()