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
I am using the above mentioned log4j property file to send email when I do
log.error("Error message");
How do I be able to make it dynamic so that the message subject can change dynamically depending upon the machine name(env name).
eg:
log4j.appender.ERROREMAIL.Subject=messagesubject1, messagesubject2, messagesubject3
I want to use subjects 1,2 and 3 dynamically depending upon machine name.
Any help will be appreciated. Thanks
In the following piece of code I read out the log4j.properties file, set the property log4j.appender.ERROREMAIL.Subject to emailRecipients and reset the log4j configuration. Could be done at the start of the application, you just need to set the emailRecepients string right.
I can think of 2 solutions:
1) Write your own logger that will use SMTPAppender and set the properties programatically.
2) Use the MDC to put a dynamic value in your code. You can get values from the MDC in your log4j.xml with the %X.
ex: log4j.appender.ERROREMAIL.Subject=%X{key}
if we are reading the properties and adding the subject throw java code we will get concurrent request problem.
if first user modified the subject and doing some other operations before sending the mail. at the same time second user added different subject. first user is also get the second user's subject.
You should just need to make use of the hostname variable something like:
Depending on your paritcular configuration and OS you may need to supply this variable to your JVM at statrup using -Dhostname='machinename' or -Dhostname=$HOST
To set a username or over ENV variable