KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > wings > util > ExtendedFormatter


1 /*
2  * $Id: ExtendedFormatter.java,v 1.3 2004/12/01 07:54:30 hengels Exp $
3  * Copyright 2000,2005 wingS development team.
4  *
5  * This file is part of wingS (http://www.j-wings.org).
6  *
7  * wingS is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU Lesser General Public License
9  * as published by the Free Software Foundation; either version 2.1
10  * of the License, or (at your option) any later version.
11  *
12  * Please see COPYING for the complete licence.
13  */

14 package org.wings.util;
15
16 import java.io.PrintWriter JavaDoc;
17 import java.io.StringWriter JavaDoc;
18 import java.text.DateFormat JavaDoc;
19 import java.text.SimpleDateFormat JavaDoc;
20 import java.util.Date JavaDoc;
21 import java.util.logging.LogRecord JavaDoc;
22
23 public class ExtendedFormatter
24         extends java.util.logging.Formatter JavaDoc {
25     private DateFormat JavaDoc dateFormatter;
26
27     public ExtendedFormatter() {
28         dateFormatter = new SimpleDateFormat JavaDoc("MMM dd, yyyy hh:mm:ss a");
29     }
30
31     public String JavaDoc format(LogRecord JavaDoc record) {
32         StringBuffer JavaDoc msg = new StringBuffer JavaDoc();
33         msg.append(dateFormatter.format(new Date JavaDoc()));
34         msg.append(" ");
35         msg.append(record.getLevel());
36         msg.append(" ");
37         msg.append(record.getSourceClassName());
38         msg.append(" ");
39         msg.append(record.getSourceMethodName());
40         msg.append(": ");
41         msg.append(formatMessage(record));
42         msg.append("\n");
43         if (record.getThrown() != null)
44             msg.append(getBackTrace(record.getThrown()));
45
46         return msg.toString();
47     }
48
49     protected String JavaDoc getBackTrace(Throwable JavaDoc theThrown) {
50         StringWriter JavaDoc sw = new StringWriter JavaDoc();
51         PrintWriter JavaDoc pw = new PrintWriter JavaDoc(sw);
52         theThrown.printStackTrace(pw);
53         pw.close();
54         return sw.getBuffer().toString();
55     }
56 }
57
Popular Tags