完整的“Scala 日志记录”示例

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29065603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 06:58:21  来源:igfitidea点击:

Complete "Scala Logging" Example

scalalogging

提问by jans

I'm trying to use Typesafe's Scala Loggingbut couldn't get it to print any debug message. I expect Scala Logging to print debug message to the default screen but it doesn't work. A complete example would be very helpful or specific advise what to change would be great too. I use Scala 2.11. Here is what I did:

我正在尝试使用 Typesafe 的Scala Logging,但无法打印任何调试消息。我希望 Scala Logging 将调试消息打印到默认屏幕,但它不起作用。一个完整的例子会非常有帮助,或者具体建议改变什么也很好。我使用 Scala 2.11。这是我所做的:

  1. I added the dependency to build.sbt:

    libraryDependencies += "com.typesafe.scala-logging" %% "scala-logging" % "3.1.0"
    

    Even though I'm not sure if this is required, I added the following line but it didn't do any difference:

    libraryDependencies += "com.typesafe.scala-logging" % "scala-logging-slf4j_2.11" % "2.1.2"
    
  2. This is how my class looks like basically:

    import com.typesafe.scalalogging._
    
    class MyClass extends LazyLogging {
      // ...
      logger.debug("Here goes my debug message.")
      // ...
    }
    
  3. I discovered the file ./src/main/resources/logback.xml but am not sure which module installed it and if its relevant. I changed the log level to "debug" without effect.

    <configuration>
    
      <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoders are assigned the type
             ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
        <encoder>
          <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
      </appender>
    
      <logger name="scala.slick" level="DEBUG"/>
    
      <root level="debug">
        <appender-ref ref="STDOUT" />
      </root>
    </configuration>
    
  1. 我在 build.sbt 中添加了依赖项:

    libraryDependencies += "com.typesafe.scala-logging" %% "scala-logging" % "3.1.0"
    

    尽管我不确定是否需要这样做,但我添加了以下行,但没有任何区别:

    libraryDependencies += "com.typesafe.scala-logging" % "scala-logging-slf4j_2.11" % "2.1.2"
    
  2. 这就是我的班级基本上的样子:

    import com.typesafe.scalalogging._
    
    class MyClass extends LazyLogging {
      // ...
      logger.debug("Here goes my debug message.")
      // ...
    }
    
  3. 我发现了文件 ./src/main/resources/logback.xml 但不确定哪个模块安装了它以及它是否相关。我将日志级别更改为“调试”但没有效果。

    <configuration>
    
      <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <!-- encoders are assigned the type
             ch.qos.logback.classic.encoder.PatternLayoutEncoder by default -->
        <encoder>
          <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
        </encoder>
      </appender>
    
      <logger name="scala.slick" level="DEBUG"/>
    
      <root level="debug">
        <appender-ref ref="STDOUT" />
      </root>
    </configuration>
    

采纳答案by Alexey Romanov

IIRC it'll print messages starting from info level by default. To change this, you need to put logback.xmlfile into src/main/resources(or use -Dlogback.configurationFile=/path/to/config.xmlJVM parameter). See Configurationchapter in Logback documentation.

IIRC 它将默认从信息级别开始打印消息。要更改此设置,您需要将logback.xml文件放入src/main/resources(或使用-Dlogback.configurationFile=/path/to/config.xmlJVM 参数)。请参阅Logback 文档中的配置章节。

回答by Vinay

For those who're still struggling for how to make your scala-logging work in your sbt project. They just need to follow these steps:

对于那些仍在为如何在 sbt 项目中使用 Scala 日志而苦苦挣扎的人。他们只需要遵循以下步骤:

  1. Add these two dependencies in your build.sbt:

    libraryDependencies += "com.typesafe.scala-logging" %% "scala-logging" % "3.1.0"
    libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.1.2"
    
  2. Create a file logback.xmlin your /src/main/resources/ and paste below mentioned content in that file.

    <configuration>
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
            </encoder>
        </appender>
    
        <appender name="FILE" class="ch.qos.logback.core.FileAppender">
            <!-- path to your log file, where you want to store logs -->
            <file>/Users/yourusername/test.log</file>
            <append>false</append>
            <encoder>
                <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
            </encoder>
        </appender>
    
        <root level="debug">
            <appender-ref ref="STDOUT" />
            <appender-ref ref="FILE" />
        </root>
    </configuration>
    
  3. Extend your Scala class or object with trait LazyLogging:

    import com.typesafe.scalalogging.slf4j.LazyLogging
    class MyClass extends LazyLogging {
      logger.debug("This is very convenient ;-)")
    }
    
  4. It's done.

  1. 将这两个依赖项添加到您的build.sbt

    libraryDependencies += "com.typesafe.scala-logging" %% "scala-logging" % "3.1.0"
    libraryDependencies += "ch.qos.logback" % "logback-classic" % "1.1.2"
    
  2. 在您的 /src/main/resources/ 中创建一个文件logback.xml并将下面提到的内容粘贴到该文件中。

    <configuration>
        <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
            <encoder>
                <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
            </encoder>
        </appender>
    
        <appender name="FILE" class="ch.qos.logback.core.FileAppender">
            <!-- path to your log file, where you want to store logs -->
            <file>/Users/yourusername/test.log</file>
            <append>false</append>
            <encoder>
                <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
            </encoder>
        </appender>
    
        <root level="debug">
            <appender-ref ref="STDOUT" />
            <appender-ref ref="FILE" />
        </root>
    </configuration>
    
  3. 使用 trait 扩展你的 Scala 类或对象LazyLogging

    import com.typesafe.scalalogging.slf4j.LazyLogging
    class MyClass extends LazyLogging {
      logger.debug("This is very convenient ;-)")
    }
    
  4. 完成。

P.S: Only a side note that loggeris already a member of trait LazyLogging so you don't need to create it (as shown in above example)

PS:只需要说明一下logger已经是 trait LazyLogging 的成员,所以你不需要创建它(如上例所示)

回答by Joseph Sawyer

You're close, but you have to declare a loggerinstance using an SLF4J logger in the applymethod for the Loggercompanion in com.typesafe.scalalogging. In your build.sbtinclude:

你靠近,但你必须声明logger中使用SLF4J记录器实例apply的方法Logger同伴com.typesafe.scalalogging。在您的build.sbt包括:

libraryDependencies += "com.typesafe.scala-logging" %% "scala-logging" % "3.1.0"

libraryDependencies += "org.slf4j" % "slf4j-api" % "1.7.10"

Then this will work:

然后这将起作用:

import com.typesafe.scalalogging._
import org.slf4j.LoggerFactory

class MyClass extends LazyLogging {
  // ...
  val logger = Logger(LoggerFactory.getLogger(this.getClass))
  logger.debug("Here goes my debug message.")
  // ...
}

Hereis a reference for LoggerFactory. Hope it helps!

是 LoggerFactory 的参考。希望能帮助到你!