Re: using of system properties in subject of SMTPAppender
You might be interested in the it.openutils.log4j.AlternateSMTPAppender [1]. It allows for dynamic subjects and more. Note that I am not affiliated with this project. I just found it to be useful. [1] http://www.openmindlab.com/lab/tools/openutilslog4j/smtp.html Jake On Mon, 23 May 2011 16:18:20 +0200 Zbynek Vavros zbynek_vav...@cz.ibm.com wrote: 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 maintained by the Regional Court in Brno (Part C, Entry 39922) IBM Global Services Delivery Center
RE: [SCL-3] using of system properties in subject of SMTPAppender
Hi Heri, thanks a lot for this advice but if I am correct I can use %X{CUSTOMER} only in PatternLayout. Then I need to create my own implementation of SMTPAppender and this is what i wanted to avoid. Any chance it can be done without custome implementation? Thx Kind Regards / Mit freundlichen Grüßen / Üdvözlettel / S pozdravem: Zbynek VAVROS (Embedded Development image moved to file: pic16556.gif) Delivery Centre 616 00, Brno Central Europe Technicka 21 Brno SITE Czech Republic (Embedded image moved to file: pic29569.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 maintained by the Regional Court in Brno (Part C, Entry 39922) IBM Global Services Delivery Center Czech Republic, s.r.o. Sídlo: Brno, Technická 2995/21, PSČ 61600 IČ: 26244535 Zapsaná v obchodním rejstříku, vedeném Krajským soudem v Brně oddíl C, vlozka 39922 | | From: | | --| |Bender Heri hben...@ergonomics.ch | --| | | To:| | --| |'Log4J Users List' log4j-user@logging.apache.org | --| | | Date: | | --| |05/24/2011 01:18 PM
RE: [SCL-3] using of system properties in subject of SMTPAppender
Hi, thx for advice but since default SMTPAppender does not allow usage of PatternLayout in subject I needed to implement this by extedning usual SMTPAppender. Now it works just fine :) Kind Regards / Mit freundlichen Grüßen / Üdvözlettel / S pozdravem: Zbynek VAVROS (Embedded Development image moved to file: pic1.gif) Delivery Centre 616 00, Brno Central Europe Technicka 21 Brno SITE Czech Republic (Embedded image moved to file: pic01271.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 maintained by the Regional Court in Brno (Part C, Entry 39922) IBM Global Services Delivery Center Czech Republic, s.r.o. Sídlo: Brno, Technická 2995/21, PSČ 61600 IČ: 26244535 Zapsaná v obchodním rejstříku, vedeném Krajským soudem v Brně oddíl C, vlozka 39922 | | From: | | --| |Bender Heri hben...@ergonomics.ch | --| | | To:| | --| |'Log4J Users List' log4j-user@logging.apache.org | --| | | Date: | | --| |05/24/2011 01:18 PM |
RE: [SCL-3] using of system properties in subject of SMTPAppender
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
Re: using of system properties in subject of SMTPAppender
If it can be in the body of the email, then there are many ways that you could customize the rendering of the message. Since it appears to be a very low frequency occurrence, you could do something like: if(logger.isErrorEnabled()) { string msg = Client + clienName + encountered unexpected exception;. logger.error(msg, e); } If you really want it in the subject, you will likely need to extend SMTPAppender. You could create an object that represents the combination of the server context and the message (or just the context if you don't need the message parameter) logger.error(context, e); or if(logger.isErrorEnabled()) { logger.error(new MyCustomObject(context, msg), e); } The toString method of the first parameter will be used for the message. Your customized SMTPAppender can recognize the message parameter object and extract whatever information you need for the subject line. On May 23, 2011, at 9:18 AM, Zbynek Vavros wrote: 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 maintained by the Regional Court in Brno (Part C, Entry 39922) IBM Global Services Delivery Center Czech Republic, s.r.o.
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 maintained by the Regional Court in Brno (Part C, Entry 39922) IBM Global Services Delivery Center Czech Republic, s.r.o. Sídlo: Brno, Technická 2995/21, PSČ 61600 IČ: 26244535 Zapsaná v obchodním rejstříku, vedeném