【问题标题】:Scala logging, direct console output to log-fileScala 日志记录,直接控制台输出到日志文件
【发布时间】:2017-07-31 16:20:57
【问题描述】:

有没有办法使用 scala 日志将控制台输出定向到日志文件。在下面的代码中 println 写入控制台是否可以将其定向到定义的 logback.xml 的日志文件。

import com.typesafe.scalalogging.LazyLogging

object FileProcessing extends LazyLogging  {

  def  main(args: Array[String]): Unit = {
    val fp = new FileProcessing
    logger.info(fp.fileMoves.toString())
    println("My desired directory is - "+fp.dir)   
  }
}

使用下面的 scala 记录我的 build.gradle

compile "ch.qos.logback:logback-classic:1.1.7"
compile "com.typesafe.scala-logging:scala-logging_2.12:3.5.0"

【问题讨论】:

  • 您需要修改记录器配置文件以包含文件输出

标签: scala scala-logging


【解决方案1】:

println 写入Console.out。你可以给它任何你想要的流。例如:

Console.withOut(new PrintStream(new FileOutputStream("/dev/null"))) {
  println("this will not be printed anywhere")
}
Console.withOut(new PrintStream(new FileOutputStream("console.txt"))) {
  println("this is printed to console.txt")
}
println("This still goes to stdout")

您还可以使用(已弃用)Console.setOut 全局更改输出流,但这并不是一个好主意(与其他任何想法一样,涉及在运行时更改全局 JVM 状态)。在这种情况下,您最好在命令行上使用> console.txt 运行您的程序。

请注意,如果其他人(除了println/Console 也在写入同一个文件),您得到的结果可能不会是您喜欢的,因为缓冲和赛车)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-12
    相关资源
    最近更新 更多