为什么我无法加载 class org.slf4j.impl.StaticLoggerBinder

Why can't I load class org.slf4j.impl.StaticLoggerBinder

我正在学习 Akka 的视频课程,但无法使日志记录正常工作。我使用 IntelliJ。我尝试了一些不同版本的 slf4j ...并且我有 pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>AkkaPrime2</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
    <akka.version>2.6.8</akka.version>
    <scala.version>2.13</scala.version>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
    <dependency>
        <groupId>com.typesafe.akka</groupId>
        <artifactId>akka-actor-typed_2.13</artifactId>
        <version>${akka.version}</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.typesafe.akka/akka-actor-testkit-typed -->
    <dependency>
        <groupId>com.typesafe.akka</groupId>
        <artifactId>akka-actor-testkit-typed_2.13</artifactId>
        <version>${akka.version}</version>
        <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.2.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.6.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.6.2</version>
        <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.slf4j/slf4j-api -->
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.25</version>
    </dependency>
</dependencies>
</project>

代码使用的是“普通”akka 方法。

当代码有运行:

.onAnyMessage(message ->{
  System.out.println("I received the message : " + message);
  return this;
})

它有效,但我明白了

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation

(和日志消息)

之后将执行此代码

.onMessageEquals("create a child", () -> {
  ActorRef<String> secondActor = getContext().spawn(FirstSimpleBehavior.create(),"second actor");
  secondActor.tell("who are you");
  return this;
})

但是控制台显示:

SLF4J: Failed to load class "org.slf4j.impl.StaticMDCBinder".
SLF4J: Defaulting to no-operation MDCAdapter implementation.

我发现方法 spawn() 不接受我的第二个参数,因为它包含一个 space(我在视频中错过了)。在途中发现 Logback 已经包含 slf4j (link) 所以 (slf4j) 我从 pom 中删除了。

我摆弄了 pom,所以现在 logback 的版本改变了......但是 pom 现在是:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>org.example</groupId>
<artifactId>AkkaPrime2</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
    <akka.version>2.6.8</akka.version>
    <scala.version>2.13</scala.version>
    <maven.compiler.source>11</maven.compiler.source>
    <maven.compiler.target>11</maven.compiler.target>
</properties>
<dependencies>
    <dependency>
        <groupId>com.typesafe.akka</groupId>
        <artifactId>akka-actor-typed_2.13</artifactId>
        <version>${akka.version}</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/com.typesafe.akka/akka-actor-testkit-typed -->
    <dependency>
        <groupId>com.typesafe.akka</groupId>
        <artifactId>akka-actor-testkit-typed_2.13</artifactId>
        <version>${akka.version}</version>
        <scope>test</scope>
    </dependency>
    <!-- https://mvnrepository.com/artifact/ch.qos.logback/logback-classic -->
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.1.9</version>
        <!--scope>test</scope-->
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-api</artifactId>
        <version>5.6.2</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
        <version>5.6.2</version>
        <scope>test</scope>
    </dependency>
</dependencies>
</project>

在baeldung.com没有“范围”标签所以我没有使用(我也不知道为什么要使用它)。