我正在制作一个小库 - 实际上是单个Flow
- 并希望从其代码中进行一些调试日志记录。该库将从基于Log4J2和Logback的应用程序使用。我希望日志记录"刚刚发生"。
我做了什么:
所有库依赖项都是"提供的":
libraryDependencies ++= Seq(
"com.typesafe.akka" %% "akka-slf4j" % AkkaVersion,
"com.typesafe.akka" %% "akka-stream" % AkkaVersion,
"com.typesafe.akka" %% "akka-http" % "10.1.12"
).map(_ % Provided) // those come from the application
我已经阅读了 Akka 日志记录(Akka 文档(,但它的版本是 2.4.20。2.6有类似的吗?
akka-slf4j
为库提供了Logging
和LoggingFactory
类。记录器在库中的初始化方式:
private val LOGGER = LoggerFactory.getLogger(this.getClass);
但是,不会发生日志记录。控制台输出,在提供 Logback 1.2.3 的主机上运行:
$ sbt test:run
[info] Loading settings for project global-plugins from plugins.sbt ...
[info] Loading global plugins from /Users/.../.sbt/1.0/plugins
[info] Loading project definition from /Users/.../myProject/project
[info] Loading settings for project ... from build.sbt ...
[info] Set current project to ... (in build file:...)
[info] running test.Main 10
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.
...
从其中一个链接:
此错误表示在类路径上找不到相应的 SLF4J 绑定。在类路径上放置一个(并且只有一个(slf4j-nop.jar,slf4j-simple.jar,slf4j-log4j12.jar,slf4j-jdk14.jar或logback-classic.jar应该可以解决问题。
如果我将slf4j-simple
添加到库依赖项中,我会得到日志,但它们没有着色。在我看来,我已经修复了库中的日志记录以使用其自己的日志记录实现 - 而不是我希望的主机。这是对的吗? 如何做到这一点?
阿卡版本是 2.6.5
原因是SLF4J似乎不在test
范围内。
用这个修复了它:
$ sbt update # download dependencies
$ mkdir lib
$ ln -s ~/.m2/repository/ch/qos/logback/logback-core/1.2.3/logback-core-1.2.3.jar lib/
$ ln -s ~/.m2/repository/ch/qos/logback/logback-classic/1.2.3/logback-classic-1.2.3.jar lib/
我在网上某处看到但从未意识到的另一种方法是描述我的问题涉及制作子项目。我发现继续使用test:run
最简单的。