1 | |
2 | /* |
3 | Copyright (c) 2002, Dean Hiller |
4 | All rights reserved. |
5 | |
6 | ***************************************************************** |
7 | IF YOU MAKE CHANGES TO THIS CODE AND DO NOT POST THEM, YOU |
8 | WILL BE IN VIOLATION OF THE LICENSE I HAVE GIVEN YOU. Contact |
9 | me at deanhiller@users.sourceforge.net if you need a different |
10 | license. |
11 | ***************************************************************** |
12 | |
13 | This program is free software; you can redistribute it and/or |
14 | modify it under the terms of the GNU General Public License |
15 | as published by the Free Software Foundation; either version 2 |
16 | of the License, or (at your option) any later version. |
17 | |
18 | This program is distributed in the hope that it will be useful, |
19 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
21 | GNU General Public License for more details. |
22 | |
23 | You should have received a copy of the GNU General Public License |
24 | along with this program; if not, write to the Free Software |
25 | Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
26 | */ |
27 | package biz.xsoftware.buildtemplate; |
28 | |
29 | import java.io.PrintWriter; |
30 | import java.io.StringWriter; |
31 | import java.text.MessageFormat; |
32 | import java.util.Date; |
33 | import java.util.logging.Formatter; |
34 | import java.util.logging.LogRecord; |
35 | |
36 | /** |
37 | * FILL IN JAVADOC HERE |
38 | */ |
39 | /* <code></code> should be put around any references to a class, interface, or method |
40 | * {@link reference alternate-text} An inline reference used in your description of a |
41 | * class or description of a method. |
42 | * <p> a paragraph break |
43 | * |
44 | * @author $Author: deanhiller $ |
45 | * @version $Revision: 1.1 $ $Date: 2004/09/23 04:08:16 $ |
46 | * @since $ProductVersion$ |
47 | * @deprecated |
48 | * @see reference alternative-text |
49 | * A reference is any of the following... |
50 | * package packageName |
51 | * Class packageName.ClassName |
52 | * InnerClass packageName.ClassName.InnerClassName |
53 | * Method in other class packageName.ClassName#methodName(type1, type2,...) |
54 | * Field in other class packageName.ClassName#fieldName |
55 | * Method in this class #methodName(type1, type2, ...) |
56 | * Field in this class #fieldName |
57 | * where each type is |
58 | * type1 packageName.ClassName |
59 | */ |
60 | public class LogFormatter extends Formatter { |
61 | |
62 | private static Date dat = new Date(); |
63 | |
64 | private final static String format = "{0,date} {0,time}"; |
65 | private MessageFormat formatter; |
66 | |
67 | private Object args[] = new Object[1]; |
68 | |
69 | // Line separator string. This is the value of the line.separator |
70 | // property at the moment that the MyFormatter was created. |
71 | private String lineSeparator = (String) java.security.AccessController.doPrivileged( |
72 | new sun.security.action.GetPropertyAction("line.separator")); |
73 | |
74 | public LogFormatter() |
75 | { |
76 | } |
77 | /** |
78 | * Format the given LogRecord. |
79 | * @param record the log record to be formatted. |
80 | * @return a formatted log record |
81 | */ |
82 | public synchronized String format(LogRecord record) |
83 | { |
84 | //Comments starting with FORMAT shows you were we are in the steps |
85 | //for clarity. |
86 | // Minimize memory allocations here. |
87 | dat.setTime(record.getMillis()); |
88 | args[0] = dat; |
89 | |
90 | StringBuffer sb = new StringBuffer(); |
91 | StringBuffer text = new StringBuffer(); |
92 | if (formatter == null) |
93 | { |
94 | formatter = new MessageFormat(format); |
95 | } |
96 | formatter.format(args, text, null); |
97 | // sb.append(text); //FORMAT: Mar 31, 2002 5:06:54 PM |
98 | // sb.append(lineSeparator); //FORMAT: Mar 31, 2002 5:06:54 PM\n |
99 | |
100 | String level = record.getLevel().getLocalizedName(); |
101 | sb.append(level); //FORMAT: Mar 31, 2002 5:06:54 PM\nFINE: |
102 | for(int i = level.length(); i < 7; i++) |
103 | sb.append(" "); //FORMAT: Mar 31, 2002 5:06:54 PM\nFINE^^^ |
104 | sb.append(": "); //FORMAT: Mar 31, 2002 5:06:54 PM\nFINE^^^: |
105 | |
106 | |
107 | //These next few lines should be commented out if you don't want them. |
108 | //this lines the logs up according to how they are called indenting them |
109 | //for you. It uses tabs and to view this log, you should set your tab |
110 | //space to '1' |
111 | ////////////////////////////////////////////////////////////// |
112 | // Begin indenting according to stack |
113 | ////////////////////////////////////////////////////////////// |
114 | // Throwable t = new Throwable(); |
115 | // t.fillInStackTrace(); |
116 | // StackTraceElement[] frames = t.getStackTrace(); |
117 | // |
118 | // for(int i = 0; i < frames.length; i++) |
119 | // sb.append("\t"); |
120 | ////////////////////////////////////////////////////////////// |
121 | // End indenting according to stack |
122 | ////////////////////////////////////////////////////////////// |
123 | |
124 | |
125 | // String className = null; |
126 | // if(record.getSourceClassName() != null) |
127 | // className = record.getSourceClassName(); |
128 | // else |
129 | // className = record.getLoggerName(); |
130 | // int index = className.lastIndexOf("."); |
131 | // int len = className.length() - (index+1); |
132 | // sb.append( className.substring(index+1, className.length()) ); |
133 | //above FORMAT: Mar 31, 2002 5:06:54 PM\nFINE^^^:ClassName |
134 | // sb.append("."); |
135 | //above FORMAT: Mar 31, 2002 5:06:54 PM\nFINE^^^:ClassName. |
136 | // String method; |
137 | // if (record.getSourceMethodName() != null) |
138 | // method = record.getSourceMethodName(); |
139 | // else |
140 | // method = "methodNotFound"; |
141 | // sb.append(method); |
142 | //above FORMAT: Mar 31, 2002 5:06:54 PM\nFINE^^^:ClassName.methodName |
143 | // for(int i = len+method.length(); i < 40; i++) |
144 | // sb.append(" "); |
145 | // sb.append(": "); |
146 | //above FORMAT: Mar 31, 2002 5:06:54 PM\nFINE^^^:ClassName.methodName^^^:^ |
147 | |
148 | String message = formatMessage(record); |
149 | sb.append(message); |
150 | sb.append(lineSeparator); |
151 | //above FORMAT: Mar 31, 2002 5:06:54 PM\nFINE:^^^ClassName.methodName(): message |
152 | if (record.getThrown() != null) |
153 | { |
154 | try |
155 | { |
156 | StringWriter sw = new StringWriter(); |
157 | PrintWriter pw = new PrintWriter(sw); |
158 | record.getThrown().printStackTrace(pw); |
159 | pw.close(); |
160 | sb.append(sw.toString()); |
161 | //above FORMAT: Mar 31, 2002 5:06:54 PM\nFINE:^^^ClassName.methodName(): message\n |
162 | // StackTrace is here |
163 | } |
164 | catch (Exception ex) |
165 | { |
166 | } |
167 | } |
168 | return sb.toString(); |
169 | } |
170 | } |