Log4j SMTP Appender

时间:2011-03-30 16:41:10

标签: java log4j

您好我是新手,我目前编写的应用程序使用struts 1.2和java。我们目前使用Log4j作为日志文件,但我需要实现SMTP Appender,以便将错误通过电子邮件发送给我们。

我已经尝试过一切都可以通过电子邮件发送错误,但没有运气。下面是我们的log4j.properties文件。

有什么建议吗?

谢谢!

log4j.rootLogger= INFO, stdout, logfile, mail

log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - <%m>%n

# Keep three backup files
log4j.appender.logfile.MaxBackupIndex=3
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - <%m>%n
log4j.appender.logfile=org.apache.log4j.RollingFileAppender
log4j.appender.logfile.File=C:/LOGS/WIRE.log
log4j.appender.logfile.MaxFileSize=2048KB

#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=johndoe@serverhere.com
log4j.appender.mail.To=johndoe@serverhere.com
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


# Hibernates use of the org.apache classes spews out stuff like mad.
log4j.logger.org.apache=INFO

# Springframework is very talkative too.
log4j.logger.org.springframework=INFO

# acegisecurity 
#log4j.logger.org.acegisecurity = INFO

# Quartz trigger checking 
log4j.logger.org.quartz.impl.jdbcjobstore=INFO
#log4j.logger.org.springframework.scheduling.quartz=INFO

2 个答案:

答案 0 :(得分:5)

默认情况下,appender仅在ERROR or FATAL levels上记录某些内容时发送电子邮件。 作为附注,似乎您的阈值属性可能具有不正确的情况。我相信:

log4j.appender.mail.threshold=error

应该是

log4j.appender.mail.Threshold=error

编辑...

可以通过配置log4j.debug configuration property将Log4j置于调试模式。这可能会提供有关SMTP appender的一些输出。

答案 1 :(得分:0)

在错误级别定义根记录器,并为选定的包覆盖它。邮件追加器将保持错误级别。

# Log appenders
#log4j.rootLogger=INFO, CONSOLE
log4j.rootLogger=ERROR, FILE, MAIL

# Log levels
log4j.logger.com.example.application=INFO
log4j.logger.com.example.application.package=DEBUG


# CONSOLE
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout
log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5r %-5p [%t] %c{2} - %m%n
log4j.appender.CONSOLE.Encoding=UTF-8


# FILE
log4j.appender.FILE=org.apache.log4j.RollingFileAppender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.File=/tmp/application.log
log4j.appender.FILE.MaxFileSize=1000KB
log4j.appender.FILE.MaxBackupIndex=99
log4j.appender.FILE.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5r %-5p [%t] %c{2} - %m%n
log4j.appender.FILE.Encoding=UTF-8


# MAIL
log4j.appender.MAIL=org.apache.log4j.net.SMTPAppender
log4j.appender.MAIL.BufferSize=1
log4j.appender.MAIL.SMTPHost=smtp.example.com
log4j.appender.MAIL.From=application@example.com
log4j.appender.MAIL.To=developer@example.com
log4j.appender.MAIL.Subject=Exception in Application
log4j.appender.MAIL.layout=org.apache.log4j.PatternLayout
log4j.appender.MAIL.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss,SSS} %5r %-5p [%t] %c{2} - %m%n