如何使用Akka和Sl4j日志记录设置Scala hello world应用程序?

时间:2016-09-24 04:54:51

标签: scala logging akka slf4j

我对编程并不陌生,但是围绕日志记录的不同内容的数量让我头疼。

任务:使用侦听RabbitMQ消息的actor获取基本Scala应用程序守护程序。

现在我不需要RabbitMQ或Akka部分的帮助,我只需要使用现代(稳定的,标准的)日志库(如slf4j)设置日志记录到文件和控制台(换句话说,没有随机的人' s github帐户)。一些将长期维持的东西。

这是我到目前为止所做的事情:

/build.sbt

 name := "BOGen"

version := "1.0"

scalaVersion := "2.11.8"

resolvers += "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/"

libraryDependencies ++= Seq {
  "com.typesafe.akka" % "akka-actor" % "2.0"
  "com.typesafe.scala-logging" %% "scala-logging-slf4j" % "2.1.2"
  "com.typesafe" %% "scalalogging-slf4j" % "1.0.1"
  "org.slf4j" % "slf4j-api" % "1.7.1"
  "org.slf4j" % "log4j-over-slf4j" % "1.7.1"
  "ch.qos.logback" % "logback-classic" % "1.0.3"
}

的src /主/阶/ main.scala

package com.mycom.mydepartment.someapp


import com.typesafe.scalalogging.slf4j.LazyLogging

object Main extends LazyLogging {

  def main(args: Array[String]) {
    logger.info("Hello From My app!") // this should log to console and file
  }

}

的src /主/资源/ logback.xml

<configuration>
    <appender name="FILE" class="ch.qos.logback.core.FileAppender">
        <File>./logs/myapp.log</File>
        <encoder>
            <pattern>%d{HH:mm:ss.SSS} [%-5level] %msg%n</pattern>
        </encoder>
    </appender>
    <root level="info">
        <appender-ref ref="FILE" />
    </root>
</configuration>

的src /主/ resoruces / application.conf

akka {
  event-handlers = ["akka.event.slf4j.Slf4jLogger"]
  loglevel = "INFO"
}

我在此设置中遇到的当前错误:

Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=256m; support was removed in 8.0
[info] Loading global plugins from C:\Users\dana.murad\.sbt\0.13\plugins
[info] Loading project definition from C:\PROJECTS\active\BOGen\project
[info] Set current project to BOGen (in build file:/C:/PROJECTS/active/BOGen/)
[info] Compiling 1 Scala source to C:\PROJECTS\active\BOGen\target\scala-2.11\classes...
[error] C:\PROJECTS\active\BOGen\src\main\scala\com\echostar\sse\voldemort\main.scala:4: object typesafe is not a member of package com
[error] import com.typesafe.scalalogging.slf4j.LazyLogging
[error]            ^
[error] C:\PROJECTS\...\src\main\scala\...\main.scala:6: not found: type LazyLogging
[error] object Main extends LazyLogging {
[error]                     ^
[error] C:\PROJECTS\...\src\main\scala\...\voldemort\main.scala:9: not found: value logger
[error]     logger.info("Hello From My app!")
[error]     ^
[error] three errors found
[error] (compile:compileIncremental) Compilation failed
[error] Total time: 3 s, completed Sep 23, 2016 10:50:21 PM

现在我发现它没有找到LazyLogging,但我读到的代码似乎完全相同(build.sbt依赖,import行和用法)。我尝试了很多不同的场景和不同的日志记录方法,这只是我目前的设置。

有人可以指点我快速直接的教程或书来设置它。为了爱scala我只需要这个基本的应用程序,当我点击sbt运行时登录到文件和控制台。

2 个答案:

答案 0 :(得分:0)

我不知道这是不是原因,但你至少有两个明显的问题:

  1. 您的build.sbt在依赖项列表中缺少逗号
  2. 您的主要课程名为Main,但位于名为main
  3. 的文件中

    修改

    您还使用旧版scala-logging。我建议运行sbt compile并跟踪其错误。从那个阶段开始,修正将非常明显。

答案 1 :(得分:0)

你的libraryDependencies有拼写错误(缺少逗号)。此外,您使用的是不正确的(较旧)版本的依赖项。用这个替换你的build.sbt:

name := "BOGen"

   version := "1.0"

   scalaVersion := "2.11.8"

   resolvers += "Typesafe Repository" at "http://repo.typesafe.com/typesafe/releases/"

   libraryDependencies ++= Seq(
    "com.typesafe.akka" %% "akka-actor" % "2.4.10",
    "com.typesafe.akka" %% "akka-slf4j" % "2.4.10",
    "com.typesafe.scala-logging" %% "scala-logging-slf4j" % "2.1.2",
    "org.slf4j" % "slf4j-api" % "1.7.1",
    "org.slf4j" % "log4j-over-slf4j" % "1.7.1",
    "ch.qos.logback" % "logback-classic" % "1.0.3"
   )