如何发现已加载的log4j2配置的完整路径?

时间:2019-04-12 21:10:27

标签: java log4j2

我的应用服务器正在加载log4j2.properties文件,但找不到它的位置。我想知道此conf文件的完整路径

如果我使用选项-Dlog4j.debug来启动jvm,就像在this question中回答的那样,我可以很容易地使log4j2显示搜索到的配置文件,但是它不会显示完整的路径。

使用debug参数,我得到以下输出:

[err] DEBUG StatusLogger Using configurationFactory org.apache.logging.log4j.core.config.ConfigurationFactory$Factory@239c2809
[err] TRACE StatusLogger Trying to find [log4j2-testago service.properties] using context class loader com.ibm.ws.classloading.internal.ThreadContextClassLoader@834b414.
[err] TRACE StatusLogger Trying to find [log4j2-testago service.properties] using com.ibm.ws.classloading.internal.AppClassLoader@8bc0851 class loader.
[err] TRACE StatusLogger Trying to find [log4j2-testago service.properties] using com.ibm.ws.classloading.internal.ThreadContextClassLoader@834b414 class loader.
[err] TRACE StatusLogger Trying to find [log4j2-testago service.properties] using ClassLoader.getSystemResource().
[err] TRACE StatusLogger Trying to find [log4j2-testago service.yml] using context class loader com.ibm.ws.classloading.internal.ThreadContextClassLoader@834b414.
[err] TRACE StatusLogger Trying to find [log4j2-testago service.yml] using com.ibm.ws.classloading.internal.AppClassLoader@8bc0851 class loader.
[err] TRACE StatusLogger Trying to find [log4j2-testago service.yml] using com.ibm.ws.classloading.internal.ThreadContextClassLoader@834b414 class loader.
[err] TRACE StatusLogger Trying to find [log4j2-testago service.yml] using ClassLoader.getSystemResource().
[err] TRACE StatusLogger Trying to find [log4j2-testago service.yaml] using context class loader com.ibm.ws.classloading.internal.ThreadContextClassLoader@834b414.
[err] TRACE StatusLogger Trying to find [log4j2-testago service.yaml] using com.ibm.ws.classloading.internal.AppClassLoader@8bc0851 class loader.
[err] TRACE StatusLogger Trying to find [log4j2-testago service.yaml] using com.ibm.ws.classloading.internal.ThreadContextClassLoader@834b414 class loader.
[err] TRACE StatusLogger Trying to find [log4j2-testago service.yaml] using ClassLoader.getSystemResource().
[err] TRACE StatusLogger Trying to find [log4j2-testago service.json] using context class loader com.ibm.ws.classloading.internal.ThreadContextClassLoader@834b414.
[err] TRACE StatusLogger Trying to find [log4j2-testago service.json] using com.ibm.ws.classloading.internal.AppClassLoader@8bc0851 class loader.
[err] TRACE StatusLogger Trying to find [log4j2-testago service.json] using com.ibm.ws.classloading.internal.ThreadContextClassLoader@834b414 class loader.
[err] TRACE StatusLogger Trying to find [log4j2-testago service.json] using ClassLoader.getSystemResource().
[err] TRACE StatusLogger Trying to find [log4j2-testago service.jsn] using context class loader com.ibm.ws.classloading.internal.ThreadContextClassLoader@834b414.
[err] TRACE StatusLogger Trying to find [log4j2-testago service.jsn] using com.ibm.ws.classloading.internal.AppClassLoader@8bc0851 class loader.
[err] TRACE StatusLogger Trying to find [log4j2-testago service.jsn] using com.ibm.ws.classloading.internal.ThreadContextClassLoader@834b414 class loader.
[err] TRACE StatusLogger Trying to find [log4j2-testago service.jsn] using ClassLoader.getSystemResource().
[err] TRACE StatusLogger Trying to find [log4j2-testago service.xml] using context class loader com.ibm.ws.classloading.internal.ThreadContextClassLoader@834b414.
[err] TRACE StatusLogger Trying to find [log4j2-testago service.xml] using com.ibm.ws.classloading.internal.AppClassLoader@8bc0851 class loader.
[err] TRACE StatusLogger Trying to find [log4j2-testago service.xml] using com.ibm.ws.classloading.internal.ThreadContextClassLoader@834b414 class loader.
[err] TRACE StatusLogger Trying to find [log4j2-testago service.xml] using ClassLoader.getSystemResource().
[err] TRACE StatusLogger Trying to find [log4j2-test.properties] using context class loader com.ibm.ws.classloading.internal.ThreadContextClassLoader@834b414.
[err] TRACE StatusLogger Trying to find [log4j2-test.properties] using com.ibm.ws.classloading.internal.AppClassLoader@8bc0851 class loader.
[err] TRACE StatusLogger Trying to find [log4j2-test.properties] using com.ibm.ws.classloading.internal.ThreadContextClassLoader@834b414 class loader.
[err] TRACE StatusLogger Trying to find [log4j2-test.properties] using ClassLoader.getSystemResource().
[err] TRACE StatusLogger Trying to find [log4j2-test.yml] using context class loader com.ibm.ws.classloading.internal.ThreadContextClassLoader@834b414.
[err] TRACE StatusLogger Trying to find [log4j2-test.yml] using com.ibm.ws.classloading.internal.AppClassLoader@8bc0851 class loader.
[err] TRACE StatusLogger Trying to find [log4j2-test.yml] using com.ibm.ws.classloading.internal.ThreadContextClassLoader@834b414 class loader.
[err] TRACE StatusLogger Trying to find [log4j2-test.yml] using ClassLoader.getSystemResource().
[err] TRACE StatusLogger Trying to find [log4j2-test.yaml] using context class loader com.ibm.ws.classloading.internal.ThreadContextClassLoader@834b414.
[err] TRACE StatusLogger Trying to find [log4j2-test.yaml] using com.ibm.ws.classloading.internal.AppClassLoader@8bc0851 class loader.
[err] TRACE StatusLogger Trying to find [log4j2-test.yaml] using com.ibm.ws.classloading.internal.ThreadContextClassLoader@834b414 class loader.
[err] TRACE StatusLogger Trying to find [log4j2-test.yaml] using ClassLoader.getSystemResource().
[err] TRACE StatusLogger Trying to find [log4j2-test.json] using context class loader com.ibm.ws.classloading.internal.ThreadContextClassLoader@834b414.
[err] TRACE StatusLogger Trying to find [log4j2-test.json] using com.ibm.ws.classloading.internal.AppClassLoader@8bc0851 class loader.
[err] TRACE StatusLogger Trying to find [log4j2-test.json] using com.ibm.ws.classloading.internal.ThreadContextClassLoader@834b414 class loader.
[err] TRACE StatusLogger Trying to find [log4j2-test.json] using ClassLoader.getSystemResource().
[err] TRACE StatusLogger Trying to find [log4j2-test.jsn] using context class loader com.ibm.ws.classloading.internal.ThreadContextClassLoader@834b414.
[err] TRACE StatusLogger Trying to find [log4j2-test.jsn] using com.ibm.ws.classloading.internal.AppClassLoader@8bc0851 class loader.
[err] TRACE StatusLogger Trying to find [log4j2-test.jsn] using com.ibm.ws.classloading.internal.ThreadContextClassLoader@834b414 class loader.
[err] TRACE StatusLogger Trying to find [log4j2-test.jsn] using ClassLoader.getSystemResource().
[err] TRACE StatusLogger Trying to find [log4j2-test.xml] using context class loader com.ibm.ws.classloading.internal.ThreadContextClassLoader@834b414.

由于最后显示的文件名是log4j2-test.xml,因此已加载文件。问题是,如果我将文件命名为log4j2.xml,则会在其之前加载名为log4j2.properties的另一个配置。而且我找不到该文件在哪里!必须有一种方法来发现它的加载位置。

1 个答案:

答案 0 :(得分:1)

在您的程序中,您可以执行以下操作:

URL url = org.apache.logging.log4j.LogManager.class.getResource("/log4j2.properties");
System.out.println(url);

这应该使用LogManager类的类加载器在类路径的根目录中显示“ log4j2.properties”文件的位置。