Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j - Logging to multiple log files based on the project modules

Consider this scenario:

I have a project with two modules and one common module as below (the package structure):

com.mysite.moduleone
com.mysite.moduletwo
com.mysite.commonmodule

In the above, the commonmodule classes can be used by other two modules.

The question:

I need to configureLog4J such a way that the log messages from moduleone and moduletwo goes to different log file. I can always do this using using category.

But the real problem is when I want to log the messages from the commonmodule also. So, when the commonmodule classes are called from moduleone the commonmodule log messages should go to the moduleone log file. If the commonmodule is accesse from moduletwo the commonmodule log messages should go to moduletwo log file.

Is it possible to configure Log4J in this fashion? Any comments?

PS: I think I made my question clear. If any confusion, leave a comment, wil try to clear it. :)

like image 828
Veera Avatar asked Nov 14 '22 10:11

Veera


1 Answers

Is it possible to configure Log4J in this fashion?

In short, it is not with log4j unless you write a custom filter. And that filter would need to capture and analyse the call stack for each log event that makes it to each log appender ... which would be expensive.

like image 148
Stephen C Avatar answered May 19 '23 21:05

Stephen C