我们在使用Log4j的时候,总是出现:
1 log4j:WARN No appenders could be found for logger (org.apache.ibatis.logging.LogFactory). 2 log4j:WARN Please initialize the log4j system properly.
这个问题是因为我们的log4j.properties文件配置不够完整,所以我们给它配置齐了就不会再出现这个问题。
log4j.properties不完整配置如下:
1 log4j.rootLogger=DEBUG, stdout 2 3 log4j.appender.stdout=org.apache.log4j.ConsoleAppender 4 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 5 log4j.appender.stdout.layout.ConversionPattern=%c{1} - %m%n 6 7 log4j.logger.java.sql.PreparedStatement=DEBUG
完整配置如下:
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控制台信息也没有了,其实只要把上面详细配置的log4j.rootLogger=CONSOLE,FILE改为log4j.rootLogger=DEBUG,CONSOLE,FILE,你就发现,控制台信息又回来了。
1 log4j.rootLogger=CONSOLE,FILE 2 #log4j.rootLogger=DEBUG,CONSOLE,FILE 3 log4j.addivity.org.apache=true