你的位置:首页 > Java教程

[Java教程](转)log4j:WARN No appenders could be found for logger 解决方案


我们在使用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.PatternLayout10 log4j.appender.CONSOLE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n11 12 # 每天新建日志13 log4j.appender.A1=org.apache.log4j.DailyRollingFileAppender14 log4j.appender.A1.File=C:/log4j/log15 log4j.appender.A1.Encoding=GBK16 log4j.appender.A1.Threshold=DEBUG17 log4j.appender.A1.DatePattern='.'yyyy-MM-dd18 log4j.appender.A1.layout=org.apache.log4j.PatternLayout19 log4j.appender.A1.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{1}:%L : %m%n20 21 #应用于文件22 log4j.appender.FILE=org.apache.log4j.FileAppender23 log4j.appender.FILE.File=C:/log4j/file.log24 log4j.appender.FILE.Append=false25 log4j.appender.FILE.Encoding=GBK26 log4j.appender.FILE.layout=org.apache.log4j.PatternLayout27 log4j.appender.FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n28 29 # 应用于文件回滚30 log4j.appender.ROLLING_FILE=org.apache.log4j.RollingFileAppender31 log4j.appender.ROLLING_FILE.Threshold=ERROR32 log4j.appender.ROLLING_FILE.File=rolling.log33 log4j.appender.ROLLING_FILE.Append=true34 log4j.appender.CONSOLE_FILE.Encoding=GBK35 log4j.appender.ROLLING_FILE.MaxFileSize=10KB36 log4j.appender.ROLLING_FILE.MaxBackupIndex=137 log4j.appender.ROLLING_FILE.layout=org.apache.log4j.PatternLayout38 log4j.appender.ROLLING_FILE.layout.ConversionPattern=[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n39 40 #自定义Appender41 log4j.appender.im = net.cybercorlin.util.logger.appender.IMAppender42 log4j.appender.im.host = mail.cybercorlin.net43 log4j.appender.im.username = username44 log4j.appender.im.password = password45 log4j.appender.im.recipient = yyflyons@163.com46 log4j.appender.im.layout=org.apache.log4j.PatternLayout47 log4j.appender.im.layout.ConversionPattern =[framework] %d - %c -%-4r [%t] %-5p %c %x - %m%n48 49 #应用于socket50 log4j.appender.SOCKET=org.apache.log4j.RollingFileAppender51 log4j.appender.SOCKET.RemoteHost=localhost52 log4j.appender.SOCKET.Port=500153 log4j.appender.SOCKET.LocationInfo=true54 # Set up for Log Facter 555 log4j.appender.SOCKET.layout=org.apache.log4j.PatternLayout56 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%n57 # Log Factor 5 Appender58 log4j.appender.LF5_APPENDER=org.apache.log4j.lf5.LF5Appender59 log4j.appender.LF5_APPENDER.MaxNumberOfRecords=200060 61 # 发送日志给邮件62 log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender63 log4j.appender.MAIL.Threshold=FATAL64 log4j.appender.MAIL.BufferSize=1065 log4j.appender.MAIL.From=yyflyons@163.com66 log4j.appender.MAIL.SMTPHost=www.wusetu.com67 log4j.appender.MAIL.Subject=Log4J Message68 log4j.appender.MAIL.To=yyflyons@126.com69 log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout70 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,FILE2 #log4j.rootLogger=DEBUG,CONSOLE,FILE3 log4j.addivity.org.apache=true