log4j properties file bundled into jar in spark app is ignored

丶灬走出姿态 提交于 2019-12-01 11:19:53

问题


I need to read a custom log4j.properties from src/resources and this is not working

try{
  val  inStream :InputStream= className.this.getClass.getClassLoader.getResourceAsStream("log4j.properties");

  logCfgProps.load(inStream)

} catch {
  case e: Throwable=>
    e.printStackTrace()
    log.error("log.properties file not present")
}

PropertyConfigurator.configure(logCfgProps)

meaning that the log4j bundled in the jar is ignored.

I cannot touch the log4j properties in the conf directory in the spark home.

What are the other options?

EDIT There must be something wrong (in the classpath ?) since doing this

val resource:URL = Thread.currentThread().getContextClassLoader()
  .getResource("log4j.properties");
System.out.println("resource = " + resource); 

It points to the log4j in the conf directory that I cannot modify and that I need to ignore.

How to do that?


回答1:


  1. You can just give another name to your file (and using it in the code as well), so there is no conflict with conf/log4j.properties.

  2. You can get all resources with the given name on the classpath using ClassLoader.getResources("log4j.properties") and choose which one you want.



来源:https://stackoverflow.com/questions/33237662/log4j-properties-file-bundled-into-jar-in-spark-app-is-ignored

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!