2012-08-28 172 views
0

當我使用org.apache.log4j.net.SMTPAppender並添加一個錯誤的smtp主機時,log4j會引發異常。但是這個例外只在控制檯中。其他味精寫入文件。我想將這個異常記錄到文件中。這個怎麼做?log4j javax.mail.MessagingException日誌到文件

我的設置:

log4j.rootLogger=INFO, FileAppender, SystemOut_Appender, email 

log4j.appender.FileAppender=com.apxx.application.CompressDailyRollingFileAppender 
log4j.appender.FileAppender.File=C:/log.log 
log4j.appender.FileAppender.layout=org.apache.log4j.PatternLayout 
log4j.appender.FileAppender.layout.ConversionPattern=%d{yyyy.MM.dd HH:mm:ss.SSS} [%p] (%C{1}.%M:%L) :: %X{sessionID} %m%n 
log4j.appender.FileAppender.DatePattern='.'yyyy-MM-dd 
log4j.appender.FileAppender.Threshold=DEBUG 
log4j.appender.FileAppender.MaxFileSize=10MB 

log4j.appender.SystemOut_Appender=org.apache.log4j.ConsoleAppender 
log4j.appender.SystemOut_Appender.layout=org.apache.log4j.PatternLayout 
log4j.appender.SystemOut_Appender.layout.ConversionPattern=%d{yyyy.MM.dd HH:mm:ss.SSS} [%p] (%C{1}.%M:%L) :: %X{sessionID} %m%n 
log4j.appender.SystemOut_Appender.Threshold=DEBUG 

log4j.appender.SystemErr_Appender=org.apache.log4j.ConsoleAppender 
log4j.appender.SystemErr_Appender.Target=System.err 
log4j.appender.SystemErr_Appender.layout=org.apache.log4j.PatternLayout 
log4j.appender.SystemErr_Appender.layout.ConversionPattern=%d{yyyy.MM.dd HH:mm:ss.SSS} [%p] (%C{1}.%M:%L) :: %X{sessionID} %m%n 
log4j.appender.SystemErr_Appender.Threshold=ERROR 


#CONFIGURE SMTP 
log4j.appender.email=org.apache.log4j.net.SMTPAppender 
log4j.appender.email.SMTPHost=fakeHost 
log4j.appender.email.SMTPUsername=asdas 
log4j.appender.email.SMTPPassword=asdasd 
log4j.appender.email.From=wasdaasdasd 
[email protected] 
log4j.appender.email.Subject= error 
log4j.appender.email.BufferSize=512 
log4j.appender.email.layout=org.apache.log4j.PatternLayout 
log4j.appender.email.layout.ConversionPattern=%d{yyyy.MM.dd HH:mm:ss.SSS} [%p] (%C{1}.%M:%L) :: %X{sessionID} %m%n 
log4j.appender.email.Threshold=ERROR 

log4j.logger.com.apxx=ALL 

唯一的例外是:

