logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From utpal <shasane_ut...@extenprise.net>
Subject Re: Query for SMTP Appender
Date Thu, 07 Jul 2005 13:11:11 GMT
Thanks. I will look into that.

On Thu, 2005-07-07 at 17:54, Jan Pernica wrote:

> Files are attached. Just change the way of getting SMTP.   
> 
> And log4j.properties
> 
> log4j.appender.A3=org.pernica.util.Log4jMailAppender
> log4j.appender.A3.from=automat@egap.cz
> log4j.appender.A3.to=jan.pernik@volny.cz
> log4j.appender.A3.subject=Zpráva serveru o FAXER
> log4j.appender.A3.location=http://hefaistos:8080/faxer
> log4j.appender.A3.layout=org.apache.log4j.PatternLayout
> log4j.appender.A3.threshold=ERROR
> 
>                              
> # Print the date in ISO 8601 format
> log4j.appender.A1.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
> log4j.appender.A2.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
> log4j.appender.A3.layout.ConversionPattern=%d [%t] %-5p %c - %m%n
> 
>  
> Regards
> 
> Jan
> 
> utpal wrote:
> 
> > I m not getting you... can you give me an example ?
> > 
> > Thanks,
> > Utpal
> > 
> > On Thu, 2005-07-07 at 17:30, Jan Pernica wrote:
> > 
> >   
> > 
> > > we have developed such an appender :-) I will change our 
> > > log4j.properties to appache one :-)
> > > 
> > > 
> > > utpal wrote:
> > > 
> > >     
> > > 
> > > > Hi
> > > > I am using web based application. I am using log4j.properties file as
:
> > > > 
> > > > log4j.rootCategory=debug,R,EMAIL
> > > >                                                                      
                                                                   
> > > > log4j.appender.EMAIL=org.apache.log4j.net.CSMTPAppender
> > > > log4j.appender.EMAIL.BufferSize=1000
> > > > log4j.appender.EMAIL.SMTPHost=dns.extenprise.net
> > > > log4j.appender.EMAIL.From=shasane_utpal@extenprise.net
> > > > log4j.appender.EMAIL.Subject=TrackException : %s
> > > > log4j.appender.EMAIL.To=shasane_utpal@extenprise.net
> > > > log4j.appender.EMAIL.layout=org.apache.log4j.PatternLayout
> > > > log4j.appender.EMAIL.layout.ConversionPattern=%d [%c] %-5p %m%n
> > > >                                                                      
                                                                                     
