Reference articles
http://willow-na.iteye.com/blog/347340
(1) configuration file
Here is the code that came directly from that article, made some changes, and will also fill in some other content, I hope the original author does not mind
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE log4j:configuration SYSTEM "Log4j.dtd"> <log4j:configurationxmlns:log4j= ' http://jakarta.apache.org/log4j/' > <Appendername= "MyConsole"class= "Org.apache.log4j.ConsoleAppender"> <Layoutclass= "Org.apache.log4j.PatternLayout"> <paramname= "Conversionpattern"value= "[%d{dd hh:mm:ss,sss\}%-5p] [%t]%c{2\}-%m%n" /> </Layout> <!--The filter sets the output level (Info-warn) and sets the Levelmin and Levelmax to the same value, enabling only one level of logging to be output - <Filterclass= "Org.apache.log4j.varia.LevelRangeFilter"> <paramname= "Levelmin"value= "INFO" /> <paramname= "Levelmax"value= "WARN" /> <paramname= "Acceptonmatch"value= "true" /> </Filter> </Appender> <Appendername= "MyFile"class= "Org.apache.log4j.RollingFileAppender"> <paramname= "File"value= "D:/output.log" /><!--set Log output file path (including file name) - <!--set whether new logs are appended to the original log when the service is restarted - <paramname= "Append"value= "true" /> <paramname= "Maxbackupindex"value= "Ten" /> <Layoutclass= "Org.apache.log4j.PatternLayout"> <paramname= "Conversionpattern"value= "%p (%c:%l)-%m%n" /> </Layout> </Appender>
<!--configured for the log output form of a class-<Appendername= "Activexappender"class= "Org.apache.log4j.DailyRollingFileAppender"> <paramname= "File"value= "E:/activex.log" /> <paramname= "Datepattern"value= "'. ' Yyyy-mm-dd '. Log ' " /> <Layoutclass= "Org.apache.log4j.PatternLayout"> <paramname= "Conversionpattern"value= "[%d{mmdd HH:mm:ss sss\}%-5p] [%t]%c{3\}-%m%n" /> </Layout> </Appender> <!--a logger child element defines a log writer that specifies the settings of the logger, defines only one level of logging for a package or a class below it, additivity indicates whether to follow the default inheritance mechanism - <Loggername= "Com.runway.bssp.activeXdemo"additivity= "false"> < Priorityvalue= "Info"/> <Appender-refref= "Activexappender" /> </Logger> <!--settings for root logger - <Root> < Priorityvalue= "Debug"/> <Appender-refref= "MyConsole"/> <Appender-refref= "MyFile"/> </Root></log4j:configuration>
(2) Loading XML form configuration file in code
LOG4J (iv) XML form configuration file