Spark 2.2.1从fat-jar中的资源读取log4.xml

时间:2018-01-18 19:37:18

标签: xml scala apache-spark log4j

我在我的机器上本地运行spark,我想从资源加载log4.xml文件。

我运行的命令是

spark-submit --class spark.MyClass --conf spark.driver.maxResultSize=1g \
--conf spark.driver.memory=26g --conf spark.executor.memory=26g \
--conf "spark.driver.extraJavaOptions=-XX:+UseG1GC -XX:+UseStringDeduplication -Dlog4j.configuration=log/log4j.xml -Dlog4j.debug" \
--conf "spark.executor.extraJavaOptions=-XX:+UseG1GC -XX:+UseStringDeduplication -Dlog4j.configuration=log/log4j.xml -Dlog4j.debug" \
--conf spark.default.parallelism=100 \
./build/libs/my-fat.jar


log4j: Trying to find [log/log4j.xml] using context classloader sun.misc.Launcher$AppClassLoader@1cf4f579.
log4j: Trying to find [log/log4j.xml] using sun.misc.Launcher$AppClassLoader@1cf4f579 class loader.
log4j: Trying to find [log/log4j.xml] using ClassLoader.getSystemResource().
log4j: Could not find resource: [log/log4j.xml].

所有类都是从资源中读取log4j.xml文件并打印出来。

 def main(args: Array[String]): Unit = {
    val resource: InputStream =this.getClass.getClassLoader.getResourceAsStream("log/log4j.xml")
    val lines=Source.fromInputStream(new BufferedInputStream(resource)).getLines().toList
     lines.foreach(l=>println(l))

}

打印文件,但log4j找不到log4j.xml文件。

有没有办法从资源加载log4j.xml文件?

1 个答案:

答案 0 :(得分:0)

当我遇到同样的问题时,我通过从应用程序开头的资源中读取log4j配置来解决它:

import org.apache.log4j.PropertyConfigurator
PropertyConfigurator.configure(getClass.getClassLoader.getResourceAsStream("log/log4j.xml"))