log4j WARN No appenders could be found for logger

本文介绍如何解决Log4j在配置SMTP邮件发送时遇到的问题,包括版本不匹配导致的错误、无法发送低于ERROR级别的日志邮件及邮件发送失败等,并提供了解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

From: http://ylzhj01.javaeye.com/blog/763361

 

使用时,如果在程序中没有 log4j.properties , 则会出现如标题的warn, 程序不能运行。

把这个文件放到src下便可

 

网络较详细地说明有:

http://blog.youkuaiyun.com/azheng270/archive/2008/03/12/2173430.aspx

 

http://blog.youkuaiyun.com/hingwu/archive/2007/02/13/1509269.aspx

 

http://www.cnblogs.com/licheng/archive/2008/09/23/1297185.html

 

http://haitao.javaeye.com/blog/192491

 

 

 

log4j WARN No such property  SMTP Username

 

是因为版本不对, log4j-1.2.13.jar   用新的 log4j-1.2.16.jar解决问题

 

出现下面的问题

 

1。即使是将 log4j.appender.MAIL.Threshold=ERROR 修改为 INFO 级别 ,也只有在日志输出级别为 ERROR (即代码中调用logger.error("message")方法)时才会发送邮件,不知道是什么原因,反正现在的已经满足了需求,就没在去研究。

 

可能的解决办法http://www.manning-sandbox.com/thread.jspa?messageID=91270

 

 

Java代码   收藏代码
  1. In order to send email logging messages  for  levels below error, you need to set and create a  new  EvaluatorClass. To follow the example above, the lo4j.properties file becomes:  
  2. # Assign appenders to root logger  
  3. log4j.rootLogger=DEBUG, myConsole, myLogFile, myMail  
  4.   
  5. # Console appender  
  6. log4j.appender.myConsole=org.apache.log4j.ConsoleAppender  
  7. log4j.appender.myConsole.layout=org.apache.log4j.PatternLayout  
  8. log4j.appender.myConsole.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n  
  9.   
  10. # Rolling file appender  
  11. log4j.appender.myLogFile=org.apache.log4j.RollingFileAppender  
  12. log4j.appender.myLogFile.File=mylog.log  
  13. log4j.appender.myLogFile.MaxFileSize=100KB  
  14. log4j.appender.myLogFile.MaxBackupIndex=2   
  15. log4j.appender.myLogFile.layout=org.apache.log4j.PatternLayout  
  16. log4j.appender.myLogFile.layout.ConversionPattern=%d{MMM d, yyyy hh:mm:ss a}: %p [%t] %m%n  
  17. log4j.appender.myLogFile.threshold=WARN  
  18.   
  19. # SMTP appender  
  20. log4j.appender.myMail=org.apache.log4j.net.SMTPAppender  
  21. log4j.appender.myMail.Threshold=WARN  
  22. log4j.appender.myMail.BufferSize=10   
  23. log4j.appender.myMail.To=username@agency .gov  
  24. log4j.appender.myMail.From=username@agency .gov  
  25. log4j.appender.myMail.SMTPHost=smtphost  
  26. log4j.appender.myMail.Subject=Log4J Message  
  27. log4j.appender.myMail.layout=org.apache.log4j.PatternLayout  
  28. log4j.appender.myMail.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n  
  29. # THIS IS THE NEW LINE TO SET THE EVALUATOR  
  30. log4j.appender.myMail.evaluatorClass=com.millersystems.example.spi.MyEvaluator  
  31.   
  32.   
  33. Now you have to create the evaluator class  and implement the org.apache.log4j.spi.TriggeringEventEvaluator  interface  and place  this   class  in a path where log4j can access it.  
  34.   
  35. //Example TriggeringEventEvaluator impl   
  36.   
  37. package  com.millersystems.example.spi;  
  38.   
  39. import  org.apache.log4j.spi.LoggingEvent;  
  40. import  org.apache.log4j.spi.TriggeringEventEvaluator;  
  41. public   class  MyEvaluator  implements  TriggeringEventEvaluator {  
  42.   
  43. public   boolean  isTriggeringEvent(LoggingEvent event) {  return   true ; }  
  44.   
  45. }   
In order to send email logging messages for levels below error, you need to set and create a new EvaluatorClass. To follow the example above, the lo4j.properties file becomes:
# Assign appenders to root logger
log4j.rootLogger=DEBUG, myConsole, myLogFile, myMail

# Console appender
log4j.appender.myConsole=org.apache.log4j.ConsoleAppender
log4j.appender.myConsole.layout=org.apache.log4j.PatternLayout
log4j.appender.myConsole.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n

# Rolling file appender
log4j.appender.myLogFile=org.apache.log4j.RollingFileAppender
log4j.appender.myLogFile.File=mylog.log
log4j.appender.myLogFile.MaxFileSize=100KB
log4j.appender.myLogFile.MaxBackupIndex=2
log4j.appender.myLogFile.layout=org.apache.log4j.PatternLayout
log4j.appender.myLogFile.layout.ConversionPattern=%d{MMM d, yyyy hh:mm:ss a}: %p [%t] %m%n
log4j.appender.myLogFile.threshold=WARN

