I'm trying to set up separate log files for different packages. I'm using a Wrapper class for a log4j
logger. Every class in my application calls same wrapper class. My wrapper class:
public class MyLogger
{
private static Logger logger = Logger.getLogger(MyLogger.class.getName());
....
....
}
It is called like this:
MyLogger.write(, , );
Is there a way to configure log4j so that it outputs logging of different packages to different files?
Thanks!
Edit:
Here is my log4j.properties
file:
log4j.rootLogger=DEBUG, infoout, aar
log4j.logger.com.businessservice.datapopulation=DEBUG, aar
log4j.additivity.com.businessservice.datapopulation=false
log4j.appender.infoout = org.apache.log4j.RollingFileAppender
log4j.appender.infoout.file=/app/aar_frontend.log
log4j.appender.infoout.append=true
log4j.appender.infoout.Threshold=DEBUG
log4j.appender.infoout.MaxFileSize=2MB
log4j.appender.infoout.MaxBackupIndex=10
log4j.appender.infoout.layout = org.apache.log4j.PatternLayout
log4j.appender.infoout.layout.ConversionPattern = %m%n
log4j.appender.aar = org.apache.log4j.RollingFileAppender
log4j.appender.aar.file=/app/aar/aar_backend.log
log4j.appender.aar.append=true
log4j.appender.aar.Threshold=DEBUG
log4j.appender.aar.MaxFileSize=2MB
log4j.appender.aar.MaxBackupIndex=10
log4j.appender.aar.layout = org.apache.log4j.PatternLayout
log4j.appender.aar.layout.ConversionPattern = %m%n
Read the required custom file location from a property file or so for each packages. Then you can use the below given method to update the log4j file location set in the log4j prop file:
You can do this like that(com.myco.a and com.myco.b being your 2 different packages):
Cheers.
Creating 2 appenders and 2 loggers would do what you want.
If you create a static Logger within MyLogger class, then you have one Logger instance, with the name set to MyLogger. When you call that logger from other packages, Log4j is not able to determine the origin of those calls, as they all use the same Logger.
The best way to handle it, is to define a separate Logger within each class, but if you want to use one class as a point of contact with Log4j, then you can do this:
Then, one of the class using it could look like:
And the
log4j.properties
file would look like:If you don't want to pass the Class from ClassA, you could use a nasty trick with reflection, that gets the calling class' name, but I wouldn't recommend that due to a performance hit: