如何让 IntelliJ IDEA 加载 org.slf4j.impl.StaticLoggerBinder 以运行 Kafka? [英] How to make IntelliJ IDEA load org.slf4j.impl.StaticLoggerBinder in order to run Kafka?
问题描述
我想从 IDEA 运行 Kafka,但出现以下错误:
<代码>>任务 :core:Kafka.main()SLF4J:无法加载类org.slf4j.impl.StaticLoggerBinder".SLF4J:默认为无操作 (NOP) 记录器实现SLF4J:有关更多详细信息,请参阅 http://www.slf4j.org/codes.html#StaticLoggerBinder.SLF4J:无法加载类org.slf4j.impl.StaticMDCBinder".SLF4J:默认为无操作 MDCAdapter 实现.SLF4J:有关更多详细信息,请参阅 http://www.slf4j.org/codes.html#no_static_mdc_binder.>任务 :core:Kafka.main() 失败任务 ':core:Kafka.main()' 执行失败.>进程 'command'/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/bin/java'' 以非零退出值 1 结束* 尝试:使用 --stacktrace 选项运行以获取堆栈跟踪.使用 --info 或 --debug 选项运行以获得更多日志输出.使用 --scan 运行以获得完整的见解.
我可以从终端运行 Kafka.我从终端运行 Zookeeper,然后从 IDEA 运行 Kafka.
运行Kafka的步骤
像往常一样,我使用终端运行命令 ./gradlew jar
从源代码构建 Kafka.我在 IDEA 中使用 idea .
从克隆存储库的根目录打开项目.
我打开文件:core/src/main/scala/kafka/Kafka.scala
然后我导航到 main()
函数并点击绿色三角形.
这有助于我获得运行配置,但它失败了.然后我在程序参数中添加 config/server.properties
以使运行配置如下所示:
使用上述配置运行后,出现上述错误.
我搜索了一下,发现通过添加依赖项解决了同样的问题,就像提到的
这是我从终端运行 Kafka 时发现的:
SLF4J:类路径包含多个 SLF4J 绑定.SLF4J:在 [jar:file:/Users/aviralsrivastava/dev/kafka/core/build/dependant-libs-2.13.5/slf4j-log4j12-1.7.30.jar!/org/slf4j/impl/StaticLoggerBinder.班级]SLF4J:在 [jar:file:/Users/aviralsrivastava/dev/kafka/tools/build/dependant-libs-2.13.5/slf4j-log4j12-1.7.30.jar!/org/slf4j/impl/StaticLoggerBinder.班级]SLF4J:在 [jar:file:/Users/aviralsrivastava/dev/kafka/connect/api/build/dependant-libs/slf4j-log4j12-1.7.30.jar!/org/slf4j/impl/StaticLoggerBinder.class] 中找到绑定SLF4J:在 [jar:file:/Users/aviralsrivastava/dev/kafka/connect/transforms/build/dependant-libs/slf4j-log4j12-1.7.30.jar!/org/slf4j/impl/StaticLoggerBinder.class] 中找到绑定SLF4J:在 [jar:file:/Users/aviralsrivastava/dev/kafka/connect/runtime/build/dependant-libs/slf4j-log4j12-1.7.30.jar!/org/slf4j/impl/StaticLoggerBinder.class] 中找到绑定SLF4J:在 [jar:file:/Users/aviralsrivastava/dev/kafka/connect/file/build/dependant-libs/slf4j-log4j12-1.7.30.jar!/org/slf4j/impl/StaticLoggerBinder.class] 中找到绑定SLF4J:在 [jar:file:/Users/aviralsrivastava/dev/kafka/connect/mirror/build/dependant-libs/slf4j-log4j12-1.7.30.jar!/org/slf4j/impl/StaticLoggerBinder.class] 中找到绑定SLF4J:在 [jar:file:/Users/aviralsrivastava/dev/kafka/connect/mirror-client/build/dependant-libs/slf4j-log4j12-1.7.30.jar!/org/slf4j/impl/StaticLoggerBinder.班级]SLF4J:在 [jar:file:/Users/aviralsrivastava/dev/kafka/connect/json/build/dependant-libs/slf4j-log4j12-1.7.30.jar!/org/slf4j/impl/StaticLoggerBinder.class] 中找到绑定SLF4J:在 [jar:file:/Users/aviralsrivastava/dev/kafka/connect/basic-auth-extension/build/dependant-libs/slf4j-log4j12-1.7.30.jar!/org/slf4j/impl/中找到绑定StaticLoggerBinder.class]SLF4J:有关解释,请参阅 http://www.slf4j.org/codes.html#multiple_bindings.SLF4J:实际绑定类型为 [org.slf4j.impl.Log4jLoggerFactory][2021-03-23 10:54:59,524] INFO 注册 kafka:type=kafka.Log4jController MBean (kafka.utils.Log4jControllerRegistration$)
从倒数第二行,我推导出依赖名称并添加它,如最后一张图片所示.
一般来说,classpath 需要正确配置,以隔离单个 Slf4j 实现(具体来说是核心模块中的一个),但是搞乱构建脚本应该避免在模块中修复你的运行时依赖
要修复您的记录器,您需要传入 -Dlog4j.configuration=config/log4j.properties
作为 VM 选项(我认为您必须切换下拉菜单-cp kafka.core.main
获取输入以显示)
如果您想模拟服务器的实际运行时行为,并附加调试器、设置断点、打开终端(假设您正在使用 Zookeeper,并且它已经在其他地方运行,否则您需要一个新的终端))
export KAFKA_DEBUG=y导出 DEBUG_SUSPEND_FLAG=ybin/kafka-server-start 配置/server.properties
然后为远程应用程序添加运行配置并将其附加到端口 5005
附加后,您的断点应该成为焦点,您可以单步执行代码
I want to run Kafka from IDEA and I am getting the following error:
> Task :core:Kafka.main()
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.
SLF4J: Failed to load class "org.slf4j.impl.StaticMDCBinder".
SLF4J: Defaulting to no-operation MDCAdapter implementation.
SLF4J: See http://www.slf4j.org/codes.html#no_static_mdc_binder for further details.
> Task :core:Kafka.main() FAILED
Execution failed for task ':core:Kafka.main()'.
> Process 'command '/Library/Java/JavaVirtualMachines/adoptopenjdk-8.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.
I am able to run Kafka from the terminal. I run Zookeeper from terminal and then Run Kafka from IDEA.
Steps that I follow to run Kafka
As usual, I run the command ./gradlew jar
to build Kafka from the source, using the terminal.
I open the project in IDEA using idea .
from the root directory of the cloned repository.
I opened the file: core/src/main/scala/kafka/Kafka.scala
I then navigate to main()
function and click on the green triangle.
This helps me get the Run configurations and it fails. I then add the config/server.properties
in the Program Arguments to make the Run config look like this:
Upon running with the above configurations, I get the aforementioned error.
I searched a bit and found that the same issue was resolved by adding dependencies, like mentioned here and here but I could not understand how I can add dependency as I do not use Maven and I cannot find the file pom.xml
file as described here.
Update 1
I tried to add the exact dependency as stated from the terminal to the Run configurations, I am unsure whether that is added or not because the result is still the same:
This is what I find when I run Kafka from the terminal:
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/Users/aviralsrivastava/dev/kafka/core/build/dependant-libs-2.13.5/slf4j-log4j12-1.7.30.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/aviralsrivastava/dev/kafka/tools/build/dependant-libs-2.13.5/slf4j-log4j12-1.7.30.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/aviralsrivastava/dev/kafka/connect/api/build/dependant-libs/slf4j-log4j12-1.7.30.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/aviralsrivastava/dev/kafka/connect/transforms/build/dependant-libs/slf4j-log4j12-1.7.30.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/aviralsrivastava/dev/kafka/connect/runtime/build/dependant-libs/slf4j-log4j12-1.7.30.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/aviralsrivastava/dev/kafka/connect/file/build/dependant-libs/slf4j-log4j12-1.7.30.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/aviralsrivastava/dev/kafka/connect/mirror/build/dependant-libs/slf4j-log4j12-1.7.30.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/aviralsrivastava/dev/kafka/connect/mirror-client/build/dependant-libs/slf4j-log4j12-1.7.30.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/aviralsrivastava/dev/kafka/connect/json/build/dependant-libs/slf4j-log4j12-1.7.30.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/Users/aviralsrivastava/dev/kafka/connect/basic-auth-extension/build/dependant-libs/slf4j-log4j12-1.7.30.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
[2021-03-23 10:54:59,524] INFO Registered kafka:type=kafka.Log4jController MBean (kafka.utils.Log4jControllerRegistration$)
From the second last line, I deduced the dependency name and added it as shown in the last image.
Generally speaking, the classpath needs configured correctly, to isolate a single Slf4j implementation (the one in the core module, specifically), but messing with the build scripts should be avoided to fix your runtime dependencies within a module
To fix your logger, you need to pass in -Dlog4j.configuration=config/log4j.properties
as a VM option (I think you'll have to toggle the drop-down where it says -cp kafka.core.main
to get that input to show)
If you want to emulate actual runtime behavior of the server, and attach a debugger, setup your breakpoints, open a terminal (assuming you are using Zookeeper, and it is already running somewhere else, otherwise you need a new terminal for it)
export KAFKA_DEBUG=y
export DEBUG_SUSPEND_FLAG=y
bin/kafka-server-start config/server.properties
Then add a run configuration for a remote application and attach it to port 5005
Once it attaches, your breakpoint should take focus, and you can step through the code
这篇关于如何让 IntelliJ IDEA 加载 org.slf4j.impl.StaticLoggerBinder 以运行 Kafka?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!