# SMTP appender
log4j.appender.myMail=org.apache.log4j.net.SMTPAppender
log4j.appender.myMail.Threshold=WARN
log4j.appender.myMail.BufferSize=10
log4j.appender.myMail.To=username@agency.gov
log4j.appender.myMail.From=username@agency.gov
log4j.appender.myMail.SMTPHost=smtphost
log4j.appender.myMail.Subject=Log4J Message
log4j.appender.myMail.layout=org.apache.log4j.PatternLayout
log4j.appender.myMail.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
# THIS IS THE NEW LINE TO SET THE EVALUATOR
log4j.appender.myMail.evaluatorClass=com.millersystems.example.spi.MyEvaluator


Now you have to create the evaluator class and implement the org.apache.log4j.spi.TriggeringEventEvaluator interface and place this class in a path where log4j can access it.

//Example TriggeringEventEvaluator impl

package com.millersystems.example.spi;

import org.apache.log4j.spi.LoggingEvent;
import org.apache.log4j.spi.TriggeringEventEvaluator;
public class MyEvaluator implements TriggeringEventEvaluator {

public boolean isTriggeringEvent(LoggingEvent event) { return true; }

} 

 

 

2。log4j:ERROR Error occured while sending e-mail notification.
javax.mail.SendFailedException: Sending failed;
  nested exception is:
 class javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. d39sm646911wam.4

 

解决办法:

 

http://codelol.com/2009/09/log4j-smtpappender-and-authentication/

 

Java代码   收藏代码
  1. I was trying to hook up my log4j configuration to my gmail account so that I could be immediately alert  for  errors. Pretty simple use  case … But I started getting errors like:   
  2.   
  3. com.sun.mail.smtp.SMTPSendFailedException: 530   5.7 . 0  Must issue a STARTTLS command first.  
  4.   
  5. and   
  6.   
  7. com.sun.mail.smtp.SMTPSendFailedException: 530   5.7 . 0  Authentication required  
  8.   
  9. I have reason to believe SMTPAppender is borked on anything authentication related because your authentication properties are read after the javax.mail.Session is created. The following is code for  an overridden SMTPAppender that takes over the session creation.   
  10.   
  11. view sourceprint?01 . import  java.security.Security;   
  12. 02 . import  java.util.Properties;   
  13. 03 . import  javax.mail.PasswordAuthentication;   
  14. 04 . import  javax.mail.Session;   
  15. 05 . import  org.apache.log4j.net.SMTPAppender;   
  16. 06 .   
  17.    
  18. 07 . public   class  SMTPSSLAppender  extends  SMTPAppender {   
  19. 08 .   
  20.    
  21. 09 .      
  22. public  SMTPSSLAppender() {   
  23. 10 .          
  24. Security.addProvider(new  com.sun.net.ssl.internal.ssl.Provider());   
  25. 11 .      
  26. }   
  27. 12 .   
  28.    
  29. 13 .      
  30. @Override   
  31. 14 .      
  32. protected  Session createSession() {   
  33. 15 .          
  34. Properties properties = new  Properties();   
  35. 16 .          
  36. properties.setProperty("mail.transport.protocol""smtp" );   
  37. 17 .          
  38. properties.setProperty("mail.host" , getSMTPHost());   
  39. 18 .          
  40. properties.put("mail.smtp.auth""true" );   
  41. 19 .          
  42. properties.put("mail.smtp.port""465" );   
  43. 20 .          
  44. properties.put("mail.smtp.socketFactory.port""465" );   
  45. 21 .          
  46. properties.put("mail.smtp.socketFactory.class" , "javax.net.ssl.SSLSocketFactory" );   
  47. 22 .          
  48. properties.put("mail.smtp.socketFactory.fallback""false" );   
  49. 23 .          
  50. properties.setProperty("mail.smtp.quitwait""false" );   
  51. 24 .   
  52.    
  53. 25 .          
  54. Session session = Session.getDefaultInstance(properties, new  javax.mail.Authenticator() {   
  55. 26 .              
  56. protected  PasswordAuthentication getPasswordAuthentication()   
  57. 27 .              
  58. return   new  PasswordAuthentication(getSMTPUsername(),getSMTPPassword());   }   
  59. 28 .          
  60. });        
  61. 29 .   
  62.    
  63. 30 .          
  64. return  session;   
  65. 31 .      
  66. }   
  67. 32 .}And the log4j.properties would have something like:  
  68.   
  69. view sourceprint?01 .log4j.appender.mail=SMTPSSLAppender   
  70. 02 .log4j.appender.mail.SMTPHost=smtp.gmail.com   
  71. 03 .log4j.appender.mail.SMTPUsername=myusername   
  72. 04 .log4j.appender.mail.SMTPPassword=mypassword   
  73. 05 .log4j.appender.mail.BufferSize= 1   
  74. 06 .log4j.appender.mail.Subject=ZOMG some error occured!   
  75. 07 .log4j.appender.mail.To=my @email .com   
  76. 08 .log4j.appender.mail.threshold=error   
  77. 09 .log4j.appender.mail.layout=org.apache.log4j.PatternLayout   
  78. 10 .log4j.appender.mail.layout.ConversionPattern=%d{ABSOLUTE} %5p %c{ 1 }:%L - %m%nOf course, overriding session creation breaks a lot of things, but it still has the  20 % of functionality that  80 % of folks will use 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值