简体   繁体   English

如何在log4j2的SmtpAppender中设置自定义主题

[英]How do I set a custom subject in SmtpAppender in log4j2

Our application is deployed as a servlet war to multiple tomcat servers under multiple customer contexts: 我们的应用程序作为servlet war部署到了多个客户上下文中的多个tomcat服务器上:

customer#application.war

We're using log4j2 as our logging and alert email mechanism. 我们使用log4j2作为日志记录和警报电子邮件机制。 So far, everything is working great and our fatal errors are being sent. 到目前为止,一切工作正常,我们的致命错误正在发送。 However, as we deploy to new contexts, it's becoming less clear which customer is generating the error. 但是,随着我们部署到新的上下文中,越来越不清楚哪个客户正在产生错误。

So far, it appears that the subject value is static and set in the config file and the system variables are loaded when the logger is built: 到目前为止,看来主题值是静态的,并且已在配置文件中设置,并且在构建记录器时会加载系统变量:

subject="[${applicationname}] Fatal Error ${hostname}:${sys:pwd}"

While it appears that there is a way to ascertain the name of our deployed context via the servlet API, we have yet to determine how to introduce this value in the email subject programmatically and dynamically at run time. 尽管似乎有一种方法可以通过Servlet API来确定我们部署的上下文的名称,但我们尚未确定如何在运行时以编程方式和动态方式在电子邮件主题中引入此值。

This would greatly reduce the time it takes to research an error. 这将大大减少研究错误所需的时间。 Any tips? 有小费吗?

So far we've considered the following: 到目前为止,我们已经考虑了以下内容:

  1. Custom war file with custom log4j2 config for each customer context (very hackish) 具有针对每个客户上下文的自定义log4j2配置的自定义war文件(非常黑)
  2. Update all log.fatal calls to include the context info from the servlet (horrid) 更新所有log.fatal调用以包括Servlet中的上下文信息(可怕的)
  3. Custom SmtpAppender (final and protected so that's out) 自定义SmtpAppender(已最终确定并受保护)
  4. Custom SmtpManager and override the subject in the MimeMessage object (seems workable but the documentation does not show how to implement) 自定义SmtpManager并覆盖MimeMessage对象中的主题(似乎可行,但文档未显示如何实现)

TIA!! TIA!

Piko PIKO

This is actually a known issue in Log4j2 as of 2.9.1. 从2.9.1开始,这实际上是Log4j2中的一个已知问题。 The problem is that a MimeMessage is cached and the subject becomes a fixed value. 问题是MimeMessage被缓存,并且主题变为固定值。 A simple solution would be to stop caching. 一个简单的解决方案是停止缓存。

There is an open ticket to address this: Log4j2-1450 . 有一个开放的票证可以解决此问题: Log4j2-1450 (Related: Log4j2-1192 , which implemented pattern lookups but didn't fix that MimeMessages are cached.) (相关: Log4j2-1192 ,它实现了模式查找,但没有解决MimeMessages被缓存的问题。)

If you can provide a patch or a pull request it would greatly increase the chances of this being addressed speedily. 如果您可以提供补丁或请求请求,则将大大增加迅速解决此问题的机会。


Update: looking my old comment in that ticket: 更新:在票证中查找我的旧评论:

Looks like subject already supports $$ runtime lookups. 看起来主题已经支持$$运行时查找。 The following attributes are used for each email that is sent, and it should be possible to support runtime lookups for these attributes: * from * replyto * to * cc * bcc * subject (already a runtime lookup) 以下属性用于所发送的每封电子邮件,并且应该有可能支持这些属性的运行时查找:*从* Replyto *到* cc * bcc *主题(已在运行时查找)

It should be possible to configure the subject to be a system properties lookup like this: 应该可以将主题配置为系统属性查找,如下所示:

subject = "$${sys:email.subject}"

Then you set system property email.subject to a different subject and send an email with a different subject. 然后,将系统属性email.subject设置为其他主题,然后发送包含其他主题的电子邮件。 Can you try this? 你可以试试这个吗?


Update 2: 更新2:

If system properties are not suitable, you can also create a custom lookup , this is only a few lines of code. 如果系统属性不合适,您还可以创建一个自定义lookup ,这只是几行代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM