separate log file for different classes in same package
I am using the log4j and wanted to create different log files for different classes in the same packa开发者_Python百科ge,I can perform this by using two xml files but I want to perform it by using a single xml file.
Please tell me the way to do this.
Make all classes which should log to the same file use the same logger, e.g. place the following Logger instantiation in each class:
Logger firstFileLogger= Logger.getLogger("firstFile");
In your log4j.xml
declare a corresponding logger
<logger name="firstFile>
<level value="debug"/>
<appender-ref ref="firstFileAppender" />
</logger>
and let it log to a corresponding file appender:
<appender name="firstFileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="file" value="firstFile.log"/>
<param name="MaxFileSize" value="100KB"/>
<param name="MaxBackupIndex" value="1"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%p %t %c - %m%n"/>
</layout>
</appender>
Repeat this for each log file you want to write.
精彩评论