> > > > log4j.appender.R=org.apache.log4j.FileAppender
> > > > log4j.appender.R.File=/usr/fileRepository/LogFiles/cstreamer444.log
> > > > log4j.appender.R.layout=org.apache.log4j.PatternLayout
> > > > log4j.appender.R.layout.ConversionPattern=[%d] [%c] [%-5p] - %m%n
> > > > 
> > > > Now whenever exception in any JSP or Java class is thrown I got email
> > > > which include 
> > > > these bunch of exceptions form JSPs and Java classes.
> > > > 
> > > > Can I set SMTP Appender to get each exception seperately from mail? or
> > > > Can I set it to get Exceptions
> > > >       
> > > 
> > > 
> > > 
> > > >from single JSP or Single Java class in a single mail?
> > >     
> > > 
> > > > This I need bcoz I am setting dynamic subject each time which append
> > > > class name (in which exception is generated) to subject.
> > > > 
> > > > Thanks,
> > > > Utpal
> > > > 
> > > >  
> > > > 
> > > > ------------------------------------------------------------------------
> > > > 
> > > > Příchozí zpráva neobsahuje viry.
> > > > Zkontrolováno Antivirovým systémem AVG.
> > > > Verze: 7.0.323 / Virová báze: 267.8.10/43 - datum vydání: 6.7.2005
> > > >  
> > > > 
> > > >       
> > 
> > 
> > 
> >   
> > 
> > ____________________________________________________________________
> > Příchozí zpráva neobsahuje viry.
> > Zkontrolováno Antivirovým systémem AVG.
> > Verze: 7.0.323 / Virová báze: 267.8.10/43 - datum vydání: 6.7.2005
> >   
> 
> 
> 
> 
> ______________________________________________________________________
> 
> 
> package org.pernica.util;
> 
> import javax.mail.*;
> import javax.mail.internet.*;
> import java.util.*;
> import javax.activation.*;
> import java.io.*;
> 
> 
> /**
>  * Trida po posilani emailu
>  *
>  * @author  Jakub Dadak <dadman@brainsys.cz>
>  * @version 0.9, 26.5.2001
>  */
> public class Mail {
>     protected static org.apache.log4j.Logger log4j = org.apache.log4j.Logger.getLogger(Mail.class);
>     
>     /** Metoda pro poslani jednoho mailu. Haze exception s pricinou pripadne chyby  */
>     public static void sendMail(String smtpServer, String from, String to, String subjectIn,
String message) {
>         sendMail(smtpServer, from, to, subjectIn, message, "text/html");
>     } //end of sendMail
>     
>     /** Metoda pro poslani jednoho mailu. Haze exception s pricinou pripadne chyby, v
parametru se zada take format mailu napr. text/html nebo text/plain */
>     public static void sendMail(String smtpServer, String from, String to, String subjectIn,
String message, String mailFormat) {
> 	sendMail(smtpServer, from, to, null, null, subjectIn, message, mailFormat);
>     }
>     public static void sendMail(String smtpServer, String from, String to, String cc,
String bc, String subjectIn, String message, String mailFormat) {
>         // create some properties and get the default Session
>         Properties props = new Properties();
>         props.put("mail.smtp.host", smtpServer);
>         
>         //    Session session = Session.getDefaultInstance(props, null);
>         Session session = Session.getInstance(props, null);
>         session.setDebug(false);
>         
>         try {
>             // create a message
>             MimeMessage msg = new MimeMessage(session);
>             msg.setFrom(new InternetAddress(from));
>             InternetAddress[] address;
>             address = InternetAddress.parse(to, false);
>             msg.setRecipients(Message.RecipientType.TO, address);
> 	    if (cc != null) {
>                 address = InternetAddress.parse(cc, false);
>                 msg.setRecipients(Message.RecipientType.CC, address);
> 	    }
> 	    if (bc != null) {
>                 address = InternetAddress.parse(bc, false);
>                 msg.setRecipients(Message.RecipientType.BCC, address);
> 	    }
>             msg.setSubject(subjectIn, "ISO-8859-2");
>             msg.setSentDate(new java.util.Date());
>             // If the desired charset is known, you can use
>             // setText(text, charset)
>             msg.setContent(message, mailFormat + ";charset=\"iso-8859-2\"");
>             
>             Transport.send(msg);
>         } catch (MessagingException mex) {
>             StringBuffer sb = new StringBuffer();
>             System.out.println("Exception: " + mex.getMessage());
>             mex.printStackTrace();
>             
>             Exception ex = mex;
>             do {
>                 if (ex instanceof SendFailedException) {
>                     SendFailedException sfex = (SendFailedException)ex;
>                     Address[] invalid = sfex.getInvalidAddresses();
>                     if (invalid != null) {
>                         sb.append("Chybn adresy: ");
>                         if (invalid != null)  {
>                             for (int i = 0; i < invalid.length; i++) sb.append(invalid[i]+"
");
>                         }
>                     }
>                     Address[] validUnsent = sfex.getValidUnsentAddresses();
>                     if (validUnsent != null) {
>                         sb.append("Neodeslno: ");
>                         if (validUnsent != null) {
>                             for (int i = 0; i < validUnsent.length; i++)
>                                 sb.append(validUnsent[i]+" ");
>                         }
>                     }
>                     Address[] validSent = sfex.getValidSentAddresses();
>                     if (validSent != null)  {
>                         sb.append("Odeslno: ");
>                         if (validSent != null)  {
>                             for (int i = 0; i < validSent.length; i++) sb.append(validSent[i]+"
");
>                         }
>                     }
>                 }
>             }
>             while ((ex = ((MessagingException)ex).getNextException()) != null);
>             // log4j.error("MAIL NOT SENT: "+sb, mex);
>             throw new MailException(sb.toString());
>         } // End of catch
>     } //end of sendMail
>     
>     /** Method for sending e-mail with atachments. Atachments are as list of MimeBodyPart
*/
>     public static void sendMail(String smtpServer, String from, String to, String subject,
ArrayList mimeBodyParts) {
> 	sendMail(smtpServer, from, to, null, null, subject, mimeBodyParts);
>     }
>     /** Method for sending e-mail with atachments. Atachments are as list of MimeBodyPart
*/
>     public static void sendMail(String smtpServer, String from, String to, String cc,
String bc, String subject, ArrayList mimeBodyParts) {
>         
>         boolean debug = false; // only for tests
>         
>         // create some properties and get the default Session
>         Properties props = new Properties();
>         props.put("mail.smtp.host", smtpServer);
>         
>         Session session = Session.getDefaultInstance(props, null);
>         session.setDebug(debug);
>         
>         try {
>             // create a message
>             MimeMessage msg = new MimeMessage(session);
>             msg.setFrom(new InternetAddress(from));
>             InternetAddress[] address = InternetAddress.parse(to, false);
>             msg.setRecipients(Message.RecipientType.TO, address);
> 	    if (cc != null) {
>                 address = InternetAddress.parse(cc, false);
>                 msg.setRecipients(Message.RecipientType.CC, address);
> 	    }
> 	    if (bc != null) {
>                 address = InternetAddress.parse(bc, false);
>                 msg.setRecipients(Message.RecipientType.BCC, address);
> 	    }
>             msg.setSubject(subject,"iso-8859-2");
>             msg.setSentDate(new Date());
>             
>             Multipart mp = new MimeMultipart();
>             for (int i=0; i<mimeBodyParts.size(); i++) {
>                 mp.addBodyPart((MimeBodyPart)mimeBodyParts.get(i));
>             }
>             
>             // add the Multipart to the message
>             msg.setContent(mp);
>             
>             // send the message
>             Transport.send(msg);
>         } catch (MessagingException mex) {
>             StringBuffer sb = new StringBuffer();
>             System.out.println("Exception: " + mex.getMessage());
>             mex.printStackTrace();
>             
>             Exception ex = mex;
>             do {
>                 if (ex instanceof SendFailedException) {
>                     SendFailedException sfex = (SendFailedException)ex;
>                     Address[] invalid = sfex.getInvalidAddresses();
>                     if (invalid != null) {
>                         sb.append("Chybn adresy: ");
>                         if (invalid != null)  {
>                             for (int i = 0; i < invalid.length; i++) sb.append(invalid[i]+"
");
>                         }
>                     }
>                     Address[] validUnsent = sfex.getValidUnsentAddresses();
>                     if (validUnsent != null) {
>                         sb.append("Nebylo dorueno: ");
>                         if (validUnsent != null) {
>                             for (int i = 0; i < validUnsent.length; i++)
>                                 sb.append(validUnsent[i]+" ");
>                         }
>                     }
>                     Address[] validSent = sfex.getValidSentAddresses();
>                     if (validSent != null)  {
>                         sb.append("Bylo poslno: ");
>                         if (validSent != null)  {
>                             for (int i = 0; i < validSent.length; i++) sb.append(validSent[i]+"
");
>                         }
>                     }
>                 }
>             }
>             while ((ex = ((MessagingException)ex).getNextException()) != null);
>             // log4j.error("MAIL NOT SENT: "+sb, mex);
>             throw new MailException(sb.toString());
>         } // End of catch
>     }
>     
>     public static class MailException extends RuntimeException {
>         public MailException( String message ) {
>             super(message);
>         }
>     }
>     
> } // end of class Mailer
> 
> 
> ______________________________________________________________________
> 
> 
> /*
>  * Log4jMailAppender.java
>  *
>  * Created on 11. duben 2005, 9:44
>  */
> 
> package org.pernica.util;
> import org.apache.log4j.*;
> import org.apache.log4j.spi.*;
> import org.apache.log4j.helpers.*;
> 
> /**
>  * Log4j appender class used to send e-mail messages
>  * of events recieved. This appender should always be added
>  * to the log4j provided AsynchronousAppender becuase of the
>  * overhead involved in sending e-mail messages.
>  *
>  * Sample log4j xml configuration: <code>
>  *     <appender name="MAIL" = class="com.frontiers.server.mail.MailAppender">
>  *               <param name="Threshold" value="ERROR" />
>  *
>  *		<param name="To" value="jkuhnert@ekosystems.com" />
>  *              <param name="Subject" value="Error from = CaseServer!!" />
>  *		<param name="From" value="caseserver@inovafairfax.com" />
>  *		<param name="Location" value="CaseServer at Inova Fairfax. I.P. = address: 10.1.1.44"
/>
>  *
>  *               <layout class="org.apache.log4j.PatternLayout">
>  *                   <param name="ConversionPattern" = value="%d{ISO8601} %-5p %c{1}
- %m%n"/>
>  *               </layout>
>  *       </appender> </code>
>  *
>  * @author Jan Pernica, Copyright 2005 Pernica IT Solutions.
>  */
> public class Log4jMailAppender extends AppenderSkeleton {
>     
>     /** From */
>     protected String _from;
>     /** Message recipients. Comma separated list. */
>     protected String _to;
>     /** E-mail subject to use */
>     protected String _subject;
>     /** Location of originating service */
>     protected String _location;
>     
>     /** Default constructor. Does nothing. */
>     public Log4jMailAppender() {
>     }
>     
>     /**
>      * Instantiates with layout to use for formatting
>      * messages.
>      *
>      * @param Layout - Layout to use
>      */
>     public Log4jMailAppender(Layout layout) {
>         this.layout = layout;
>     }
>     
>     public void setFrom(String from) { _from = from; }
>     public String getFrom() { return _from; }
>     
>     public void setTo(String to) { _to = to; }
>     public String getTo() { return _to; }
>     
>     public void setSubject(String subject) { _subject = subject; }
>     public String getSubject() { return _subject; }
>     
>     public void setLocation(String loc) { _location = loc; }
>     public String getLocation() { return _location; }
>     
>     /**
>      * Implementation of message appends. These
>      * mesages will be e-mail to recipients.
>      *
>      * @param event
>      */
>     public void append(LoggingEvent event) {
>         try {
>             String smtp = (String) org.pernica.util.JNDI.get("mail/smtp server");
>             String from = (String) org.pernica.util.JNDI.get("mail/sender");
>             if (smtp != null && from != null) {
>                 //If we have a throwable string, capture and print it
>                 if (event.getThrowableStrRep() != null && event.getThrowableStrRep().length
> 0) {
>                     StringBuffer str = new StringBuffer();
>                     String[] s = event.getThrowableStrRep();
>                     
>                     for (int i=0; i < s.length; i++) {
>                         str.append(s[i]).append(Layout.LINE_SEP);
>                     }
>                     
>                     // System.out.println("sending mail to "+_to+": "+this.layout.format(event));
>                     org.pernica.util.Mail.sendMail(smtp,from,
>                             _to, _subject, "Location: "+_location+"\n"+this.layout.format(event)+"\n"+
str.toString());
>                 } else {
>                     //Regular generic message
>                     // System.out.println("sending mail to "+_to+": "+this.layout.format(event));
>                     org.pernica.util.Mail.sendMail(smtp,from,
>                             _to, _subject, "\nLocation: "+_location+"\n"+this.layout.format(event));
>                 }
>             }
>         } catch (Throwable t) {
>             System.out.println("MailAppender: Caught exception:" + t.getMessage());
>             t.printStackTrace();
>         }
>     }
>     
>     public void close(){}
>     
>     public boolean requiresLayout() {
>         return true;
>     }
>     
>     
> }
> 
> 
> ______________________________________________________________________
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message