KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > quickserver > util > logging > SimpleConsoleFormatter


1 /*
2  * This file is part of the QuickServer library
3  * Copyright (C) 2003-2005 QuickServer.org
4  *
5  * Use, modification, copying and distribution of this software is subject to
6  * the terms and conditions of the GNU Lesser General Public License.
7  * You should have received a copy of the GNU LGP License along with this
8  * library; if not, you can download a copy from <http://www.quickserver.org/>.
9  *
10  * For questions, suggestions, bug-reports, enhancement-requests etc.
11  * visit http://www.quickserver.org
12  *
13  */

14
15 package org.quickserver.util.logging;
16
17 import java.util.logging.*;
18 import java.util.Date JavaDoc;
19 import java.text.SimpleDateFormat JavaDoc;
20 import org.quickserver.util.MyString;
21
22 /**
23  * Formats the LogRecord as "hh:mm:ss,SSS [LEVEL] Class.method() - MESSAGE"
24  * @since 1.3.2
25  */

26 public class SimpleConsoleFormatter extends Formatter {
27     private Date JavaDoc date = new Date JavaDoc();
28     private SimpleDateFormat JavaDoc df = new SimpleDateFormat JavaDoc("hh:mm:ss,SSS");
29     private String JavaDoc lineSeparator = (String JavaDoc) java.security.AccessController.doPrivileged(
30         new sun.security.action.GetPropertyAction("line.separator"));
31
32     public synchronized String JavaDoc format(LogRecord record) {
33         date.setTime(record.getMillis());
34
35         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
36         sb.append(df.format(date));
37         sb.append(" [");
38         sb.append(MyString.alignLeft(record.getLevel().getLocalizedName(), 7));
39         sb.append("] ");
40         if(record.getSourceClassName() != null) {
41             sb.append(record.getSourceClassName());
42         } else {
43             sb.append(record.getLoggerName());
44         }
45         if(record.getSourceMethodName() != null) {
46             sb.append('.');
47             sb.append(record.getSourceMethodName());
48         }
49         sb.append(" - ");
50         sb.append(formatMessage(record));
51             
52         if(record.getThrown() != null) {
53             sb.append(lineSeparator);
54             sb.append("[Exception: ");
55             sb.append(record.getThrown().toString());
56             sb.append(']');
57         }
58         
59         sb.append(lineSeparator);
60         return sb.toString();
61     }
62 }
63
Popular Tags