在 spark 应用程序中捆绑到 jar 中的 log4j 属性文件被忽略

log4j properties file bundled into jar in spark app is ignored

我需要从 src/resources 中读取自定义 log4j.properties,但它不起作用

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)

意味着jar中捆绑的log4j被忽略了。

我无法触及 spark 主目录中 conf 目录中的 log4j 属性。

还有哪些选择?

编辑 肯定有问题(在类路径中?) 自从这样做以来

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

指向conf目录下的log4j,我不能修改,需要忽略。

怎么做?

  1. 您可以为您的文件指定另一个名称(并在代码中使用它),因此与 conf/log4j.properties.

    [=20 没有冲突=]
  2. 您可以使用 ClassLoader.getResources("log4j.properties") 获取类路径中给定名称的所有资源,然后选择您想要的资源。