Mercurial > hg > blitz_condensed
comparison src/com/go/trove/log/LogScribe.java @ 0:3dc0c5604566
Initial checkin of blitz 2.0 fcs - no installer yet.
author | Dan Creswell <dan.creswell@gmail.com> |
---|---|
date | Sat, 21 Mar 2009 11:00:06 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3dc0c5604566 |
---|---|
1 /* ==================================================================== | |
2 * Trove - Copyright (c) 1997-2000 Walt Disney Internet Group | |
3 * ==================================================================== | |
4 * The Tea Software License, Version 1.1 | |
5 * | |
6 * Copyright (c) 2000 Walt Disney Internet Group. All rights reserved. | |
7 * | |
8 * Redistribution and use in source and binary forms, with or without | |
9 * modification, are permitted provided that the following conditions | |
10 * are met: | |
11 * | |
12 * 1. Redistributions of source code must retain the above copyright | |
13 * notice, this list of conditions and the following disclaimer. | |
14 * | |
15 * 2. Redistributions in binary form must reproduce the above copyright | |
16 * notice, this list of conditions and the following disclaimer in | |
17 * the documentation and/or other materials provided with the | |
18 * distribution. | |
19 * | |
20 * 3. The end-user documentation included with the redistribution, | |
21 * if any, must include the following acknowledgment: | |
22 * "This product includes software developed by the | |
23 * Walt Disney Internet Group (http://opensource.go.com/)." | |
24 * Alternately, this acknowledgment may appear in the software itself, | |
25 * if and wherever such third-party acknowledgments normally appear. | |
26 * | |
27 * 4. The names "Tea", "TeaServlet", "Kettle", "Trove" and "BeanDoc" must | |
28 * not be used to endorse or promote products derived from this | |
29 * software without prior written permission. For written | |
30 * permission, please contact opensource@dig.com. | |
31 * | |
32 * 5. Products derived from this software may not be called "Tea", | |
33 * "TeaServlet", "Kettle" or "Trove", nor may "Tea", "TeaServlet", | |
34 * "Kettle", "Trove" or "BeanDoc" appear in their name, without prior | |
35 * written permission of the Walt Disney Internet Group. | |
36 * | |
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | |
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
40 * DISCLAIMED. IN NO EVENT SHALL THE WALT DISNEY INTERNET GROUP OR ITS | |
41 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
42 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
43 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
44 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
45 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
46 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
47 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
48 * ==================================================================== | |
49 * | |
50 * For more information about Tea, please see http://opensource.go.com/. | |
51 */ | |
52 | |
53 package com.go.trove.log; | |
54 | |
55 import java.io.*; | |
56 import java.text.*; | |
57 import java.util.TimeZone; | |
58 import com.go.trove.util.FastDateFormat; | |
59 | |
60 /****************************************************************************** | |
61 * LogScribe is a LogListener that writes log messages to a PrintWriter. Each | |
62 * message is printed to a line that is prepended with other LogEvent | |
63 * information. The default prepend format is as follows: | |
64 * | |
65 * <pre> | |
66 * [event type code],[date & time],[thread name],[log source name]>[one space] | |
67 * </pre> | |
68 * | |
69 * The event type codes are <tt>" D", " I", "*W" and "*E"</tt> for debug, | |
70 * info, warn and error, respectively. The default date format looks like | |
71 * this: <tt>"1999/06/08 18:08:34.067 PDT"</tt>. If there is no log source, or | |
72 * it has no name, it is omitted from the prepend. Here is a sample line that | |
73 * is written out: | |
74 * | |
75 * <pre> | |
76 * I,1999/06/08 18:08:34.67 PDT,main> Started Transaction Manager | |
77 * </pre> | |
78 * | |
79 * @author Brian S O'Neill | |
80 * @version | |
81 * <!--$$Revision: 1.1 $-->, <!--$$JustDate:--> 01/07/02 <!-- $--> | |
82 */ | |
83 public class LogScribe implements LogListener { | |
84 private PrintWriter mWriter; | |
85 private DateFormat mSlowFormat; | |
86 private FastDateFormat mFastFormat; | |
87 | |
88 private boolean mShowThread = true; | |
89 private boolean mShowSourceName = true; | |
90 | |
91 public LogScribe(PrintWriter writer) { | |
92 this(writer, (FastDateFormat)null); | |
93 } | |
94 | |
95 public LogScribe(PrintWriter writer, DateFormat format) { | |
96 mWriter = writer; | |
97 mSlowFormat = format; | |
98 if (format == null) { | |
99 mFastFormat = | |
100 FastDateFormat.getInstance("yyyy/MM/dd HH:mm:ss.SSS z"); | |
101 } | |
102 else if (format instanceof SimpleDateFormat) { | |
103 SimpleDateFormat simple = (SimpleDateFormat)format; | |
104 String pattern = simple.toPattern(); | |
105 TimeZone timeZone = simple.getTimeZone(); | |
106 DateFormatSymbols symbols = simple.getDateFormatSymbols(); | |
107 mFastFormat = | |
108 FastDateFormat.getInstance(pattern, timeZone, null, symbols); | |
109 } | |
110 } | |
111 | |
112 public LogScribe(PrintWriter writer, FastDateFormat format) { | |
113 mWriter = writer; | |
114 mSlowFormat = null; | |
115 if (format == null) { | |
116 format = FastDateFormat.getInstance("yyyy/MM/dd HH:mm:ss.SSS z"); | |
117 } | |
118 mFastFormat = format; | |
119 } | |
120 | |
121 public void logMessage(LogEvent e) { | |
122 String message = e.getMessage(); | |
123 if (message != null) { | |
124 synchronized (mWriter) { | |
125 mWriter.print(createPrepend(e)); | |
126 mWriter.println(message); | |
127 mWriter.flush(); | |
128 } | |
129 } | |
130 } | |
131 | |
132 public void logException(LogEvent e) { | |
133 Throwable t = e.getException(); | |
134 | |
135 if (t == null) { | |
136 logMessage(e); | |
137 } | |
138 else { | |
139 synchronized (mWriter) { | |
140 mWriter.print(createPrepend(e)); | |
141 t.printStackTrace(mWriter); | |
142 mWriter.flush(); | |
143 } | |
144 } | |
145 } | |
146 | |
147 /** | |
148 * The showing of the event thread name is on by default. | |
149 */ | |
150 public boolean isShowThreadEnabled() { | |
151 return mShowThread; | |
152 } | |
153 | |
154 public void setShowThreadEnabled(boolean enabled) { | |
155 mShowThread = enabled; | |
156 } | |
157 | |
158 /** | |
159 * The showing of the event source name is on by default. | |
160 */ | |
161 public boolean isShowSourceEnabled() { | |
162 return mShowSourceName; | |
163 } | |
164 | |
165 public void setShowSourceEnabled(boolean enabled) { | |
166 mShowSourceName = enabled; | |
167 } | |
168 | |
169 /** | |
170 * Creates the default line prepend for a message. | |
171 */ | |
172 protected String createPrepend(LogEvent e) { | |
173 StringBuffer pre = new StringBuffer(80); | |
174 | |
175 String code = "??"; | |
176 switch (e.getType()) { | |
177 case LogEvent.DEBUG_TYPE: | |
178 code = " D"; | |
179 break; | |
180 case LogEvent.INFO_TYPE: | |
181 code = " I"; | |
182 break; | |
183 case LogEvent.WARN_TYPE: | |
184 code = "*W"; | |
185 break; | |
186 case LogEvent.ERROR_TYPE: | |
187 code = "*E"; | |
188 break; | |
189 } | |
190 | |
191 pre.append(code); | |
192 pre.append(','); | |
193 if (mFastFormat != null) { | |
194 pre.append(mFastFormat.format(e.getTimestamp())); | |
195 } | |
196 else { | |
197 synchronized (mSlowFormat) { | |
198 pre.append(mSlowFormat.format(e.getTimestamp())); | |
199 } | |
200 } | |
201 | |
202 if (isShowThreadEnabled()) { | |
203 pre.append(','); | |
204 pre.append(e.getThreadName()); | |
205 } | |
206 | |
207 if (isShowSourceEnabled()) { | |
208 Log source = e.getLogSource(); | |
209 if (source != null) { | |
210 String sourceName = source.getName(); | |
211 if (sourceName != null) { | |
212 pre.append(','); | |
213 pre.append(sourceName); | |
214 } | |
215 } | |
216 } | |
217 | |
218 pre.append('>'); | |
219 pre.append(' '); | |
220 | |
221 return pre.toString(); | |
222 } | |
223 } |