2017-07-31 15 views
0

スカラーログを使用してコンソール出力をログファイルに送る方法はありますか?以下のコードでは、printlnをコンソールに書き込むことで、定義されたlogback.xmlとしてログファイルに書き込むことができます。Scalaのログ出力、ログファイルへのダイレクトコンソール出力

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) 
    } 
} 

私のbuild.gradleログインScalaの下に使用

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

ファイル出力を含むようにロガー設定ファイルを変更する必要があります – puhlen

答えて

1

printlnConsole.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も同じファイルに書き込んでいる場合)は、バッファリングとレースのためにあなたが好きなものになるとは限りません。

関連する問題