1

我错过了什么?我有以下代码成功发送了一封电子邮件,但电子邮件的正文为空。我认为这与模式布局有关,但一切看起来都还不错。关于发生了什么的任何想法?

private void createAdminNotifyAppender(LoggerContext lc, AppConfigPropertiesReader propReader)
{
    logger.info("Setting up logback admin_notify logging");

    //Set up the admin notify appender programatically (needs to use the properties from the propReader above)
    OnMarkerEvaluator markerEvaluator = new OnMarkerEvaluator();
    markerEvaluator.addMarker(LogbackMarker.ADMIN_NOTIFY.toString());
    String adminEmails = propReader.getProperty("adminEmail");
    PatternLayout patternLayout = new PatternLayout();
    patternLayout.setPattern("[\\(%-6threadHash\\) app=iv  %d{yyyy-MM-dd HH:mm:ss.SSS}] %-5level %-25logger{25} - %msg%n");

    SMTPAppender adminNotifyAppender = new SMTPAppender();
    adminNotifyAppender.setContext(lc);
    adminNotifyAppender.setEvaluator(markerEvaluator);
    adminNotifyAppender.setSMTPHost(propReader.getProperty("smtpHost"));
    for (String adminEmail : adminEmails.split(",")) { adminNotifyAppender.addTo(adminEmail); };
    adminNotifyAppender.setFrom(propReader.getProperty("sysEmailAddressNoReply"));
    adminNotifyAppender.setSubject("System Alert! [" + propReader.getProperty("environment") + "]");
    adminNotifyAppender.setLayout(patternLayout);
    adminNotifyAppender.start();

    Logger rootLogger = lc.getLogger(Logger.ROOT_LOGGER_NAME);
    rootLogger.addAppender(adminNotifyAppender);
}
4

1 回答 1

1

问题是 PatternLayout 和 OnMarkerEvaluator 都有应该调用的启动方法(MarkerEvaluator 似乎没有它也可以工作)。

此外,两者都需要知道 loggerContext(通过调用 patternLayout.setContext(lc) 设置),否则你会得到如下输出:

[(%PARSER_ERROR[threadHash]) app=iv  %PARSER_ERROR[d]] %PARSER_ERROR[level] %PARSER_ERROR[logger] - %PARSER_ERROR[msg]%PARSER_ERROR[n]

现在一切都很好。

于 2012-07-17T20:11:06.183 回答