程序加载制定日志文件
public static final String log4j = "log4j.xml";
1 /** 2 * @declare 加载log4j 3 * @throws IOException 4 * @author cphmvp 5 */ 6 private static void loadLog4j() throws IOException { 7 Properties p = new Properties(); 8 InputStream is = null; 9 is = ClassLoader.getSystemResourceAsStream(log4j); 10 p.load(is); 11 PropertyConfigurator.configure(p);// 路径 12 is.close(); 13 }
较为简单的 :log4j.properties 日志区分不细
#log4j.rootLogger =DEBUG,Console log4j.rootLogger =info,LogFile,Console # Output the log info to the Java Console log4j.appender.Console = org.apache.log4j.ConsoleAppender log4j.appender.Console.Target = System.out log4j.appender.Console.ImmediateFlush = true log4j.appender.Console.Threshold = INFO log4j.appender.Console.layout = org.apache.log4j.PatternLayout log4j.appender.Console.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} %-5p %x -%m%n # Save the log info to the log file one day. log4j.appender.LogFile = org.apache.log4j.DailyRollingFileAppender log4j.appender.LogFile.File = ./logs/error.log log4j.appender.LogFile.Append = true log4j.appender.LogFile.ImmediateFlush = true log4j.appender.LogFile.Threshold = INFO log4j.appender.LogFile.Encoding = UTF-8 log4j.appender.LogFile.layout = org.apache.log4j.PatternLayout log4j.appender.LogFile.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} -[%-t] %-5p %-4c %x -%m%n log4j.logger.httpclient.wire.header=ERROR log4j.logger.httpclient.wire.content=ERROR log4j.logger.org.apache.commons.httpclient = ERROR # Set framework log level is debug log4j.logger.java.sql=ERROR #log4j.logger.org.springframework.jdbc.core=DEBUG #log4j.logger.com.opensymphony.oscache.base=DEBUG org.apache.http.client.protocol=ERROR # log4j.logger.org.springframework.security=DEBUG log4j.logger.java.sql.Connection=DEBUG log4j.logger.java.sql.Statement=DEBUG log4j.logger.java.sql.PreparedStatement=DEBUG
较为完善的: 区分级别,可根据不同组织制定不同日志输出的 log4j.xml
1 <?xml version="1.0" encoding="UTF-8"?> 2 <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> 3 <log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'> 4 <appender name="stdout" class="org.apache.log4j.ConsoleAppender"> 5 <layout class="org.apache.log4j.PatternLayout"> 6 <param name="ConversionPattern" value="[%t] %d{yyyy.MM.dd HH:mm:ss} %5p - %m%n" /> 7 </layout> 8 </appender> 9 <!--***********************************crawler日志 ********************************** --> 10 <!--****INFO级别***** --> 11 <appender name="crawler_infoLog" class="org.apache.log4j.RollingFileAppender"> 12 <param name="File" value="./logs/crawler_info.log" /> 13 <param name="maxFileSize" value="2000KB" /> 14 <param name="maxBackupIndex" value="10" /> 15 <layout class="org.apache.log4j.PatternLayout"> 16 <param name="ConversionPattern" value="[%t] %d{yyyy.MM.dd HH:mm:ss} %5p - %m%n" /> 17 </layout> 18 <filter class="org.apache.log4j.varia.LevelRangeFilter"> 19 <param name="LevelMax" value="INFO" /> 20 <param name="LevelMin" value="INFO" /> 21 </filter> 22 </appender> 23 24 25 <!--****WARN级别***** --> 26 <appender name="crawler_warnLog" class="org.apache.log4j.RollingFileAppender"> 27 <param name="File" value="./logs/crawler_warn.log" /> 28 <param name="maxFileSize" value="2000KB" /> 29 <param name="maxBackupIndex" value="10" /> 30 <layout class="org.apache.log4j.PatternLayout"> 31 <param name="ConversionPattern" value="[%t] %d{yyyy.MM.dd HH:mm:ss} %5p - %m%n" /> 32 </layout> 33 <filter class="org.apache.log4j.varia.LevelRangeFilter"> 34 <param name="LevelMax" value="WARN" /> 35 <param name="LevelMin" value="WARN" /> 36 </filter> 37 </appender> 38 <!--****REEOR级别***** --> 39 <appender name="crawler_errorLog" class="org.apache.log4j.RollingFileAppender"> 40 <param name="File" value="./logs/crawler_error.log" /> 41 <param name="maxFileSize" value="2000KB" /> 42 <param name="maxBackupIndex" value="10" /> 43 <layout class="org.apache.log4j.PatternLayout"> 44 <param name="ConversionPattern" value="[%t] %d{yyyy.MM.dd HH:mm:ss} %5p - %m%n" /> 45 </layout> 46 <filter class="org.apache.log4j.varia.LevelRangeFilter"> 47 <param name="LevelMax" value="ERROR" /> 48 <param name="LevelMin" value="ERROR" /> 49 </filter> 50 </appender> 51 <!--****FATAL级别***** --> 52 <appender name="crawler_fatalLog" class="org.apache.log4j.RollingFileAppender"> 53 <param name="File" value="./logs/crawler_fatal.log" /> 54 <param name="maxFileSize" value="2000KB" /> 55 <param name="maxBackupIndex" value="10" /> 56 <layout class="org.apache.log4j.PatternLayout"> 57 <param name="ConversionPattern" value="[%t] %d{yyyy.MM.dd HH:mm:ss} %5p - %m%n" /> 58 </layout> 59 <filter class="org.apache.log4j.varia.LevelRangeFilter"> 60 <param name="LevelMax" value="FATAL" /> 61 <param name="LevelMin" value="FATAL" /> 62 </filter> 63 </appender> 64 65 <!-- 记录该包下所有日志 --> 66 <logger name="org.apache"> 67 <level value="INFO" /> 68 <appender-ref ref="stdout" /> 69 <appender-ref ref="crawler_infoLog" /> 70 <appender-ref ref="crawler_warnLog" /> 71 <appender-ref ref="crawler_errorLog" /> 72 <appender-ref ref="crawler_fatalLog" /> 73 </logger> 74 <logger name="com.alibaba.druid"> 75 <level value="INFO" /> 76 <appender-ref ref="stdout" /> 77 <appender-ref ref="crawler_infoLog" /> 78 <appender-ref ref="crawler_warnLog" /> 79 <appender-ref ref="crawler_errorLog" /> 80 <appender-ref ref="crawler_fatalLog" /> 81 </logger> 82 83 84 <logger name="com.loongtao.dmscrawler"> 85 <level value="INFO" /> 86 <appender-ref ref="stdout" /> 87 <appender-ref ref="crawler_infoLog" /> 88 <appender-ref ref="crawler_warnLog" /> 89 <appender-ref ref="crawler_errorLog" /> 90 <appender-ref ref="crawler_fatalLog" /> 91 </logger> 92 93 94 </log4j:configuration>
记录日志写法 ,error 级别的 logger.error(e.getMessage(),e); 【能显示详尽的信息,符合sona的语法检查 】