log4j in a multi-module Maven project

2019-06-28 05:58发布

问题:

How do I implement log4j in a multi-module Maven project? Do I add a "log4j.properties" file to the src/main/resources directory of every submodule? If so, do I have to specify a different output file for each file appender? Or is there a way to have one overarching log4j.properties file for the whole project, without having a configuration nightmare. Which approach (many log4j.properties files versus one) is more common and better practice? Thanks for all feedback in advance.

回答1:

It depends on how you intend to deploy and run this project. For hierarchical multi-module projects, we generally included a log4j config file in the module with the main method that would launch the program.

For example, consider a simplified setup with projects client and server which both rely on framework for common classes (RMI interfaces, domain objects, etc). Client is run as a desktop application, has its own main entry point, and thus gets its own log4j.properties file under src/main/resources. So does the server module. Framework, however, does not get a properties file -- its logger calls will be handled by whichever loggers have been configured by its calling module.