问题
I have to implement multiple log location using log4j in out project which includes multiple modules, for each module i have to log the in separate location
回答1:
Perhaps the log4j.properties on this articles would help you seek what you are looking for. http://ta.cnci.org/more-about-java/35-java-blogs/238-dailyfileappender and may be you could use the DailyFileAppender.java that I wrote a while back. The idea is simple and it is in 4 parts:
- Set the log4j.rootCategory to include your "FILE DEF"
- Define your "FILE DEF" such as log4j.appender.SOADAILY
- Tell the Logger to filter it to your "FILE DEF" as: log4j.logger.com.incresearch.soa.db=DEBUG, TDLDAILY
- Tell the Logger not delete from everywhere else as: log4j.additivity.org.cnci.tdl=false
But here is an example:
#------------------------------------------------------------------------------ # Default log4j.properties file. This should be in the LocalFiles folder # # Possible Log Levels: # FATAL, ERROR, WARN, INFO, DEBUG # log4j.rootCategory=DEBUG, SOADAILY, TDLDAILY log4j.logger.org.apache=ERROR #------------------------------------------------------------------------------ # The following properties configure the Daily Rolling File appender. # For SOA components, mostly from org.cnci.soa.* packages #------------------------------------------------------------------------------ log4j.appender.SOADAILY = org.cnci.util.DailyFileAppender log4j.appender.SOADAILY.File = logs/SOAServices.log log4j.appender.SOADAILY.MaxLogs = 30 log4j.appender.SOADAILY.Append = true log4j.appender.SOADAILY.DatePattern = '.'yyy-MM-dd log4j.appender.SOADAILY.layout = org.apache.log4j.PatternLayout log4j.appender.SOADAILY.layout.ConversionPattern = %d %5p %c{1}:%L - %m%n #------------------------------------------------------------------------------ # The following properties configure the Daily Rolling File appender for TDL. # For TDL applications, mostly from org.cnci.tdl.* packages and jboss seams, richfaces... #------------------------------------------------------------------------------ log4j.appender.TDLDAILY = org.apache.log4j.FileAppender log4j.appender.TDLDAILY.File = logs/TDLServices.log log4j.appender.TDLDAILY.MaxLogs = 30 log4j.appender.TDLDAILY.Append = true log4j.appender.TDLDAILY.DatePattern = '.'yyy-MM-dd log4j.appender.TDLDAILY.layout = org.apache.log4j.PatternLayout log4j.appender.TDLDAILY.layout.ConversionPattern = %d %5p %c{1}:%L - %m%n # Extracting all Adapter logging into it own file log4j.logger.org.cnci.soa=DEBUG, SOADAILY log4j.additivity.org.cnci.soa=false log4j.logger.com.hazelcast=ERROR, SOADAILY log4j.additivity.com.hazelcast=false log4j.logger.org.opensaml=ERROR, SOADAILY log4j.additivity.org.opensaml=false # Extracting all DB (TDL) logging into it own file log4j.logger.com.incresearch.soa.db=DEBUG, TDLDAILY log4j.additivity.org.cnci.tdl=false log4j.logger.org.jboss.seam=ERROR, DAILY log4j.additivity.org.jboss.seam=false log4j.logger.org.richfaces=ERROR, DAILY log4j.additivity.org.richfaces=false log4j.logger.org.ajax4jsf=ERROR, DAILY log4j.additivity.org.ajax4jsf=false
回答2:
Check out my answer here: Log4j to write json array to disk
It shows how to send com.foo.bar logs to console, com.example to a file appender; you can mix and match - essentially the package name after the log4j.com. part will create a new logger for the sub tree of classes rooted there; if in a descendant package you need the change the location (remember, appenders are additive by default) you'll have to set 'inherit' to 'false'.
来源:https://stackoverflow.com/questions/22032446/what-is-the-best-way-to-implement-multiple-logs-location-using-log4j