如何使用 Gradle 在 IntelliJ 中设置 SLF4J

xbl*_*3kx 6 java logging slf4j

我很难在 IntelliJ 中使用 Gradle 配置 SLF4J。无论我做什么,我都会收到以下消息:

SLF4J:无法加载类“org.slf4j.impl.StaticLoggerBinder”。SLF4J:默认为无操作 (NOP) 记录器实现 SLF4J:有关更多详细信息,请参阅http://www.slf4j.org/codes.html#StaticLoggerBinder

我正在尝试登录控制台并在测试特定文件夹中的文件后。任何帮助都会很棒。

build.gradle 文件:

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) }
    }
}
Run Code Online (Sandbox Code Playgroud)

log4j.properties 文件(在 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
Run Code Online (Sandbox Code Playgroud)

Pyt*_*ner 5

由于您将 log4j.properties 文件与 SLf4J 一起使用,因此您必须使用 log4j 绑定实现。将以下代码与 Slf4j 一起使用。

compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.29'
Run Code Online (Sandbox Code Playgroud)

或者implementation,如果您使用更高版本的 gradle,则可以使用compile group 代替。

implementation 'org.slf4j:slf4j-log4j1:1.7.29'
Run Code Online (Sandbox Code Playgroud)


Mic*_*ael 5

您的类路径上需要一个日志记录框架。SLF4J 是一个支持多种实现(logback、log4j 等)的日志门面。但是,如果您不包含特定的后端,SLF4J 默认为 NOP 实现,它会忽略所有内容。:)

如果要使用log4j,则需要为它包含一个绑定,如下所示:

compile group: 'org.slf4j', name: 'slf4j-log4j12', version: '1.7.29'
Run Code Online (Sandbox Code Playgroud)

有关更多信息,请参阅http://www.slf4j.org/manual.html#swapping

  • 由于 log4j 1.x 已[生命结束](http://logging.apache.org/log4j/1.2/index.html) 人们应该使用 [log4j 2 绑定](https://mvnrepository.com/工件/org.apache.logging.log4j/log4j-slf4j-impl): `org.apache.logging.log4j:log4j-slf4j-impl:2.14.1` (4认同)