2017-02-10 19 views
2

私はこれを、単純なlog4j2test構成+用法以外の目的を持たない非常に簡単なテストプロジェクトに分けました。ファイルのディレクトリ構造:"log4j2構成ファイルが見つかりませんでした。

. 
├── build.sbt 
└── src 
└── main 
    ├── resources 
    │ └── log4j2.xml 
    └── scala 
    └── mycompany 
     └── myapp 
     └── SimpleMain.scala 

Build.sbt:

name := """log4j2test""" 

version := "1.0.0-SNAPSHOT" 

scalaVersion := "2.12.1" 

scalacOptions := Seq("-unchecked", "-deprecation", "-encoding", "utf8") 

libraryDependencies ++= { 
    Seq(
"org.apache.logging.log4j"   % "log4j-slf4j-impl" % "2.8", 
"org.apache.logging.log4j"   % "log4j-api" % "2.8", 
"org.apache.logging.log4j"   % "log4j-core" % "2.8", 
"com.fasterxml.jackson.dataformat" % "jackson-dataformat-yaml" % "2.8.6" 
) 
} 

log4j2.xml内容ですコピー/公式ドキュメントの例から貼り付け:https://logging.apache.org/log4j/2.x/manual/configuration.html

SimpleMain.scala:

package mycompany.myapp 

import org.slf4j.LoggerFactory 

object SimpleMain { 
    val logger = LoggerFactory.getLogger(getClass()) 

    def main(args: Array[String]): Unit = { 
println("simple println test") 
logger.info("this is a logger.info message") 
logger.debug("this is a logger.debug message") 
logger.error("this is a logger.error message") 

val stream = this.getClass.getClassLoader.getResourceAsStream("log4j2.xml") 
if (stream == null) { 
    println("failed to open log4j2.xml on classpath") 
} else { 
    println("successfully opened log4j2.xml on classpath") 
    stream.close() 
} 
    } 
} 

実行中

sbt "run-main mycompany.myapp.SimpleMain" 

出力:

[info] Running mycompany.myapp.SimpleMain 
ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. 
simple println test 
14:12:14.508 [run-main-0] ERROR mycompany.myapp.SimpleMain$ - this is a logger.error message 
successfully opened log4j2.xml on classpath 
[success] Total time: 4 s, completed Feb 10, 2017 2:12:14 PM 
+0

SBTを初期化するときにクラスパスに 'log4j.configurationFile = path/to/log4j2.xml'を追加してみてください。 –

+1

私はそれをする必要はありません。クラスパス上にファイルを置いておきます。 – clay

+1

[本当ですか?](http://stackoverflow.com/questions/28572783/no-log4j2-configuration-file-found-using-default-configuration-logging-only-er) –

答えて

0

はそれがSimpleMainアプリケーションのクラスローダは、SLF4Jのためのクラスローダとは異なることは可能ですか?システムプロパティorg.apache.logging.log4j.simplelog.StatusLogger.levelTRAC‌​Eに設定して、Log4j2の初期化をデバッグしてみてください。

0

log4j2.xmlはjarのルートに配置されていますか?私はgetResourceAsStreamが動作しているのに驚いています。相対パスで指定すると、/mycompany/myapp/log4j2.xmlが見つかるはずです。

関連する問題