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
- Inordertosendemailloggingmessages for levelsbelowerror,youneedtosetandcreatea new EvaluatorClass.Tofollowtheexampleabove,thelo4j.propertiesfilebecomes:
- #Assignappenderstorootlogger
- log4j.rootLogger=DEBUG,myConsole,myLogFile,myMail
- #Consoleappender
- 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
- #Rollingfileappender
- 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{MMMd,yyyyhh:mm:ssa}:%p[%t]%m%n
- log4j.appender.myLogFile.threshold=WARN
- #SMTPappender
- 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=Log4JMessage
- log4j.appender.myMail.layout=org.apache.log4j.PatternLayout
- log4j.appender.myMail.layout.ConversionPattern=%5p[%t](%F:%L)-%m%n
- #THISISTHENEWLINETOSETTHEEVALUATOR
- log4j.appender.myMail.evaluatorClass=com.millersystems.example.spi.MyEvaluator
- Nowyouhavetocreatetheevaluatorclass andimplementtheorg.apache.log4j.spi.TriggeringEventEvaluator interface andplace this class inapathwherelog4jcanaccessit.
- //ExampleTriggeringEventEvaluatorimpl
- 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(LoggingEventevent){ return true ;}
- }
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/
- Iwastryingtohookupmylog4jconfigurationtomygmailaccountsothatIcouldbeimmediatelyalert for errors.Prettysimpleuse case …ButIstartedgettingerrorslike:
- com.sun.mail.smtp.SMTPSendFailedException:530 5.7 . 0 MustissueaSTARTTLScommandfirst.
- and
- com.sun.mail.smtp.SMTPSendFailedException:530 5.7 . 0 Authenticationrequired
- IhavereasontobelieveSMTPAppenderisborkedonanythingauthenticationrelatedbecauseyourauthenticationpropertiesarereadafterthejavax.mail.Sessioniscreated.Thefollowingiscodefor anoverriddenSMTPAppenderthattakesoverthesessioncreation.
- viewsourceprint?01 . import java.security.Security;
- 02 . import java.util.Properties;
- 03 . import javax.mail.PasswordAuthentication;
- 04 . import javax.mail.Session;
- 05 . import org.apache.log4j.net.SMTPAppender;
- 06 .
- 07 . public class SMTPSSLAppender extends SMTPAppender{
- 08 .
- 09 .
- public SMTPSSLAppender(){
- 10 .
- Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
- 11 .
- }
- 12 .
- 13 .
- @Override
- 14 .
- protected SessioncreateSession(){
- 15 .
- Propertiesproperties=new Properties();
- 16 .
- properties.setProperty("mail.transport.protocol" , "smtp" );
- 17 .
- properties.setProperty("mail.host" ,getSMTPHost());
- 18 .
- properties.put("mail.smtp.auth" , "true" );
- 19 .
- properties.put("mail.smtp.port" , "465" );
- 20 .
- properties.put("mail.smtp.socketFactory.port" , "465" );
- 21 .
- properties.put("mail.smtp.socketFactory.class" , "javax.net.ssl.SSLSocketFactory" );
- 22 .
- properties.put("mail.smtp.socketFactory.fallback" , "false" );
- 23 .
- properties.setProperty("mail.smtp.quitwait" , "false" );
- 24 .
- 25 .
- Sessionsession=Session.getDefaultInstance(properties,new javax.mail.Authenticator(){
- 26 .
- protected PasswordAuthenticationgetPasswordAuthentication()
- 27 .
- {return new PasswordAuthentication(getSMTPUsername(),getSMTPPassword());}
- 28 .
- });
- 29 .
- 30 .
- return session;
- 31 .
- }
- 32 .}Andthelog4j.propertieswouldhavesomethinglike:
- viewsourceprint?01 .log4j.appender.mail=SMTPSSLAppender
- 02 .log4j.appender.mail.SMTPHost=smtp.gmail.com
- 03 .log4j.appender.mail.SMTPUsername=myusername
- 04 .log4j.appender.mail.SMTPPassword=mypassword
- 05 .log4j.appender.mail.BufferSize= 1
- 06 .log4j.appender.mail.Subject=ZOMGsomeerroroccured!
- 07 .log4j.appender.mail.To=my @email .com
- 08 .log4j.appender.mail.threshold=error
- 09 .log4j.appender.mail.layout=org.apache.log4j.PatternLayout
- 10 .log4j.appender.mail.layout.ConversionPattern=%d{ABSOLUTE}%5p%c{ 1 }:%L-%m%nOfcourse,overridingsessioncreationbreaksalotofthings,butitstillhasthe 20 %offunctionalitythat 80 %offolkswilluse