KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joseki > logging > java > OneLineFormatter


1 /*
2  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
3  * [See end of file]
4  */

5
6 package org.joseki.logging.java;
7
8 import java.util.Date JavaDoc ;
9 import java.text.* ;
10 import java.io.* ;
11 import java.util.logging.*;
12
13 /** Java logging formatter that record logs on one line.
14  *
15  * @author Andy Seaborne
16  * @version $Id: OneLineFormatter.java,v 1.3 2004/04/30 14:13:14 andy_seaborne Exp $
17  */

18 public class OneLineFormatter extends Formatter
19 {
20     // Derived from java.util.logging.SimpleFormatter
21
// Looks sorta like the Log4j pattern
22
static public int nameColWidth = 25 ;
23     
24     Date JavaDoc date = new Date JavaDoc();
25     
26     // If the format isn't what you expect (e.g. wrong Locale)
27
// please note some Java versions like to default to the wrong Locale.
28
// Under Java 1.4.2_b28 the default, here in the UK, is back to en_US.
29
// Humph.
30

31     //DateFormat df = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM) ;
32
//DateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss.SSSS z") ;
33

34     // Use a "universal" format. Set this else where if you want something different.
35
public DateFormat dateAndTimeFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss") ;
36
37     // Line separator string. This is the value of the line.separator
38
// property at the moment that the SimpleFormatter was created.
39
private String JavaDoc lineSeparator =
40         (String JavaDoc) java.security.AccessController.doPrivileged(
41             new sun.security.action.GetPropertyAction("line.separator"));
42
43     /**
44      * Format the given LogRecord.
45      * @param record the log record to be formatted.
46      * @return a formatted log record
47      */

48     public synchronized String JavaDoc format(LogRecord record)
49     {
50         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
51         // Minimize memory allocations here.
52
date.setTime(record.getMillis());
53         StringBuffer JavaDoc text = new StringBuffer JavaDoc();
54
55         sb.append(dateAndTimeFormat.format(date)) ;
56
57         sb.append(" ") ;
58         String JavaDoc tmp = record.getLevel().getLocalizedName() ;
59         sb.append(tmp);
60         // Pad out.
61
for ( int i = tmp.length() ; i < 6 ; i++ )
62             sb.append(' ') ;
63         
64         sb.append(" : ");
65
66         int len1 = sb.length() ;
67
68         // Get just the classname, no package name (my pref for small/medium systems)
69
// May also be the replacement name (e.g. a servlet name)
70
// when overridding the default logging style.
71

72         if (record.getSourceClassName() != null)
73         {
74             if (!record.getSourceClassName().equals(""))
75             {
76                 sb.append(" ");
77                 String JavaDoc str = record.getSourceClassName();
78                 int i = str.lastIndexOf('.');
79                 if (i >= 0)
80                     sb.append(str.substring(i + 1));
81                 else
82                     sb.append(str) ;
83             }
84         }
85         
86 // if ( record.getSourceMethodName() != null )
87
// {
88
// sb.append(".");
89
// sb.append(record.getSourceMethodName());
90
// }
91

92         // Pad out.
93
for ( int i = sb.length() ; i < len1+nameColWidth ; i++ )
94             sb.append(' ') ;
95         
96         // The standard simple formatter inserts a line separator at this point
97
//sb.append(lineSeparator);
98
sb.append(" :: ") ;
99         
100         if ( record.getLevel().equals(Level.WARNING) ||
101              record.getLevel().equals(Level.SEVERE) )
102         {
103             sb.append(record.getLevel().getLocalizedName()) ;
104             sb.append(" :: ") ;
105         }
106
107
108         String JavaDoc message = formatMessage(record);
109         sb.append(message);
110         
111         sb.append(lineSeparator);
112         if (record.getThrown() != null)
113         {
114             try
115             {
116                 StringWriter sw = new StringWriter();
117                 PrintWriter pw = new PrintWriter(sw);
118                 record.getThrown().printStackTrace(pw);
119                 pw.close();
120                 sb.append(sw.toString());
121             }
122             catch (Exception JavaDoc ex)
123             {
124             }
125         }
126         return sb.toString();
127     }
128 }
129
130 /*
131  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
132  * All rights reserved.
133  *
134  * Redistribution and use in source and binary forms, with or without
135  * modification, are permitted provided that the following conditions
136  * are met:
137  * 1. Redistributions of source code must retain the above copyright
138  * notice, this list of conditions and the following disclaimer.
139  * 2. Redistributions in binary form must reproduce the above copyright
140  * notice, this list of conditions and the following disclaimer in the
141  * documentation and/or other materials provided with the distribution.
142  * 3. The name of the author may not be used to endorse or promote products
143  * derived from this software without specific prior written permission.
144  *
145  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
146  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
147  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
148  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
149  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
150  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
151  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
152  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
153  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
154  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
155  */

156
157
Popular Tags