使用 log4j SMTPAppender 将日志发送到电子邮件
Sending log to email using log4j SMTPAppender
我有一个 Spring 启动应用程序。我正在尝试使用 SMTPAppender 将日志发送到邮件,但我没有收到任何邮件。我已经设法将 log4j 用于 ConsoleAppender 和 RollingFileAppender,所以我想我已经正确添加了 log4j,并且可以看到具有 log4j 属性的文件。
这是我的 log4j.properties 文件:
log4j.rootLogger = info, email, stdout, file
log4j.appender.email=org.apache.log4j.net.SMTPAppender
log4j.appender.email.SMTPHost=smtp.google.com
log4j.appender.email.SMTPUsername=xyz@gmail.com
log4j.appender.email.SMTPPassword=abc123
log4j.appender.email.From=xyz@gmail.com
log4j.appender.email.To=xyz@gmail.com
log4j.appender.email.Subject=Log
log4j.appender.email.BufferSize=1
log4j.appender.email.EvaluatorClass=TriggerLogEvent
log4j.appender.email.layout=org.apache.log4j.PatternLayout
log4j.appender.email.layout.ConversionPattern=%m
#------------------------------------------------------------------------------------------
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
#------------------------------------------------------------------------------------------
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=mylog.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
我从有关 SMTPAppender 的文档中找到了一个重要的部分。
By default, an email message will be sent when an ERROR or higher severity message is appended. The triggering criteria can be modified by setting the evaluatorClass property with the name of a class implementing TriggeringEventEvaluator, setting the evaluator property with an instance of TriggeringEventEvaluator or nesting a triggeringPolicy element where the specified class implements TriggeringEventEvaluator
此外,您不能使用 "smtp.google.com" - 它应该是 smtp.gmail.com
你应该使用 GmailSMTPAppender,详情在这里 -
http://www.tgerm.com/2010/05/log4j-smtpappender-gmail-custom.html
我有一个 Spring 启动应用程序。我正在尝试使用 SMTPAppender 将日志发送到邮件,但我没有收到任何邮件。我已经设法将 log4j 用于 ConsoleAppender 和 RollingFileAppender,所以我想我已经正确添加了 log4j,并且可以看到具有 log4j 属性的文件。
这是我的 log4j.properties 文件:
log4j.rootLogger = info, email, stdout, file
log4j.appender.email=org.apache.log4j.net.SMTPAppender
log4j.appender.email.SMTPHost=smtp.google.com
log4j.appender.email.SMTPUsername=xyz@gmail.com
log4j.appender.email.SMTPPassword=abc123
log4j.appender.email.From=xyz@gmail.com
log4j.appender.email.To=xyz@gmail.com
log4j.appender.email.Subject=Log
log4j.appender.email.BufferSize=1
log4j.appender.email.EvaluatorClass=TriggerLogEvent
log4j.appender.email.layout=org.apache.log4j.PatternLayout
log4j.appender.email.layout.ConversionPattern=%m
#------------------------------------------------------------------------------------------
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] (%F:%L) - %m%n
#------------------------------------------------------------------------------------------
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=mylog.log
log4j.appender.file.MaxFileSize=10MB
log4j.appender.file.MaxBackupIndex=10
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n
我从有关 SMTPAppender 的文档中找到了一个重要的部分。
By default, an email message will be sent when an ERROR or higher severity message is appended. The triggering criteria can be modified by setting the evaluatorClass property with the name of a class implementing TriggeringEventEvaluator, setting the evaluator property with an instance of TriggeringEventEvaluator or nesting a triggeringPolicy element where the specified class implements TriggeringEventEvaluator
此外,您不能使用 "smtp.google.com" - 它应该是 smtp.gmail.com 你应该使用 GmailSMTPAppender,详情在这里 - http://www.tgerm.com/2010/05/log4j-smtpappender-gmail-custom.html