log4j:ERROR Error occured while sending e-mail notification. 
javax.mail.MessagingException: Unknown SMTP host: fakeHost; 
    nested exception is: 
    java.net.UnknownHostException: fakeHost 
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1280) 
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:370) 
    at javax.mail.Service.connect(Service.java:297) 
    at javax.mail.Service.connect(Service.java:156) 
    at javax.mail.Service.connect(Service.java:105) 
    at javax.mail.Transport.send0(Transport.java:168) 
    at javax.mail.Transport.send(Transport.java:98) 
    at org.apache.log4j.net.SMTPAppender.sendBuffer(SMTPAppender.java:416) 
    at org.apache.log4j.net.SMTPAppender.append(SMTPAppender.java:256) 
    at org.apache.log4j.AppenderSkeleton.doAppend(AppenderSkeleton.java:251) 
    at org.apache.log4j.helpers.AppenderAttachableImpl.appendLoopOnAppenders(AppenderAttachableImpl.java:66) 
    at org.apache.log4j.Category.callAppenders(Category.java:206) 
    at org.apache.log4j.Category.forcedLog(Category.java:391) 
    at org.apache.log4j.Category.log(Category.java:856) 
    at org.apache.commons.logging.impl.Log4JLogger.error(Log4JLogger.java:245) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) 
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:39) 
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:27) 
    at java.lang.reflect.Constructor.newInstance(Constructor.java:513) 
    at java.lang.Class.newInstance0(Class.java:355) 
    at java.lang.Class.newInstance(Class.java:308) 
    at com.sun.faces.config.ManagedBeanFactoryImpl.newInstance(ManagedBeanFactoryImpl.java:275) 
    at com.sun.faces.application.ApplicationAssociate.createAndMaybeStoreManagedBeans(ApplicationAssociate.java:512) 
    at com.sun.faces.el.ManagedBeanELResolver.getValue(ManagedBeanELResolver.java:82) 
    at javax.el.CompositeELResolver.getValue(CompositeELResolver.java:54) 
    at com.sun.faces.el.FacesCompositeELResolver.getValue(FacesCompositeELResolver.java:64) 
    at org.apache.el.parser.AstIdentifier.getValue(AstIdentifier.java:69) 
    at org.apache.el.parser.AstValue.getValue(AstValue.java:112) 
    at org.apache.el.ValueExpressionImpl.getValue(ValueExpressionImpl.java:186) 
    at com.sun.facelets.el.ELText$ELTextVariable.toString(ELText.java:174) 
    at com.sun.facelets.compiler.AttributeInstruction.write(AttributeInstruction.java:49) 
    at com.sun.facelets.compiler.UIInstructions.encodeBegin(UIInstructions.java:39) 
    at com.sun.facelets.compiler.UILeaf.encodeAll(UILeaf.java:149) 
    at javax.faces.component.UIComponent.encodeAll(UIComponent.java:892) 
    at com.sun.facelets.FaceletViewHandler.renderView(FaceletViewHandler.java:592) 
    at org.ajax4jsf.application.ViewHandlerWrapper.renderView(ViewHandlerWrapper.java:100) 
    at org.ajax4jsf.application.AjaxViewHandler.renderView(AjaxViewHandler.java:176) 
    at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:106) 
    at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:251) 
    at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:144) 
    at javax.faces.webapp.FacesServlet.service(FacesServlet.java:245) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.ajax4jsf.webapp.BaseXMLFilter.doXmlFilter(BaseXMLFilter.java:206) 
    at org.ajax4jsf.webapp.BaseFilter.handleRequest(BaseFilter.java:290) 
    at org.ajax4jsf.webapp.BaseFilter.processUploadsAndHandleRequest(BaseFilter.java:388) 
    at org.ajax4jsf.webapp.BaseFilter.doFilter(BaseFilter.java:515) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:145) 
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)   
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235) 
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206) 
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233) 
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) 
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:470) 
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) 
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102) 
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) 
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) 
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:859) 
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:602) 
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) 
    at java.lang.Thread.run(Thread.java:662) 
Caused by: java.net.UnknownHostException: fakeHost 
    at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:195) 
    at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:366) 
    at java.net.Socket.connect(Socket.java:529) 
    at java.net.Socket.connect(Socket.java:478) 
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:232) 
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:189) 
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1250) 
    ... 69 more 
+0

檢查你的代碼是否給出異常catch塊的任何println語句 – Bathakarai

+0

我的文章有什麼問題? – Sisak

+0

我告訴驗證您的源代碼是否存在catch塊內的任何println語句。 – Bathakarai

回答

0
  log4j.appender.FileAppender.MaxBackupIndex=10 

這一行添加到您的log4j.properties文件。它可能會幫助你。

0

問題出在log4j源代碼中: 在類org.apache.log4j.helpers.LogLog中。 問題: System.err.println(ERR_PREFIX + msg);