KickJava   Java API By Example, From Geeks To Geeks.

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


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.io.* ;
9 import java.util.logging.*;
10
11 /** Java logging formatter that record logs with an abbreviated output,
12  * suitable for servlet logging where timestamps will be
13  * added by the underlying system.
14  *
15  * @author Andy Seaborne
16  * @version $Id: OneLineFormatterCompact.java,v 1.2 2004/04/30 14:13:14 andy_seaborne Exp $
17  */

18
19 public class OneLineFormatterCompact extends Formatter
20 {
21     // Derived from java.util.logging.SimpleFormatter
22
static public int nameColWidth = 30 ;
23
24     // Line separator string. This is the value of the line.separator
25
// property at the moment that the SimpleFormatter was created.
26
private String JavaDoc lineSeparator =
27         (String JavaDoc) java.security.AccessController.doPrivileged(
28             new sun.security.action.GetPropertyAction("line.separator"));
29
30     /**
31      * Format the given LogRecord.
32      * @param record the log record to be formatted.
33      * @return a formatted log record
34      */

35     public synchronized String JavaDoc format(LogRecord record)
36     {
37         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
38         
39         if ( ! record.getLevel().equals(Level.INFO))
40         {
41             sb.append(record.getLevel().getLocalizedName());
42             sb.append(": ");
43         }
44
45         // Get just the classname, no package name (my pref for small/medium systems)
46
// May also be the replacement name (e.g. a servlet name)
47
// when overridding the default logging style.
48

49         if (record.getSourceClassName() != null)
50         {
51             if (!record.getSourceClassName().equals(""))
52             {
53                 String JavaDoc str = record.getSourceClassName();
54                 int i = str.lastIndexOf('.');
55                 if (i >= 0)
56                     sb.append(str.substring(i + 1));
57                 else
58                     sb.append(str) ;
59             }
60         }
61         
62         if ( record.getSourceMethodName() != null )
63         {
64             sb.append(".");
65             sb.append(record.getSourceMethodName());
66         }
67         
68         sb.append(": ") ;
69         
70         if ( record.getLevel().equals(Level.WARNING) ||
71              record.getLevel().equals(Level.SEVERE) )
72         {
73             sb.append(record.getLevel().getLocalizedName()) ;
74             sb.append(" :: ") ;
75         }
76
77
78         String JavaDoc message = formatMessage(record);
79         sb.append(message);
80         
81         // Does not end in a line separator.
82

83         if (record.getThrown() != null)
84         {
85             try
86             {
87                 StringWriter sw = new StringWriter();
88                 PrintWriter pw = new PrintWriter(sw);
89                 record.getThrown().printStackTrace(pw);
90                 pw.close();
91                 sb.append(lineSeparator);
92                 sb.append(sw.toString());
93             }
94             catch (Exception JavaDoc ex)
95             {
96             }
97         }
98         return sb.toString();
99     }
100 }
101
102 /*
103  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
104  * All rights reserved.
105  *
106  * Redistribution and use in source and binary forms, with or without
107  * modification, are permitted provided that the following conditions
108  * are met:
109  * 1. Redistributions of source code must retain the above copyright
110  * notice, this list of conditions and the following disclaimer.
111  * 2. Redistributions in binary form must reproduce the above copyright
112  * notice, this list of conditions and the following disclaimer in the
113  * documentation and/or other materials provided with the distribution.
114  * 3. The name of the author may not be used to endorse or promote products
115  * derived from this software without specific prior written permission.
116  *
117  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
118  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
119  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
120  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
121  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
122  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
123  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
124  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
125  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
126  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
127  */

128
129
Popular Tags