log4j.appender.ERROREMAIL=org.apache.log4j.net.SMTPAppender
log4j.appender.ERROREMAIL.SMTPHost=www.company.com
log4j.appender.ERROREMAIL.Threshold=ERROR
log4j.appender.ERROREMAIL.To=email.address1@company.com,email.address2@company.com,email.address3@company.com
log4j.appender.ERROREMAIL.From=some.emailaddress.com
log4j.appender.ERROREMAIL.Subject=messagesubject1
Run Code Online (Sandbox Code Playgroud)
我正在使用上面提到的log4j属性文件来发送电子邮件
log.error("Error message");
Run Code Online (Sandbox Code Playgroud)
如何使其成为动态的,以便消息主题可以根据计算机名称(env名称)动态更改.
例如:
log4j.appender.ERROREMAIL.Subject=messagesubject1, messagesubject2, messagesubject3
Run Code Online (Sandbox Code Playgroud)
我想根据机器名称动态使用主题1,2和3.
任何帮助将不胜感激.谢谢
您应该只需要使用hostname变量,例如:
log4j.appender.ERROREMAIL.Subject=${hostname}
Run Code Online (Sandbox Code Playgroud)
根据您的paritcular配置和操作系统,您可能需要使用-Dhostname ='machinename'或-Dhostname = $ HOST在statrup中将此变量提供给JVM
在下面的代码中,我读出了log4j.properties文件,将属性log4j.appender.ERROREMAIL.Subject设置为emailRecipients并重置 log4j 配置。可以在应用程序启动时完成,您只需正确设置emailRecepients字符串即可。
Properties props = new Properties();
try {
InputStream configStream = Thread.class.getResourceAsStream("/log4j.properties");
if (configStream == null) {
throw new RuntimeException();
}
props.load(configStream);
configStream.close();
} catch(Throwable e) {
System.out.println("Error: Cannot load log4j configuration file ");
}
props.setProperty("log4j.appender.ERROREMAIL.Subject", emailRecipients);
LogManager.resetConfiguration();
PropertyConfigurator.configure(props);
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
6463 次 |
最近记录: |