LogMail.java
001 /*
002  * Copyright (C) 2006  exedio GmbH (www.exedio.com)
003  *
004  * This library is free software; you can redistribute it and/or
005  * modify it under the terms of the GNU Lesser General Public
006  * License as published by the Free Software Foundation; either
007  * version 2.1 of the License, or (at your option) any later version.
008  *
009  * This library is distributed in the hope that it will be useful,
010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
012  * Lesser General Public License for more details.
013  *
014  * You should have received a copy of the GNU Lesser General Public
015  * License along with this library; if not, write to the Free Software
016  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
017  */
018 package com.exedio.pop3serv.log4j;
019 
020 import com.exedio.pop3serv.TextMail;
021 import java.io.UnsupportedEncodingException;
022 import java.util.Date;
023 import java.util.regex.Pattern;
024 import org.apache.log4j.Level;
025 import org.apache.log4j.spi.LocationInfo;
026 import org.apache.log4j.spi.LoggingEvent;
027 import org.apache.log4j.spi.ThrowableInformation;
028 
029 /**
030  * This subclass of {@link TextMail} creates a readable text body from a {@link LoggingEvent} by appending logger, level, message and throwable (if exists).
031  * The priority is set using the log event level, the logger is used as the sender and the first line of the message is used as the subject.
032  @author ebert
033  */
034 public class LogMail extends TextMail
035 {
036   private final LogMailDropManager theMailDropManager;
037 
038   public LogMail(LogMailDropManager mailAppender, String appenderName, LoggingEvent eventthrows UnsupportedEncodingException
039   {
040     super(Integer.toHexString(event.hashCode()), event.getLoggerName(), appenderName, new Date(), firstLine(event.getMessage().toString()), createBody(event), getPriority(event));
041     theMailDropManager = mailAppender;
042   }
043   
044   public void remove()
045   {
046     theMailDropManager.remove(this);
047   }
048 
049   private static String createBody(LoggingEvent event)
050   {
051     StringBuffer buffer = new StringBuffer();
052     buffer.append("Logger: ").append(event.getLoggerName()).append((char)0x0D).append((char)0x0A);
053     buffer.append("Level: ").append(event.getLevel().toString()).append((char)0x0D).append((char)0x0A);
054     buffer.append("Message: ").append(event.getMessage().toString()).append((char)0x0D).append((char)0x0A);
055     buffer.append((char)0x0D).append((char)0x0A);
056     LocationInfo location = event.getLocationInformation();
057     buffer.append("Location: ").append(location.getClassName()).append(".").append(location.getMethodName());
058     if (location.getLineNumber() == null)
059     {
060       buffer.append(" (source line unknown)");
061     }
062     else
063     {
064       buffer.append(" at line ").append(location.getLineNumber());
065     }
066     ThrowableInformation throwable = event.getThrowableInformation();
067     if (throwable != null)
068     {
069       buffer.append((char)0x0D).append((char)0x0A);
070       buffer.append((char)0x0D).append((char)0x0A);
071       buffer.append("Throwable: ");
072       String[] stackTrace = throwable.getThrowableStrRep();
073       for (int i=0; i<stackTrace.length; i++)
074       {
075         buffer.append(stackTrace[i]);
076         if (i<stackTrace.length-1)
077         {
078           buffer.append((char)0x0D).append((char)0x0A);
079         }
080       }
081     }
082     return buffer.toString();
083   }
084 
085   private static int getPriority(LoggingEvent event)
086   {
087     switch(event.getLevel().toInt())
088     {
089       case Level.FATAL_INT:
090       case Level.ERROR_INT: return 1;
091       case Level.WARN_INT: return 2;
092       case Level.INFO_INT: return 3;
093       case Level.TRACE_INT: return 4;
094       defaultreturn 5;
095     }
096   }
097   
098   private static String firstLine(String text)
099   {
100     return Pattern.compile("$", Pattern.MULTILINE).split(text, 2)[0];
101   }
102 }