0

当我使用 org.apache.log4j.net.SMTPAppender 并添加错误的 smtp 主机时,log4j 会引发异常。但是这个例外只在控制台中。其他味精很好地写入文件。我想将此异常记录到文件中。这个怎么做?

我的设置:

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
log4j.appender.email.To=testeasdasdasdasd@asdasdsa.com
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
4

2 回答 2

0

问题出在 log4j 源代码中:在 org.apache.log4j.helpers.LogLog 类中。问题:System.err.println(ERR_PREFIX+msg);

于 2012-08-29T11:22:15.610 回答
0
            log4j.appender.FileAppender.MaxBackupIndex=10

add this line to your log4j.properties file. It may help you.

于 2012-08-29T06:11:57.537 回答