001    package biz.wedoit4u;
002    
003    import java.io.FileOutputStream;
004    import java.io.PrintStream;
005    import java.util.Date;
006    import java.util.Properties;
007    
008    /**
009     * Utility class that is used to <code>log errors/warnings/information
010     * </code> to output files or to a back end database.
011     *
012     * <p>Copyright 2003, Rakesh Vidyadharan and wedoit4u.biz</p>
013     *
014     * @author Rakesh Vidyadharan 10<sup><small>th</small></sup> September 2003
015     *
016     * @version $Id: Logger.java,v 1.4 2004/05/26 11:42:30 rakesh Exp $
017     */
018    public class Logger extends Object
019    {
020      /**
021       * The property that is used to denote the error log.
022       */
023      public static final String ERROR_LOG_PROPERTY = "errorLog";
024    
025      /**
026       * The property that is used to denote the out log.
027       */
028      public static final String OUT_LOG_PROPERTY = "outLog";
029    
030      /**
031       * The <code>PrintStream</code> to which error messages are to be
032       * written.
033       */
034      private static PrintStream errorStream = System.err;
035    
036      /**
037       * The <code>PrintStream</code> to which the messages are to be
038       * written.
039       */
040      private static PrintStream outStream = System.out;
041    
042      /**
043       * Static initialiser for the {@link #errorStream} and {@link
044       * #outStream} <code>PrintStreams</code>.  Look for the environment
045       * entries for <code>errorLog</code> and <code>outLog</code>, and
046       * if not found default to <code>System.err</code> and
047       * <code>System.out</code> respectively.
048      static
049      {
050        // Initialise the errorStream
051        try
052        {
053          Properties properties = System.getProperties();
054    
055          String file = properties.getProperty( Logger.ERROR_LOG_PROPERTY );
056          if ( file != null )
057          {
058            errorStream = new PrintStream( new FileOutputStream( file ) );
059          }
060        }
061        catch ( Exception ex )
062        {
063          errorStream = System.err;
064        }
065    
066        // Initialise the outStream
067        try
068        {
069          Properties properties = System.getProperties();
070    
071          String file = properties.getProperty( Logger.OUT_LOG_PROPERTY );
072          if ( file != null )
073          {
074            outStream = new PrintStream( new FileOutputStream( file ) );
075          }
076        }
077        catch ( Exception ex )
078        {
079          outStream = System.out;
080        }
081      }
082       */
083    
084      /**
085       * Log the specified error message to the {@link #errorStream}.
086       *
087       * @param message - The error message to log.
088       */
089      public static final void error( String message )
090      {
091        Date date = new Date();
092        errorStream.println( date.toString() + "  " + message );
093      }
094    
095      /**
096       * Log the specified error message to the {@link #errorStream}.
097       * Print the <code>StackTraceElement</code> associated with the
098       * specified instance of <code>Throwable</code> as well.
099       *
100       * @param message - The error message to log
101       * @param throwable - The instance of Throwable whose
102       *   StackTrace is to be logged.
103       */
104      public static final void error( String message, Throwable throwable )
105      {
106        Date date = new Date();
107        errorStream.println( date.toString() + "  " + message );
108        errorStream.println( getStackTrace( throwable ) );
109      }
110    
111      /**
112       * Log the specified message to the {@link #outStream}.
113       */
114      public static final void message( String message )
115      {
116        Date date = new Date();
117        outStream.println( date.toString() + "  " + message );
118      }
119    
120      /**
121       * Utility method that prints out the entire <code>stack trace</code>
122       * for the specified instance of <code>Throwable</code>.  This method
123       * just uses the <code>StackTraceElement</code> class methods
124       * to create the <code>String</code> that represents the entire
125       * <code>stack trace</code>.
126       *
127       * @param throwable - The instance of <code>Throwable</code>
128       *   whose entire <code>stack trace</code> is to be retrieved.
129       */
130      public static final String getStackTrace( Throwable throwable )
131      {
132        StringBuffer buffer = new StringBuffer( 1024 );
133    
134        buffer.append( throwable.toString() );
135        buffer.append( "\n" );
136        buffer.append( printTrace( throwable ) );
137    
138        // Loop through the causes if they exist
139        Throwable throwable1 = throwable.getCause();
140        while ( throwable1 != null )
141        {
142          buffer.append( "Caused by: " );
143          buffer.append( throwable1.toString() );
144          buffer.append( "\n" );
145          buffer.append( printTrace( throwable1 ) );
146          throwable1 = throwable1.getCause();
147        }
148    
149        return buffer.toString();
150      }
151    
152      /**
153       * Return the <code>stack trace</code> for the specified instance
154       * of <code>Throwable</code>.
155       *
156       * @param throwable - The instance of <code>Throwable</code>
157       *   whose <code>stack trace</code> is to be retrieved.
158       */
159      private static final String printTrace( Throwable throwable )
160      {
161        StringBuffer buffer = new StringBuffer( 256 );
162        StackTraceElement [] ste = throwable.getStackTrace();
163    
164        for ( int i = 0; i < ste.length; ++i )
165        {
166          buffer.append( "\tat " );
167          buffer.append( ste[i].getClassName() );
168          buffer.append( "." );
169          buffer.append( ste[i].getMethodName() );
170          buffer.append( "(" );
171          buffer.append( ste[i].getFileName() );
172          buffer.append( ":" );
173          buffer.append( ste[i].getLineNumber() );
174          buffer.append( ")" );
175          buffer.append( "\n" );
176        }
177    
178        return buffer.toString();
179      }
180    
181      /**
182       * Utility method that prints out the entire <code>stack trace</code>
183       * for the specified instance of <code>Throwable</code> in HTML
184       * .  This method just uses the <code>StackTraceElement</code> class 
185       * methods to create the <code>HTML String</code> that represents the 
186       * entire <code>stack trace</code>.
187       *
188       * @param throwable - The instance of <code>Throwable</code>
189       *   whose entire <code>stack trace</code> is to be retrieved.
190       */
191      public static final String getHTMLStackTrace( Throwable throwable )
192      {
193        StringBuffer buffer = new StringBuffer( 1024 );
194    
195        buffer.append( throwable.toString() );
196        buffer.append( "<br/>" );
197        buffer.append( printTrace( throwable ) );
198    
199        // Loop through the causes if they exist
200        Throwable throwable1 = throwable.getCause();
201        while ( throwable1 != null )
202        {
203          buffer.append( "Caused by: " );
204          buffer.append( throwable1.toString() );
205          buffer.append( "<br/>" );
206          buffer.append( printHTMLTrace( throwable1 ) );
207          throwable1 = throwable1.getCause();
208        }
209    
210        return buffer.toString();
211      }
212    
213      /**
214       * Return the <code>stack trace</code> for the specified instance
215       * of <code>Throwable</code>.
216       *
217       * @param throwable - The instance of <code>Throwable</code>
218       *   whose <code>stack trace</code> is to be retrieved.
219       */
220      private static final String printHTMLTrace( Throwable throwable )
221      {
222        StringBuffer buffer = new StringBuffer( 256 );
223        StackTraceElement [] ste = throwable.getStackTrace();
224    
225        buffer.append( "<ul>" );
226        for ( int i = 0; i < ste.length; ++i )
227        {
228          buffer.append( "<li>at " );
229          buffer.append( ste[i].getClassName() );
230          buffer.append( "." );
231          buffer.append( ste[i].getMethodName() );
232          buffer.append( "(" );
233          buffer.append( ste[i].getFileName() );
234          buffer.append( ":" );
235          buffer.append( ste[i].getLineNumber() );
236          buffer.append( ")" );
237        }
238        buffer.append( "</ul>" );
239    
240        return buffer.toString();
241      }
242    }