View Javadoc

1   /*
2       StatCvs - CVS statistics generation 
3       Copyright (C) 2002  Lukasz Pekacki <lukasz@pekacki.de>
4       http://statcvs.sf.net/
5       
6       This library is free software; you can redistribute it and/or
7       modify it under the terms of the GNU Lesser General Public
8       License as published by the Free Software Foundation; either
9       version 2.1 of the License, or (at your option) any later version.
10  
11      This library is distributed in the hope that it will be useful,
12      but WITHOUT ANY WARRANTY; without even the implied warranty of
13      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14      Lesser General Public License for more details.
15  
16      You should have received a copy of the GNU Lesser General Public
17      License along with this library; if not, write to the Free Software
18      Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19      
20  	$RCSfile: LogFormatter.java,v $ 
21  	Created on $Date: 2008/04/02 11:22:15 $ 
22  */
23  
24  package net.sf.statcvs.util;
25  
26  import java.io.PrintWriter;
27  import java.io.StringWriter;
28  import java.util.logging.Formatter;
29  import java.util.logging.Level;
30  import java.util.logging.LogRecord;
31  
32  /**
33   * Logging formatter for StatCvs
34   * @author Lukasz Pekacki <lukasz@pekacki.de>
35   * @version $Id: LogFormatter.java,v 1.9 2008/04/02 11:22:15 benoitx Exp $
36   */
37  public class LogFormatter extends Formatter {
38  
39      private final String lineSeparator = System.getProperty("line.separator");
40  
41      /**
42       * @see java.util.logging.Formatter#format(LogRecord)
43       */
44      public String format(final LogRecord record) {
45          final StringBuffer sb = new StringBuffer();
46          if (record.getLevel().intValue() < Level.INFO.intValue()) {
47              sb.append(record.getLevel().getLocalizedName());
48              sb.append(" ");
49              if (record.getSourceClassName() != null) {
50                  String className = record.getSourceClassName();
51                  className = className.substring(7);
52                  sb.append(className);
53              } else {
54                  sb.append(record.getLoggerName());
55              }
56              if (record.getSourceMethodName() != null) {
57                  sb.append(" ");
58                  sb.append(record.getSourceMethodName());
59              }
60              sb.append("(): ");
61          }
62          final String message = formatMessage(record);
63          sb.append(message);
64          sb.append(lineSeparator);
65          if (record.getThrown() != null) {
66              try {
67                  final StringWriter sw = new StringWriter();
68                  final PrintWriter pw = new PrintWriter(sw);
69                  record.getThrown().printStackTrace(pw);
70                  pw.close();
71                  sb.append(sw.toString());
72              } catch (final Exception ex) {
73                  System.err.println("Error formatting logmessage! " + ex.toString());
74              }
75          }
76          return sb.toString();
77      }
78  
79  }