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

xpcnnkqh  于 2022-11-06  发布在  其他
关注(0)|答案(1)|浏览(220)

我正在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

(and日志消息)
之后,将执行此代码

.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.
uxhixvfz

uxhixvfz1#

我发现方法 spawn() 不接受我的第二个参数,因为它包含了一个空格(我在视频中没有看到)。
我摆弄了一下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上没有“scope”标记,所以我不使用(我也不知道为什么要使用它)。

相关问题