【问题标题】:Log4j SMTP Send Error Messages to E-MailLog4j SMTP 将错误消息发送到电子邮件
【发布时间】:2013-02-18 23:21:51
【问题描述】:

我在尝试让我的 Log4j 在出现问题时发送 SMTP 电子邮件时遇到了一个大问题。这是我的代码:

final Properties properties = new Properties();
final String strProperties = "log4j.rootLogger=ERROR, console, smtp\n"
            + "log4j.appender.console=org.apache.log4j.ConsoleAppender\n"
            + "log4j.appender.console.Target=System.out\n"
            + "log4j.appender.console.layout=org.apache.log4j.PatternLayout\n"
            + "log4j.appender.console.layout.ConversionPattern=%-5p: %c - %m%n\n"
            + "log4j.appender.smtp=org.apache.log4j.net.SMTPAppender\n"
            + "log4j.appender.smtp.EvaluatorClass=com.blah.blah.log4j.TriggerOnError\n"
            + "log4j.appender.smtp.SMTPHost=localhost\n"
            + "log4j.appender.smtp.From=mml_automation@blah.com\n"
            + "log4j.appender.smtp.To=ccmis@blah.com\n"
            + "log4j.appender.smtp.Subject=BagBox ERROR\n"
            + "log4j.appender.smtp.layout=org.apache.log4j.PatternLayout\n"
            + "log4j.appender.smtp.threshold=INFO\n"
            + "log4j.appender.smtp.layout.ConversionPattern=%d{yyyy-MM-dd HH:mm:ss} : %-40c : %-5p %x - %m %n\n"
            + "log4j.logger.com.blah.blah.service.BagBoxService=INFO, smtp\n"
            + "log4j.logger.com.blah.blah.security=INFO, smtp\n"
            + "log4j.logger.org.springframework.core=ERROR, console, smtp\n"
            + "log4j.logger.org.springframework.beans=ERROR, console, smtp\n"
            + "log4j.logger.org.springframework.context=ERROR, console, smtp\n"
            + "log4j.logger.org.springframework.web=ERROR, console, smtp\n"
            + "log4j.logger.org.springframework.web.servlet.DispatcherServlet=ERROR, console, smtp\n"
            + "log4j.logger.org.springframework.security=ERROR, console, smtp\n"
            + "log4j.logger.org.springframework.aop=ERROR, console, smtp\n"
            + "log4j.logger.org.springframework.ui=ERROR, console, smtp\n";

    final ByteArrayInputStream byteProperties = new ByteArrayInputStream(
            strProperties.getBytes());
    properties.load(byteProperties);
    PropertyConfigurator.configure(properties);

我的控制台中不断出现以下错误:

Could not connect to SMTP host: localhost, port: 25;
  nested exception is:
java.net.SocketException: Network is unreachable: connect

如果我在 IP 地址上运行 telnet,它会正常返回并且可以正常连接。谁能告诉我我做错了什么?谢谢。

【问题讨论】:

    标签: java smtp log4j


    【解决方案1】:

    我猜您的 SMTP 守护程序正在侦听“真实”IP,例如 192.168.1.123,因此未绑定到localhost (127.0.0.1),如

    中所示
    log4j.appender.smtp.SMTPHost=localhost
    

    netstat -anpt | grep 25 告诉你什么?您是否可能在 25 以外的端口上运行 SMTP?

    干杯,

    【讨论】:

    • 我实际上得到了这个工作。但是,现在我有一个不同的问题。如果 log4j 向 DB 发送消息时出现问题(即,如果它无法连接到 mySQL),它将不会发送电子邮件。如果数据库部分出现故障,log4j 的电子邮件部分还能工作吗?
    • 如果没有看到您的代码和数据库附加程序配置,这是不可能具体回答的。不过,一般来说,您可能希望查看FailoverAppender 并将 DB appender 定义为您的主服务器,并将 SMTP 定义为故障转移/辅助服务器。不确定它是否完全适合您的用例,但您可以从那里开始工作。
    • 我想我的问题是,如果 log4j 本身出现错误,您将如何发送电子邮件?它不发送的原因是因为它仍然是INFO级别,但是有log4j:ERROR JDBCAppender::append(), Not ready to append !,那我怎么发邮件呢?
    • 如果 Log4j 本身出现错误,我不希望 Log4j 能够附加到电子邮件。你期待什么?
    猜你喜欢
    • 2017-06-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-16
    相关资源
    最近更新 更多