Logger4j的使用孝腊早1.配置文件完整配置如下:1 log4j.rootLogger=CONSOLE,FILE 2 log4j.addivity.org.apache=true 3 4 # 应用于控制台 5 log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender 6 log4j.appender.CONSOLE.Threshold=INFO 7 log4j.appender.CONSOLE.Target=System.out 8 log4j.appender.CONSOLE.Encoding=GBK 9 log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout 10 log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n 11 12 # 每天新建日志 13 log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender 14 log4j.appender.A1.File=C:/log4j/log 15 log4j.appender.A1.Encoding=GBK 16 log4j.appender.A1.Threshold=DEBUG 17 log4j.appender.A1.DatePattern='.'yyyy-MM-dd 18 log4j.appender.A1.layout=org.apache.log4j.PatternLayout 19 log4j.appender.A1.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L : %m%n 20 21 #应巧雀用于文件 22 log4j.appender.FILE=org.apache.log4j.FileAppender 23 log4j.appender.FILE.File=C:/log4j/file.log 24 log4j.appender.FILE.Append=false 25 log4j.appender.FILE.Encoding=GBK 26 log4j.appender.FILE.layout=org.apache.log4j.PatternLayout 27 log4j.appender.FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n 28 29 # 应用于文局友件回滚 30 log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender 31 log4j.appender.ROLLING_FILE.Threshold=ERROR 32 log4j.appender.ROLLING_FILE.File=rolling.log 33 log4j.appender.ROLLING_FILE.Append=true 34 log4j.appender.CONSOLE_FILE.Encoding=GBK 35 log4j.appender.ROLLING_FILE.MaxFileSize=10KB 36 log4j.appender.ROLLING_FILE.MaxBackupIndex=1 37 log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout 38 log4j.appender.ROLLING_FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n 39 40 #自定义Appender 41 log4j.appender.im = net.cybercorlin.util.logger.appender.IMAppender 42 log4j.appender.im.host = mail.cybercorlin.net 43 log4j.appender.im.username = username 44 log4j.appender.im.password = password 45 log4j.appender.im.recipient = yyflyons@163.com 46 log4j.appender.im.layout=org.apache.log4j.PatternLayout 47 log4j.appender.im.layout.ConversionPattern =[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n 48 49 #应用于socket 50 log4j.appender.SOCKET=org.apache.log4j.RollingFileAppender 51 log4j.appender.SOCKET.RemoteHost=localhost 52 log4j.appender.SOCKET.Port=5001 53 log4j.appender.SOCKET.LocationInfo=true 54 # Set up for Log Facter 5 55 log4j.appender.SOCKET.layout=org.apache.log4j.PatternLayout 56 log4j.appender.SOCET.layout.ConversionPattern=[start]%d{DATE}[DATE]%n%p[PRIORITY]%n%x[NDC]%n%t[THREAD]%n%c[CATEGORY]%n%m[MESSAGE]%n%n 57 # Log Factor 5 Appender 58 log4j.appender.LF5_APPENDER=org.apache.log4j.lf5.LF5Appender 59 log4j.appender.LF5_APPENDER.MaxNumberOfRecords=2000 60 61 # 发送日志给邮件 62 log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender 63 log4j.appender.MAIL.Threshold=FATAL 64 log4j.appender.MAIL.BufferSize=10 65 log4j.appender.MAIL.From=yyflyons@163.com 66 log4j.appender.MAIL.SMTPHost=www.wusetu.com 67 log4j.appender.MAIL.Subject=Log4J Message 68 log4j.appender.MAIL.To=yyflyons@126.com 69 log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout 70 log4j.appender.MAIL.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n 使用Log4j的时候,总是出现log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory). log4j:WARN Please initialize the log4j system properly. 解决方案:把上面详细配置的log4j.rootLogger=CONSOLE,FILE改为log4j.rootLogger=DEBUG,CONSOLE,FILE,你就发现,控制台信息又回来了 2.加载配置文件3.使用logger日志文件 具体示例如下所示:(1)Logger4j配置文件:### set log levels ###log4j.rootLogger = info,daqAppender,D,outAppender#log4j.rootLogger = DEBUG,CONSOLE,FILE log4j.appender.D = org.apache.log4j.ConsoleAppenderlog4j.appender.D.Target = System.outlog4j.appender.D.layout = org.apache.log4j.PatternLayoutlog4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n log4j.appender.daqAppender = org.apache.log4j.DailyRollingFileAppenderlog4j.appender.daqAppender.File = logs/log.loglog4j.appender.daqAppender.Append = truelog4j.appender.daqAppender.Threshold = infolog4j.appender.daqAppender.layout = org.apache.log4j.PatternLayoutlog4j.appender.daqAppender.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss} [ %t:%r ] - [ %p ] %m%n log4j.appender.outAppender = org.apache.log4j.DailyRollingFileAppenderlog4j.appender.outAppender.File = logs/error.loglog4j.appender.outAppender.Append = truelog4j.appender.outAppender.Threshold = ERRORlog4j.appender.outAppender.layout = org.apache.log4j.PatternLayoutlog4j.appender.outAppender.layout.ConversionPattern = %-d{yyyy-MM-dd HH\:mm\:ss} [ %t\:%r ] - [ %p ] %m%n (2)加载配置文件URL pa = ClassLoader.getSystemResource("resources/log4j.properties");PropertyConfigurator.configure(pa); (3)使用logger4j输出日志Loggers.daqlog.info("日志参数加载--info--"); 该Loggers日志类如下所示:import org.apache.log4j.Logger; public class Loggers {public static Logger daqlog=Logger.getLogger("daqAppender");public static Logger outlog=Logger.getLogger("outAppender");} Sue remark:上面的示例是自定义的,定义日志文件输出的位置:log4j.appender.outAppender.File = logs/error.loglog4j.appender.daqAppender.File = logs/log.log 定义日志的级别:log4j.appender.outAppender.Threshold = ERRORlog4j.appender.daqAppender.Threshold = info 定义日志的布局:log4j.appender.outAppender.layout = org.apache.log4j.PatternLayout 定义日志输出的格式:log4j.appender.outAppender.layout.ConversionPattern = %-d{yyyy-MM-dd HH\:mm\:ss} [ %t\:%r ] - [ %p ] %m%n 标识的意思:# %c 输出日记信息所属的类的全名 # %d 输出日记时间点的日期或时间,默许格式为ISO8601,也可以或许在厥后指定格式,比如:%d{yyy-MM-dd HH:mm:ss },输出近似:2002-10-# 18- 22:10:28 # %f 输出日记信息所属的类的类名 # %l 输出日记事件的产生地位,即输出日记信息的语句处于它地点的类的第几行 # %m 输出代码中指定的信息,如log(message)中的message # %n 输出一个回车换行符,Windows平台为“ ”,Unix平台为“ ” # %p 输出优先级,即DEBUG,INFO,WARN,ERROR,FATAL。假定是调用debug()输出的,则为DEBUG,依此类推 # %r 输出自利用启动到输出该日记信息所破钞的毫秒数 # %t 输生产生该日记事件的线程名