multiple log4j instance configuration
I want to use multiple instances of the log4j Logger. I need to attach different Properties objects to each of these log4j Logger instances.
Here is the code to configure for one ins开发者_如何学编程tance:
LOG4J = org.apache.log4j.Logger.getLogger(Logger.class);
Properties log4jProps = new Properties();
...
PropertyConfigurator.configure(log4jProps);
What if I want to have two log4j instance and each of them has different properties?
Can you explain why you want multiple loggers in more detail? I don't think it's possible to have multiple log4j instances.
If you just want multiple appenders, look here:
- http://www.velocityreviews.com/forums/t721794-log4j-different-logger-instances.html
Here's the log4j.properties from the above link:
# logj4.properties
log4j.rootCategory = WARN, A
log4j.category.com.lewscanon = WARN, F
log4j.category.com.lewscanon.mouser = DEBUG, X
log4j.appender.A = org.apache.log4j.ConsoleAppender
log4j.appender.A.layout = org.apache.log4j.PatternLayout
log4j.appender.F = org.apache.log4j.RollingFileAppender
log4j.appender.F.layout = org.apache.log4j.PatternLayout
log4j.appender.F.File = /projects/mouser/logs/lewscanon.log
log4j.appender.F.MaxFileSize = 512KB
log4j.appender.F.MaxBackupIndex = 2
log4j.appender.X = org.apache.log4j.RollingFileAppender
log4j.appender.X.layout = org.apache.log4j.PatternLayout
log4j.appender.X.File = /projects/mouser/logs/mouser.log
log4j.appender.X.MaxFileSize = 512KB
log4j.appender.X.MaxBackupIndex = 2
log4j.appender.A.layout.ConversionPattern= %d %-16c{1}:%-39m %-t %x%n
log4j.appender.F.layout.ConversionPattern= %d %-16c{1}:%-39m %-t %x%n
log4j.appender.X.layout.ConversionPattern= %d %-16c{1}:%-39m %-t %x%n
These lines:
log4j.rootCategory = WARN, A
log4j.category.com.lewscanon = WARN, F
log4j.category.com.lewscanon.mouser = DEBUG, X
say the following:
- Log everything to appender A (which is console). Only log warnings and above
- Log everything from com.lewscanon package to appender F (which goes to file lewscanon.log). Only log warnings and above
- Log everything from com.lewscannon.mouser package to appender X (which goes to file mouser.log). Only log debug and above
It's also possible to supply full class names:
log4j.category.com.lewscanon.SomeClass = WARN, F
means log everything from com.lewscanon.SomeClass
class to appender F, level warn and above.
I suppose the above is granular enough, but if you absolutely need it more granular (I don't think that's practical enough, though), per:
- http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Logger.html
Logger.getLogger(String name)
accepts names. So you can do something like:
log4j.category.myFancyLogger = INFO, F
and use Logger.getLogger("myFancyLogger")
to get the logger that logs to appender F with level info.
精彩评论