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.                                                  
>  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                                              
> 
> 
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org
> For additional commands, e-mail: log4j-user-h...@logging.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscr...@logging.apache.org
For additional commands, e-mail: log4j-user-h...@logging.apache.org

Reply via email to