scala SBT-ASSEMBLY:Logback不适用于über-jar

4szc88ey  于 2022-11-09  发布在  Scala
关注(0)|答案(1)|浏览(167)

如果我在IntelliJ中运行应用程序,日志记录工作正常,但如果我运行über-jar,我会得到以下错误:

LF4J: No SLF4J providers were found.
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#noProviders for further details.

我使用以下配置使用sbt-Assembly构建我的über-jar:

lazy val app = (project in file("."))
  .settings(
    assembly / mainClass := Some("com.example.app.Main"),
    assembly / assemblyJarName := "gcm.jar",
    assembly / assemblyMergeStrategy := {
      case PathList("META-INF", xs@_*) => MergeStrategy.discard
      case PathList("reference.conf") => MergeStrategy.concat
      case x => MergeStrategy.first
    }
  )
  • LogbackScala-Logging*的依赖项为:
ThisBuild / libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.4.0"
ThisBuild / libraryDependencies += "com.typesafe.scala-logging" %% "scala-logging" % "3.9.5"

logback.xml

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration>

<configuration>
    <import class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"/>
    <import class="ch.qos.logback.core.ConsoleAppender"/>
    <import class="ch.qos.logback.core.FileAppender"/>

    <appender name="STDOUT" class="ConsoleAppender">
        <encoder class="PatternLayoutEncoder">
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{0} - %msg%n</pattern>
        </encoder>
    </appender>
    <appender name="FILE" class="FileAppender">
        <file>gcm.log</file>
        <append>true</append>
        <immediateFlush>true</immediateFlush>
        <encoder class="PatternLayoutEncoder">
            <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{0} - %msg%n</pattern>
        </encoder>
    </appender>

    <root level="debug">
        <appender-ref ref="STDOUT"/>
        <appender-ref ref="FILE"/>
    </root>
</configuration>

。。

mkshixfv

mkshixfv1#

这是因为您的合并策略丢弃了服务提供者(在META-INF中定义)。从Logback 1.2.x迁移到1.4.x时,我遇到了同样的问题。
我在sbt-assembly doc中找到的一个选项是将META-INF大小写替换为:

case PathList("META-INF", xs@_*) =>
  (xs map {_.toLowerCase}) match {
    case "services" :: xs =>
      MergeStrategy.filterDistinctLines
    case _ => MergeStrategy.discard
  }

有没有办法找出是不是这样--如果是--是什么导致了冲突?
在调试模式./sbt assembly --debug下运行可能有助于识别哪些文件被丢弃:

...
[debug] Merging 'META-INF/services/ch.qos.logback.classic.spi.Configurator' with strategy 'discard'
[debug] Merging 'META-INF/services/jakarta.servlet.ServletContainerInitializer' with strategy 'discard'
[debug] Merging 'META-INF/services/org.slf4j.spi.SLF4JServiceProvider' with strategy 'discard'
...

相关问题