如何使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''完成,退出值非零* 尝试:使用--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]信息已注册kafka:type = kafka.Log4jController MBean(kafka.utils.Log4jControllerRegistration $)
从倒数第二行开始,推导依赖项名称,并将其添加为最后一个图像所示.
通常来说,需要正确配置类路径,以隔离单个Slf4j实现(特别是核心模块中的一个),但是应该弄乱构建脚本避免在模块内修复运行时依赖项
要修复您的记录器,您需要将 -Dlog4j.configuration = config/log4j.properties
作为VM选项传递(我认为您必须在下拉菜单中将其显示为 -cp kafka.core.main
以显示该输入)
如果要模拟服务器的实际运行时行为,并附加调试器,请设置断点,然后打开一个终端(假设您正在使用Zookeeper,并且它已经在其他地方运行了,否则您需要一个新的终端))
export KAFKA_DEBUG = y导出DEBUG_SUSPEND_FLAG = ybin/kafka-server-start config/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屋!