KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > joseki > logging > log4j > OneLineLayout


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

5
6 package org.joseki.logging.log4j;
7
8 import org.apache.log4j.* ;
9 import org.apache.log4j.spi.*;
10
11 import java.util.* ;
12 import java.text.* ;
13 /**
14  * There are a few things the standard log4j PatternLayout does not do:
15  *
16  * 1/ I prefer log messages as "class.method", and column aligned.
17  * There is no way to align a compound item like "%C{1}.%M"
18  * Could hack the Log4j code to do %30{%C.%M}-like patterns
19  *
20  * 2/ I prefer message to add WARNING/ERROR/FATAL to make them standout
21  * but not INFO. This I find helpful for verbose logging situations.
22  * Could do this by adding a second appender if the first only output
23  * INFO and below level output.
24  * Can be done with filters, except are they possible in a log4j.properties file?
25  *
26  * Either I put up with the standard output, find out how to modifiy it (and there
27  * are some package local accesses so extending is impossible), or write my own.
28  *
29  * Hence this - a directly done layout.
30  *
31  * @author Andy Seaborne
32  * @version $Id: OneLineLayout.java,v 1.2 2004/04/30 14:13:14 andy_seaborne Exp $
33  */

34 public class OneLineLayout extends Layout
35 {
36     Date date = new Date();
37     public DateFormat dateAndTimeFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss") ;
38
39     // Line separator string. This is the value of the line.separator
40
// property at the moment that the SimpleFormatter was created.
41
private String JavaDoc lineSeparator =
42         (String JavaDoc) java.security.AccessController.doPrivileged(
43             new sun.security.action.GetPropertyAction("line.separator"));
44
45     
46     static public int nameColWidth = 30 ;
47
48     
49     public OneLineLayout()
50     {
51     }
52
53     public String JavaDoc format(LoggingEvent ev)
54     {
55         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
56         // Minimize memory allocations here.
57
date.setTime(System.currentTimeMillis());
58         StringBuffer JavaDoc text = new StringBuffer JavaDoc();
59
60         sb.append(dateAndTimeFormat.format(date)) ;
61         sb.append(" - ") ;
62
63         boolean nameClassMethod = true ;
64       
65         if ( nameClassMethod )
66         {
67             LocationInfo info = ev.getLocationInformation() ;
68             String JavaDoc clsStr = info.getClassName() ;
69             
70             int ind = clsStr.lastIndexOf('.') ;
71             if ( ind > -1 )
72                 clsStr = clsStr.substring(ind+1) ;
73             
74             String JavaDoc methStr = info.getMethodName() ;
75             int len = 0 ;
76             
77             sb.append(clsStr) ; len += clsStr.length() ;
78             sb.append(".") ; len += 1 ;
79             sb.append(methStr) ; len += methStr.length() ;
80             
81             for ( int i = len ; i <= nameColWidth ; i++ )
82                 sb.append(' ') ;
83         }
84         else
85         {
86             
87             String JavaDoc loggerName = ev.getLoggerName() ;
88             sb.append(loggerName) ;
89             for ( int i = loggerName.length() ; i <= 20 ; i++ )
90                 sb.append(' ') ;
91         }
92         
93         sb.append(" :: ") ;
94         if ( ev.getLevel() == Level.WARN )
95             sb.append("WARN: ") ;
96         else if ( ev.getLevel() == Level.ERROR )
97             sb.append("ERROR: ") ;
98         else if ( ev.getLevel() == Level.FATAL )
99             sb.append("FATAL: ") ;
100         sb.append(ev.getMessage().toString()) ;
101         sb.append(lineSeparator) ;
102         return sb.toString() ;
103     }
104
105     public boolean ignoresThrowable()
106     {
107         return true;
108     }
109
110     public void activateOptions()
111     {
112     }
113     
114 }
115
116
117 /*
118  * (c) Copyright 2003, 2004 Hewlett-Packard Development Company, LP
119  * All rights reserved.
120  *
121  * Redistribution and use in source and binary forms, with or without
122  * modification, are permitted provided that the following conditions
123  * are met:
124  * 1. Redistributions of source code must retain the above copyright
125  * notice, this list of conditions and the following disclaimer.
126  * 2. Redistributions in binary form must reproduce the above copyright
127  * notice, this list of conditions and the following disclaimer in the
128  * documentation and/or other materials provided with the distribution.
129  * 3. The name of the author may not be used to endorse or promote products
130  * derived from this software without specific prior written permission.
131  *
132  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
133  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
134  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
135  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
136  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
137  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
138  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
139  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
140  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
141  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
142  */

143
Popular Tags