KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > logging > DebugFormatter


1 //The contents of this file are subject to the Mozilla Public License Version 1.1
2
//(the "License"); you may not use this file except in compliance with the
3
//License. You may obtain a copy of the License at http://www.mozilla.org/MPL/
4
//
5
//Software distributed under the License is distributed on an "AS IS" basis,
6
//WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
7
//for the specific language governing rights and
8
//limitations under the License.
9
//
10
//The Original Code is "The Columba Project"
11
//
12
//The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13
//Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
//
15
//All Rights Reserved.
16
package org.columba.core.logging;
17
18 import java.text.SimpleDateFormat JavaDoc;
19 import java.util.Date JavaDoc;
20 import java.util.logging.Formatter JavaDoc;
21 import java.util.logging.LogRecord JavaDoc;
22
23 /**
24  * DebugFormatter is a LogFormatter for the Java.util.logging logging framework.
25  * Using this class outputs a oneline log that looks like this:
26  * "01/12/2002 22:00 [Classname.Methoname()] MESSAGE"
27  *
28  * @author redsolo
29  */

30 public class DebugFormatter extends Formatter JavaDoc {
31
32     private static final SimpleDateFormat JavaDoc DATE_FORMATTER = new SimpleDateFormat JavaDoc("dd/MM/yyyy kk:mm:ss");
33
34     /**
35      * Format the given log record and return the formatted string.
36      *
37      * @param record the log record to be formatted
38      * @return the formatted log record
39      */

40     public String JavaDoc format(LogRecord JavaDoc record) {
41         StringBuffer JavaDoc string = new StringBuffer JavaDoc();
42
43         String JavaDoc className = record.getSourceClassName();
44         if (className != null) {
45             int lastPos = className.lastIndexOf('.');
46             if (lastPos != -1) {
47                 className = className.substring(lastPos + 1);
48             }
49         } else {
50             className = "unknown";
51         }
52
53         string.append(DATE_FORMATTER.format(new Date JavaDoc(record.getMillis())));
54         string.append(" [");
55         string.append(className);
56         string.append(".");
57         string.append(record.getSourceMethodName());
58
59         string.append("()] ");
60         string.append(record.getMessage());
61         string.append("\n");
62
63         return string.toString();
64     }
65 }
66
Popular Tags