我错过了什么?我有以下代码成功发送电子邮件,但电子邮件的正文是空的。我认为它与patternLayout有关,但一切看起来还不错。关于发生了什么的任何想法?
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);
}
答案 0 :(得分:0)
问题是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]
一切都很好。