简体   繁体   中英

log4j2 logging happening in the wrong path

I have an ear deployed in weblogic with the log4j2.xml logPath set as

<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

<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. But the logs for both are coming in the path some_path_2 and with the file name defined in that 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

    <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

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. 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.

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

  1. Remove log4j2.xml from abc.jar Do it with your build-script in ANT/MAVEN/GRADLE or whatever. 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. Now you could include the whole config-file in your log4j2.xml. This is called 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. You can try to reconfigure log4j2 in code with a specific configuration file .

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 . I don't think, log4j2 automatic configuration mechanism try to find out configuration file with name "logj.xml".

From 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.

If no system property is set the properties ConfigurationFactory will look for log4j2.properties or log4j2.json or log4j2.xml files in classpath.

Possibly you should try to rename the file "xyz.war//WEB-INF/classes/log4j.xml" to "xyz.war//WEB-INF/classes/log4j2.xml". Obviously content of this file must confirm to log4j configuration xsd.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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