views:

76

answers:

2

Hello,

I'm writing an application that consists of several maven modules. All of them have to log stuff to their own log files. I created a log4j.properties file in the main/resources folder of each module. Now when I start the application from one of the modules, it writes everything to the log file of that module. I wondered if this was just because it might have taken the root-dir from that start-module and used that to save the log files, but when I create an appender for just that start-module (using category in the log4j properties) and no rootLogger it gives me an error about classes in the other modules that can't find their appenders, which indicates the log4j.properties files in each of those modules isn't found or read. Anyone any ideas how to solve this and make those modules use those properties files, or do I have to put all the appenders in one big log4j.properties file, causing all the logs to appear in the root folder of that start-module instead of the root folders of those other modules?

I hope the question is clear enough to understand, otherwise feel free to ask for details.

A: 

As far as I know only the log4j.properties of the invoked module is read and initialized by default.

If you want to invoke the separate modules from a single start-module, you'll have to add all your appenders to a single log4j.properties file located in the start-module/src/main/resources directory. If and when you'd want this is probably a architecture question.

Just a suggestion: Switch to XML-based logging now if you want more advanced logging controls now or in the future.

Tim
A: 

If you include separate log4j.properties in the src/main/resources of each separate module and the load the generated jar files into a single classloader, only one resource will be loaded since log4j loads the properties files from the classloader (your resources overlap in filename).

I wouldn't include log4j.configuration files in any of my jar files. For two reasons:

  • Won't work in your case.

  • In order to configure log4j (editing verbosity of loggers), I would have to open the jar file and edit the properties file.

A different solution could be make another module producing a shaded ( http://maven.apache.org/plugins/maven-shade-plugin/) jar file from the remainder modules, including a single log4j.properties in src/main/resources. Then, if you have another module consuming your previous projects as dependencies, you could only include as a dependency the shaded one.

However, that's a solution I wouldn't use.

Hope that helps.

mrrtnn