简体   繁体   中英

Configuring log4j2 in a multi-module J2EE web project without maven

I've been searching the web and SO for a while now to find the answer for my question, but everything I could find addressed multi-module projects that were implemented with Maven. My project does not use maven and so it doesn't help my scenario.

I'm building a Java 8 J2EE web services project that runs on Tomcat 8 and is broken into a few separate projects (see project tree below). I am not using maven, I'm defining all of the dependencies through eclipse (I am hoping to learn about maven at some point and "mavenize" my projects, but I'm not there yet).

How can I achieve the following: Have a single log4j2.xml file that would be used by the dynamic web project and all the Java sub-projects. The only thing that has worked for me so far is to have a copy of the log4j2.xml file present in the src folder of each of the projects. I've attempted various things, such as placing the configuration file in a shared folder and adding the folder to each of the projects' class path, or placing the file in the CommonLib project, which is used by all, but each time I keep getting a log4j warning that no configuration file was found.

Here's my project tree:

Services (Dynamic web project)
   ^
   |__ BusinessCore (Java project)
   |         ^
   |         |___ DAO (Java project)
   |         |     ^
   |_________|_____|__CommonLib (Java project)

It sounds like you're trying to use log4j2 automatic configuration by including the log4j2 config file on the classpath.

I further assume that you're trying to run your code from within Eclipse. The only way it will work correctly with automatic configuration (without specifying the path to the config file via system property) is if the log4j2 config file is inside of a "source folder" that is included on the build path or if you put your config file inside of a jar and add that jar to your build path.

So, assuming you're using a log4j2.xml file, if you place log4j2.xml inside of a source folder of your CommonLib project and include this project on the build path of your other projects (along with the necessary log4j2 jars) it should work fine.

Here is how I have set up an example project:

Content of root level project folder non-maven-web-project-log4j2 :

在此处输入图片说明

Content of BusinessCore :

在此处输入图片说明

Content of CommonLib :

在此处输入图片说明

Note that the "config" folder is a source folder (you will see this again later) in Eclipse and that the log4j2.xml is contained in the config folder.

Build Path configuration:

non-maven-web-project-log4j2 has a dependency on the BusinessCore project

在此处输入图片说明

BusinessCore has a dependency on CommonLib (no screenshot, it's almost identical to the previous)

CommonLib has an extra source folder called config:

在此处输入图片说明

and this config folder contains the log4j2.xml file:

在此处输入图片说明

Each project has one simple class in its source folder that references a class from its child project. In the non-maven-web-project-log4j2 project there is a class that refers to the class in BusinessCore, and in BusinessCore there is a class referring to the class in CommonLib. Every class generates a log4j2 message. When I run the class from the root project (non-maven-web-project-log4j2) I am able to see all of the logs and the logs have the pattern I specified in my log4j2 config file which confirms that log4j2 is able to find its configuration.

As a final note - I added the extra "config" source folder just to emphasize the point that you can place your log4j2 config file in any source folder in the build path, it is not necessary to create a separate folder for it if you don't want to - you could instead just include it in the src directory as you already noted in your question.

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