简体   繁体   English

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

[英]log4j2 logging happening in the wrong path

I have an ear deployed in weblogic with the log4j2.xml logPath set as 我在weblogic中部署了一个ear,并将log4j2.xml logPath设置为

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

and a logger defined as 和记录器定义为

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

Inside this ear/lib there is a jar containing a log4j2.xml and the logPath property is defined as 在这个ear / lib中,有一个包含log4j2.xml的jar,logPath属性定义为

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

and a logger with a very similar packaging structure 和一个包装结构非常相似的记录器

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

Both log4j2.xml's are configured to have a different log file name as well. log4j2.xml都配置为具有不同的日志文件名。 But the logs for both are coming in the path some_path_2 and with the file name defined in that jar 但两者的日志都在路径some_path_2中,并且在该jar中定义了文件名

How to ensure both logging outputs happens separately as defined? 如何确保两个日志记录输出按照定义单独进行?

My ear structure is like 我的耳朵结构就像

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

Edit1: I added the following in the containing war's web.xml but it did not help Edit1:我在包含war的web.xml中添加了以下内容,但它没有帮助

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

Edit2: I also tried this and found that this is working intermittently 编辑2:我也试过这个,发现这是间歇性的

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

Log4j is initialized only once by using the configuration file which is first found by the log4j bootstrapper. Log4j仅使用log4j引导程序首次找到的配置文件初始化一次。 All other (possible present) configuration files will not be taken into account. 将不考虑所有其他(可能存在的)配置文件。 Learn more on the precedences of log4j auto configuration in the appropriate tutorials. 在相应的教程中了解有关log4j自动配置优先级的更多信息。

As you probally already know your problem is that only one configuration-file gets loaded and which one depends on the classloader. 正如您可能已经知道您的问题是只有一个配置文件被加载,哪个依赖于类加载器。 So you should not rely on that. 所以你不应该依赖它。

There are only two possible soulutions for your problem, but today I do not have the time to work it out for you - sorry. 你的问题只有两个可能的问题,但今天我没有时间为你解决问题 - 抱歉。 You have to remove or rename the config-file in abc.jar 您必须删除或重命名abc.jar中的配置文件

  1. Remove log4j2.xml from abc.jar Do it with your build-script in ANT/MAVEN/GRADLE or whatever. 从abc.jar中删除log4j2.xml使用ANT / MAVEN / GRADLE中的构建脚本或其他方法。 Copy-paste the interessting parts of the config-file in your config-file. 将配置文件的有趣部分复制粘贴到配置文件中。

  2. Rename log4j2.xml inside abc.jar Do it with your build-script in ANT/MAVEN/GRADLE or whatever. 重命名abc.jar中的log4j2.xml使用ANT / MAVEN / GRADLE中的构建脚本或其他任何内容。 Now you could include the whole config-file in your log4j2.xml. 现在您可以在log4j2.xml中包含整个配置文件。 This is called CompositeConfiguration . 这称为CompositeConfiguration

I hope this little advice will help you. 我希望这个小建议对你有帮助。 Good luck! 祝好运!

Can you remove the XMLs from the jar/war and use only one xml file at the ear level which has details of both the logging paths/properties. 你可以从jar / war中删除XML,并且在ear级别只使用一个xml文件,该文件包含两个日志记录路径/属性的详细信息。 You can try to reconfigure log4j2 in code with a specific configuration file . 您可以尝试使用特定配置文件在代码中重新配置log4j2

You can also dynamically write to separate log files , but I personally haven't tried this so I wouldn't be able to guide more on this. 您也可以动态写入单独的日志文件 ,但我个人没有尝试过,所以我无法指导更多。

Are you sure you want to use "log4.xml" file to be use by log4j 2 . 您确定要使用log4j 2使用的“log4.xml”文件吗? I don't think, log4j2 automatic configuration mechanism try to find out configuration file with name "logj.xml". 我不认为,log4j2自动配置机制试图找出名为“logj.xml”的配置文件。

From official documentation , 官方文档

Log4j has the ability to automatically configure itself during initialization. Log4j能够在初始化期间自动配置自身。 When Log4j starts it will locate all the ConfigurationFactory plugins and arrange them in weighted order from highest to lowest. 当Log4j启动时,它将找到所有ConfigurationFactory插件并按加权顺序从最高到最低排列。 As delivered, Log4j contains four ConfigurationFactory implementations: one for JSON, one for YAML, one for properties, and one for XML. 交付时,Log4j包含四个ConfigurationFactory实现:一个用于JSON,一个用于YAML,一个用于属性,一个用于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. Log4j将检查“log4j.configurationFile”系统属性,如果设置,将尝试使用与文件扩展名匹配的ConfigurationFactory加载配置。

If no system property is set the properties ConfigurationFactory will look for log4j2.properties or log4j2.json or log4j2.xml files in classpath. 如果未设置系统属性,则ConfigurationFactory属性将在类路径中查找log4j2.properties或log4j2.json或log4j2.xml文件。

Possibly you should try to rename the file "xyz.war//WEB-INF/classes/log4j.xml" to "xyz.war//WEB-INF/classes/log4j2.xml". 可能您应该尝试将文件“xyz.war // WEB-INF / classes / log4j.xml”重命名为“xyz.war // WEB-INF / classes / log4j2.xml”。 Obviously content of this file must confirm to log4j configuration xsd. 显然这个文件的内容必须确认log4j配置xsd。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM