15

I'm having a hard time configuring SLF4J with Gradle in IntelliJ. No matter what I do I get this message:

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.

I'm trying to log in to the console and after testing to a file in a specific folder. Any help would be great.

build.gradle file:

plugins {
    id 'java'
    id 'org.openjfx.javafxplugin' version '0.0.8'
    id 'application'
}

version '1.0-SNAPSHOT'

sourceCompatibility = 1.8

repositories {
    mavenCentral()
    jcenter()
    google()
}

dependencies {
    compile group: 'com.googlecode.json-simple', name: 'json-simple', version: '1.+'
    testCompile group: 'junit', name: 'junit', version: '4.12'
    compile group: 'org.apache.poi', name: 'poi', version: '4.+'
    compile group: 'org.apache.poi', name: 'poi-ooxml', version: '4.+'
    compile group: 'org.slf4j', name: 'slf4j-api', version: '1.7.+'
    // compile group: 'ch.qos.logback', name: 'logback-classic', version: '1.+'
    implementation 'com.google.firebase:firebase-admin:6.11.0'
}
javafx {
    version = '12'
    modules = ['javafx.controls', 'javafx.fxml']
}

mainClassName = 'ui.Main'
apply plugin: 'org.openjfx.javafxplugin'

apply plugin: 'idea'

jar {
    manifest {
        attributes 'Main-Class': mainClassName
    }
    from {
        configurations.runtimeClasspath.collect { it.isDirectory() ? it : zipTree(it) }
    }
}

log4j.properties file (in src/main/resources/):

# Define the root logger with appender file
log4j.rootLogger = DEBUG, FILE

# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${log}/log.out

# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.conversionPattern=%m%n
xblaz3kx
  • 341
  • 1
  • 3
  • 15
  • 2
    You have `ch.qos.logback:logback-classic` commented out, but your `.properties` is from `org.slf4j:slf4j-log4j12`. They are both are implementations of logging API. Use the answer @Michael provided. :) – Xobotun Dec 04 '19 at 14:36

4 Answers4

11

You need a logging framework on your classpath. SLF4J is a logging facade that supports multiple implementations (logback, log4j etc.). However, if you don't include a specific backend, SLF4J defaults to a NOP implementation that simply ignores everything. :)

If you want to use log4j, you need to include a binding for it like this:

compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.29'

For more information, see http://www.slf4j.org/manual.html#swapping

Michael
  • 1,044
  • 5
  • 9
  • 1
    I can't believe I didn't notice I didn't include slf4j-log4j12 but only slf4j-api. – xblaz3kx Dec 04 '19 at 14:37
  • That happens. ;) – Michael Dec 04 '19 at 14:37
  • 3
    Since log4j 1.x is [end of life'd](http://logging.apache.org/log4j/1.2/index.html) people should use the [log4j 2 binding](https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-slf4j-impl): `org.apache.logging.log4j:log4j-slf4j-impl:2.14.1` – Karl Molina Aug 09 '21 at 21:53
  • Karl Molina, I think your advice is wrong. I get requirements to use minimum api26 in Android. Seems like backward compliance is missing? – carl Jun 06 '23 at 08:51
8

Since you are using log4j.properties file along with SLf4J, you have to use the log4j binding implementation. Use the below code along with Slf4j.

compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.29'

or you can use implementation instead of compile group if you are using higher version of gradle.

implementation 'org.slf4j:slf4j-log4j1:1.7.29'
PythonLearner
  • 1,416
  • 7
  • 22
3

Add

// https://mvnrepository.com/artifact/org.slf4j/slf4j-jdk14
Compile group: 'org.slf4j', name: 'slf4j-jdk14', version: '1.7.5'

as a dependency and have it a try.

dkb
  • 4,389
  • 4
  • 36
  • 54
3

response from PythonLearner is correct, but using implementation you need this:

implementation 'org.slf4j:slf4j-log4j12:1.7.29'