Usually a customer is served within one separate thread, isn't it? If yes, you
can use the MDC (mapped diagnostic context). You put a value into it:
MDC.put(CUSTOMER, SOME NAME) at the begin of the session (probably in a
servlet listener) and reference this value with the layout pattern
%X{CUSTOMER}.
Do not forget to remove the value when session terminates!
Heri
-Original Message-
From: Zbynek Vavros [mailto:zbynek_vav...@cz.ibm.com]
Sent: Monday, May 23, 2011 4:18 PM
To: log4j-user@logging.apache.org
Subject: [SCL-3] using of system properties in subject of SMTPAppender
Hi,
I have a 1 server app and many clients app on different servers.
Obviously I wanted to know from which server the email comes,
so I used
this:
log4j.xml
appender name=email class=org.apache.log4j.net.SMTPAppender
param name=BufferSize value=512 /
param name=SMTPHost value=my_smtp_host /
param name=From value=AutoBot /
param name=To value=my_em...@someserver.com /
param name=Subject value=Agent on [${HOSTNAME}] has
encountered error /
layout class=org.apache.log4j.PatternLayout
param name=ConversionPattern
value=%d{HH:mm:ss} [%t] %p %C
(%L) %x - %m%n /
/layout
filter class=org.apache.log4j.varia.LevelRangeFilter
param name=LevelMin value=error /
param name=LevelMax value=fatal /
/filter
/appender
and since system property has to be setup before configuring
log4j, I use this in my code.
Main class
//create system property to hold server name
System.setProperty(HOSTNAME, InetAddress.getLocalHost
().getCanonicalHostName());
//reconfigure log4j so it picks up HOSTNAME property
DOMConfigurator.configure(getClass().getClassLoader().getResource(
log4j.xml));
Then Im receiving emails with subject like this: Agent on
[127.0.0.1] has encountered error.
So far so good.
And now I have request to include customer name in subject.
I have no problem to include it in stack trace, but since I
cannot set customer name the same way as I did with IP (there
are multiple customers handled by my app so I cannot just use
System.setProperty(CUSTOMER, SOME NAME)).
I though of solution to set this property when exception is
catched and reconfigure log4j after setting proper customer
name into system property.
But I think it is politelly said ugly.
Can some1 suggest a better way maybe?
Thanks, Zbynek
Kind Regards / Mit
freundlichen Grüßen /
Üdvözlettel / S
pozdravem:
Zbynek VAVROS
(Embedded
Development
image moved
to file:
pic20342.gif)
Delivery Centre 616 00, Brno
Central Europe Technicka 21
Brno SITE Czech Republic
(Embedded image moved to
file: pic17964.gif)Phone: 420-53341- x6283
Mobile:
E-mail: zbynek_vav...@cz.ibm.com
IBM Global Services
Delivery Center Czech
Republic, s.r.o.
Registered address:
Brno, Technicka 2995/21,
Zip code: 61600, Company
ID: 26244535
Entered in the
Commercial Register