在apache tomcat server start中通过电子邮件发送错误和异常

时间:2013-11-11 07:07:02

标签: java xml apache tomcat logging

我使用了apache-tomcat-7.0.25并在我的系统上启动了一个本地实例。 我必须通过电子邮件向我在日志文件中找到的各种人发送错误和异常。 我有配置文件server.xml,web.xml,tomcat-users.xml,context.xml,catalina.properties文件以及logging.properties文件。

logging.properties文件包含以下代码。

handlers = 1catalina.org.apache.juli.FileHandler, \
           2localhost.org.apache.juli.FileHandler, \
           3manager.org.apache.juli.FileHandler, \
           java.util.logging.ConsoleHandler

.handlers = 1catalina.org.apache.juli.FileHandler, java.util.logging.ConsoleHandler

############################################################
# Handler specific properties.
# Describes specific configuration info for Handlers.
############################################################

1catalina.org.apache.juli.FileHandler.level = FINE
1catalina.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
1catalina.org.apache.juli.FileHandler.prefix = catalina.

2localhost.org.apache.juli.FileHandler.level = FINE
2localhost.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
2localhost.org.apache.juli.FileHandler.prefix = localhost.

3manager.org.apache.juli.FileHandler.level = FINE
3manager.org.apache.juli.FileHandler.directory = ${catalina.base}/logs
3manager.org.apache.juli.FileHandler.prefix = manager.
3manager.org.apache.juli.FileHandler.bufferSize = 16384

java.util.logging.ConsoleHandler.level = FINE
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter


############################################################
# Facility specific properties.
# Provides extra control for each logger.
############################################################

org.apache.catalina.core.ContainerBase.[Catalina].[localhost].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].handlers = \
   2localhost.org.apache.juli.FileHandler

org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].level = INFO
org.apache.catalina.core.ContainerBase.[Catalina].[localhost].[/manager].handlers = \
   3manager.org.apache.juli.FileHandler

# For example, set the org.apache.catalina.util.LifecycleBase logger to log
# each component that extends LifecycleBase changing state:
#org.apache.catalina.util.LifecycleBase.level = FINE

如果出现任何错误或异常,如何将其重定向到邮件?

2 个答案:

答案 0 :(得分:2)

免责声明:我是JavaMail项目中包含的MailHandler的内容开发人员。

对于java.util.logging,您可以使用MailHandler中包含的JavaMail

  1. 下载JavaMail参考实施jar文件(javax.mail.jar)。
  2. Tomcat引导程序ClassLoader中的
  3. Modify the tomcat startup script include JavaMail
  4. Modify the logging.properties文件来安装MailHandler和任何支持类。
  5. 以下是一个示例配置:

    handlers = 1catalina.org.apache.juli.AsyncFileHandler, 2localhost.org.apache.juli.AsyncFileHandler, 3manager.org.apache.juli.AsyncFileHandler, 4host-manager.org.apache.juli.AsyncFileHandler, java.util.logging.ConsoleHandler, com.sun.mail.util.logging.MailHandler
    
    .handlers = 1catalina.org.apache.juli.AsyncFileHandler, java.util.logging.ConsoleHandler, com.sun.mail.util.logging.MailHandler
    
    
    com.sun.mail.util.logging.MailHandler.subject=com.sun.mail.util.logging.CollectorFormatter
    #com.sun.mail.util.logging.CollectorFormatter.format=Tomcat:{0}{1}{2}{4,choice,-1#|0#|0<... {4,number,integer} more}
    #com.sun.mail.util.logging.CompactFormatter.format=[%4$-7.7s] %7$#.140s
    com.sun.mail.util.logging.MailHandler.level=WARNING
    com.sun.mail.util.logging.MailHandler.filter=com.sun.mail.util.logging.DurationFilter
    com.sun.mail.util.logging.MailHandler.pushLevel=WARNING
    com.sun.mail.util.logging.MailHandler.mail.host=some-smtp-host
    #com.sun.mail.util.logging.MailHandler.mail.user=some-user
    #com.sun.mail.util.logging.MailHandler.authenticator=some-password
    com.sun.mail.util.logging.MailHandler.mail.from=app@server.com
    #com.sun.mail.util.logging.MailHandler.mail.sender=team@list.com
    com.sun.mail.util.logging.MailHandler.mail.to=devs@bugfixers.com
    com.sun.mail.util.logging.MailHandler.verify=resolve
    com.sun.mail.util.logging.MailHandler.mail.smtp.quitwait=false
    com.sun.mail.util.logging.MailHandler.mail.smtps.quitwait=false
    com.sun.mail.util.logging.MailHandler.mail.smtp.connectiontimeout=45000
    com.sun.mail.util.logging.MailHandler.mail.smtps.connectiontimeout=45000
    com.sun.mail.util.logging.MailHandler.mail.smtp.timeout=45000
    com.sun.mail.util.logging.MailHandler.mail.smtps.timeout=45000
    

答案 1 :(得分:1)

您可以通过SMTPAppender

使用log4j

示例配置如下:

log4j.rootLogger= , mail, .. more appenders ..

#Email Appender
log4j.appender.mail=org.apache.log4j.net.SMTPAppender
log4j.appender.mail.BufferSize=1
log4j.appender.mail.SMTPHost=smtp.serverhere.com
log4j.appender.mail.From=FROMEmailAddress
log4j.appender.mail.To=List of recipients
log4j.appender.mail.Subject=Application Error
log4j.appender.mail.Threshold=ERROR
log4j.appender.mail.layout=org.apache.log4j.PatternLayout
log4j.appender.mail.layout.ConversionPattern=%d %p [%c] - <%m>%n