有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java log4j2日志记录发生在错误的路径中

我在weblogic中部署了一个带有log4j2的ear。xml日志路径设置为

<Property name="logPath">some_path_1<Property>

以及定义为

<Logger name="a.b.c.d" level="INFO" />

在这个ear/lib中有一个包含log4j2的jar。xml和logPath属性定义为

<Property name="logPath">some_path_2<Property>

和一个包装结构非常相似的记录器

<Logger name="a.b.c" level="INFO" />

两个都是log4j2。xml也被配置为具有不同的日志文件名。 但是这两个文件的日志都是在路径some_path_2中,并且文件名是在jar中定义的

如何确保两个日志输出按照定义分别进行

我的耳朵结构像

ear
|--lib
|--|--abc.jar
|--|--|--log4j2.xml //the one thats getting loaded
|--xyz.war
|--|--WEB-INF
|--|--|--classes
|--|--|--|--log4j2.xml // the one I want

Edit1:我在遏制战争的网站上添加了以下内容。但它没有帮助

    <context-param>
        <param-name>log4jConfigLocation</param-name>
        <param-value>/WEB-INF/classes/log4j2.xml</param-value>
    </context-param>

Edit2:我也试过这个,发现它间歇性工作

ear
|--lib
|--|--abc.jar
|--|--|--log4j2.xml //the one thats getting loaded
|--|--xyz.jar
|--|--|--log4j2.xml //the one I want.. works but not always.Does classloader loads the jars alphabetically?
|--xyz.war
|--|--WEB-INF
|--|--|--classes
|--|--|--|--log4j2.xml // the one I want

共 (4) 个答案

  1. # 2 楼答案

    使用Log4j引导程序首先找到的配置文件,Log4j只初始化一次。不考虑所有其他(可能存在的)配置文件。在相应的教程中了解更多关于log4j自动配置的先例

  2. # 3 楼答案

    您确定要使用log4j2使用的“log4.xml”文件吗。我不认为log4j2自动配置机制试图找到名为“logj.xml”的配置文件

    official documentation

    Log4j has the ability to automatically configure itself during initialization. When Log4j starts it will locate all the ConfigurationFactory plugins and arrange them in weighted order from highest to lowest. As delivered, Log4j contains four ConfigurationFactory implementations: one for JSON, one for YAML, one for properties, and one for XML.

    Log4j will inspect the "log4j.configurationFile" system property and, if set, will attempt to load the configuration using the ConfigurationFactory that matches the file extension.

    如果未设置任何系统属性,则properties ConfigurationFactory将查找log4j2。属性或log4j2。json或log4j2。类路径中的xml文件

    您可能应该尝试将文件“xyz.war//WEB-INF/classes/log4j.xml”重命名为“xyz.war//WEB-INF/classes/log4j2.xml”。显然,该文件的内容必须符合log4j配置xsd

  3. # 4 楼答案

    正如您可能已经知道的,您的问题是,只加载了一个配置文件,而哪个配置文件取决于类加载器。所以你不应该依赖它

    你的问题只有两种可能的解决办法,但今天我没有时间为你解决——对不起。您必须删除或重命名abc中的配置文件。罐子

    1. 拆下log4j2。来自abc的xml。罐子 用ANT/MAVEN/GRADLE或其他任何语言编写构建脚本。复制并粘贴配置文件中感兴趣的部分

    2. 重命名log4j2。abc内部的xml。罐子 用ANT/MAVEN/GRADLE或其他任何语言编写构建脚本。现在,您可以在log4j2中包含整个配置文件。xml。这被称为CompositeConfiguration

    我希望这个小小的建议能对你有所帮助。 祝你好运