author | mchung |
Thu, 07 Feb 2013 09:41:47 -0800 | |
changeset 16112 | fc9eb3e70734 |
parent 14333 | 65fe875afb41 |
child 19808 | 39cb79123ab2 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
14321
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
2 |
* Copyright (c) 2000, 2012, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
||
27 |
package java.util.logging; |
|
28 |
||
29 |
import java.io.*; |
|
30 |
||
31 |
/** |
|
32 |
* Stream based logging <tt>Handler</tt>. |
|
33 |
* <p> |
|
34 |
* This is primarily intended as a base class or support class to |
|
35 |
* be used in implementing other logging <tt>Handlers</tt>. |
|
36 |
* <p> |
|
37 |
* <tt>LogRecords</tt> are published to a given <tt>java.io.OutputStream</tt>. |
|
38 |
* <p> |
|
39 |
* <b>Configuration:</b> |
|
40 |
* By default each <tt>StreamHandler</tt> is initialized using the following |
|
14321
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
41 |
* <tt>LogManager</tt> configuration properties where <tt><handler-name></tt> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
42 |
* refers to the fully-qualified class name of the handler. |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
43 |
* If properties are not defined |
2 | 44 |
* (or have invalid values) then the specified default values are used. |
45 |
* <ul> |
|
14321
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
46 |
* <li> <handler-name>.level |
2 | 47 |
* specifies the default level for the <tt>Handler</tt> |
14321
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
48 |
* (defaults to <tt>Level.INFO</tt>). </li> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
49 |
* <li> <handler-name>.filter |
2 | 50 |
* specifies the name of a <tt>Filter</tt> class to use |
14321
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
51 |
* (defaults to no <tt>Filter</tt>). </li> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
52 |
* <li> <handler-name>.formatter |
2 | 53 |
* specifies the name of a <tt>Formatter</tt> class to use |
14321
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
54 |
* (defaults to <tt>java.util.logging.SimpleFormatter</tt>). </li> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
55 |
* <li> <handler-name>.encoding |
2 | 56 |
* the name of the character set encoding to use (defaults to |
14321
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
57 |
* the default platform encoding). </li> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
58 |
* </ul> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
59 |
* <p> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
60 |
* For example, the properties for {@code StreamHandler} would be: |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
61 |
* <ul> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
62 |
* <li> java.util.logging.StreamHandler.level=INFO </li> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
63 |
* <li> java.util.logging.StreamHandler.formatter=java.util.logging.SimpleFormatter </li> |
2 | 64 |
* </ul> |
14321
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
65 |
* <p> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
66 |
* For a custom handler, e.g. com.foo.MyHandler, the properties would be: |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
67 |
* <ul> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
68 |
* <li> com.foo.MyHandler.level=INFO </li> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
69 |
* <li> com.foo.MyHandler.formatter=java.util.logging.SimpleFormatter </li> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
70 |
* </ul> |
7f9f265ac11e
7159567: inconsistent configuration of MemoryHandler
jgish
parents:
5506
diff
changeset
|
71 |
* <p> |
2 | 72 |
* @since 1.4 |
73 |
*/ |
|
74 |
||
75 |
public class StreamHandler extends Handler { |
|
76 |
private LogManager manager = LogManager.getLogManager(); |
|
77 |
private OutputStream output; |
|
78 |
private boolean doneHeader; |
|
79 |
private Writer writer; |
|
80 |
||
81 |
// Private method to configure a StreamHandler from LogManager |
|
82 |
// properties and/or default values as specified in the class |
|
83 |
// javadoc. |
|
84 |
private void configure() { |
|
85 |
LogManager manager = LogManager.getLogManager(); |
|
86 |
String cname = getClass().getName(); |
|
87 |
||
88 |
setLevel(manager.getLevelProperty(cname +".level", Level.INFO)); |
|
89 |
setFilter(manager.getFilterProperty(cname +".filter", null)); |
|
90 |
setFormatter(manager.getFormatterProperty(cname +".formatter", new SimpleFormatter())); |
|
91 |
try { |
|
92 |
setEncoding(manager.getStringProperty(cname +".encoding", null)); |
|
93 |
} catch (Exception ex) { |
|
94 |
try { |
|
95 |
setEncoding(null); |
|
96 |
} catch (Exception ex2) { |
|
97 |
// doing a setEncoding with null should always work. |
|
98 |
// assert false; |
|
99 |
} |
|
100 |
} |
|
101 |
} |
|
102 |
||
103 |
/** |
|
104 |
* Create a <tt>StreamHandler</tt>, with no current output stream. |
|
105 |
*/ |
|
106 |
public StreamHandler() { |
|
107 |
sealed = false; |
|
108 |
configure(); |
|
109 |
sealed = true; |
|
110 |
} |
|
111 |
||
112 |
/** |
|
113 |
* Create a <tt>StreamHandler</tt> with a given <tt>Formatter</tt> |
|
114 |
* and output stream. |
|
115 |
* <p> |
|
116 |
* @param out the target output stream |
|
117 |
* @param formatter Formatter to be used to format output |
|
118 |
*/ |
|
119 |
public StreamHandler(OutputStream out, Formatter formatter) { |
|
120 |
sealed = false; |
|
121 |
configure(); |
|
122 |
setFormatter(formatter); |
|
123 |
setOutputStream(out); |
|
124 |
sealed = true; |
|
125 |
} |
|
126 |
||
127 |
/** |
|
128 |
* Change the output stream. |
|
129 |
* <P> |
|
130 |
* If there is a current output stream then the <tt>Formatter</tt>'s |
|
131 |
* tail string is written and the stream is flushed and closed. |
|
132 |
* Then the output stream is replaced with the new output stream. |
|
133 |
* |
|
134 |
* @param out New output stream. May not be null. |
|
135 |
* @exception SecurityException if a security manager exists and if |
|
136 |
* the caller does not have <tt>LoggingPermission("control")</tt>. |
|
137 |
*/ |
|
138 |
protected synchronized void setOutputStream(OutputStream out) throws SecurityException { |
|
139 |
if (out == null) { |
|
140 |
throw new NullPointerException(); |
|
141 |
} |
|
142 |
flushAndClose(); |
|
143 |
output = out; |
|
144 |
doneHeader = false; |
|
145 |
String encoding = getEncoding(); |
|
146 |
if (encoding == null) { |
|
147 |
writer = new OutputStreamWriter(output); |
|
148 |
} else { |
|
149 |
try { |
|
150 |
writer = new OutputStreamWriter(output, encoding); |
|
151 |
} catch (UnsupportedEncodingException ex) { |
|
152 |
// This shouldn't happen. The setEncoding method |
|
153 |
// should have validated that the encoding is OK. |
|
154 |
throw new Error("Unexpected exception " + ex); |
|
155 |
} |
|
156 |
} |
|
157 |
} |
|
158 |
||
159 |
/** |
|
160 |
* Set (or change) the character encoding used by this <tt>Handler</tt>. |
|
161 |
* <p> |
|
162 |
* The encoding should be set before any <tt>LogRecords</tt> are written |
|
163 |
* to the <tt>Handler</tt>. |
|
164 |
* |
|
165 |
* @param encoding The name of a supported character encoding. |
|
166 |
* May be null, to indicate the default platform encoding. |
|
167 |
* @exception SecurityException if a security manager exists and if |
|
168 |
* the caller does not have <tt>LoggingPermission("control")</tt>. |
|
169 |
* @exception UnsupportedEncodingException if the named encoding is |
|
170 |
* not supported. |
|
171 |
*/ |
|
172 |
public void setEncoding(String encoding) |
|
173 |
throws SecurityException, java.io.UnsupportedEncodingException { |
|
174 |
super.setEncoding(encoding); |
|
175 |
if (output == null) { |
|
176 |
return; |
|
177 |
} |
|
178 |
// Replace the current writer with a writer for the new encoding. |
|
179 |
flush(); |
|
180 |
if (encoding == null) { |
|
181 |
writer = new OutputStreamWriter(output); |
|
182 |
} else { |
|
183 |
writer = new OutputStreamWriter(output, encoding); |
|
184 |
} |
|
185 |
} |
|
186 |
||
187 |
/** |
|
188 |
* Format and publish a <tt>LogRecord</tt>. |
|
189 |
* <p> |
|
190 |
* The <tt>StreamHandler</tt> first checks if there is an <tt>OutputStream</tt> |
|
191 |
* and if the given <tt>LogRecord</tt> has at least the required log level. |
|
192 |
* If not it silently returns. If so, it calls any associated |
|
193 |
* <tt>Filter</tt> to check if the record should be published. If so, |
|
194 |
* it calls its <tt>Formatter</tt> to format the record and then writes |
|
195 |
* the result to the current output stream. |
|
196 |
* <p> |
|
197 |
* If this is the first <tt>LogRecord</tt> to be written to a given |
|
198 |
* <tt>OutputStream</tt>, the <tt>Formatter</tt>'s "head" string is |
|
199 |
* written to the stream before the <tt>LogRecord</tt> is written. |
|
200 |
* |
|
201 |
* @param record description of the log event. A null record is |
|
202 |
* silently ignored and is not published |
|
203 |
*/ |
|
204 |
public synchronized void publish(LogRecord record) { |
|
205 |
if (!isLoggable(record)) { |
|
206 |
return; |
|
207 |
} |
|
208 |
String msg; |
|
209 |
try { |
|
210 |
msg = getFormatter().format(record); |
|
211 |
} catch (Exception ex) { |
|
212 |
// We don't want to throw an exception here, but we |
|
213 |
// report the exception to any registered ErrorManager. |
|
214 |
reportError(null, ex, ErrorManager.FORMAT_FAILURE); |
|
215 |
return; |
|
216 |
} |
|
217 |
||
218 |
try { |
|
219 |
if (!doneHeader) { |
|
220 |
writer.write(getFormatter().getHead(this)); |
|
221 |
doneHeader = true; |
|
222 |
} |
|
223 |
writer.write(msg); |
|
224 |
} catch (Exception ex) { |
|
225 |
// We don't want to throw an exception here, but we |
|
226 |
// report the exception to any registered ErrorManager. |
|
227 |
reportError(null, ex, ErrorManager.WRITE_FAILURE); |
|
228 |
} |
|
229 |
} |
|
230 |
||
231 |
||
232 |
/** |
|
233 |
* Check if this <tt>Handler</tt> would actually log a given <tt>LogRecord</tt>. |
|
234 |
* <p> |
|
235 |
* This method checks if the <tt>LogRecord</tt> has an appropriate level and |
|
236 |
* whether it satisfies any <tt>Filter</tt>. It will also return false if |
|
3853 | 237 |
* no output stream has been assigned yet or the LogRecord is null. |
2 | 238 |
* <p> |
239 |
* @param record a <tt>LogRecord</tt> |
|
240 |
* @return true if the <tt>LogRecord</tt> would be logged. |
|
241 |
* |
|
242 |
*/ |
|
243 |
public boolean isLoggable(LogRecord record) { |
|
244 |
if (writer == null || record == null) { |
|
245 |
return false; |
|
246 |
} |
|
247 |
return super.isLoggable(record); |
|
248 |
} |
|
249 |
||
250 |
/** |
|
251 |
* Flush any buffered messages. |
|
252 |
*/ |
|
253 |
public synchronized void flush() { |
|
254 |
if (writer != null) { |
|
255 |
try { |
|
256 |
writer.flush(); |
|
257 |
} catch (Exception ex) { |
|
258 |
// We don't want to throw an exception here, but we |
|
259 |
// report the exception to any registered ErrorManager. |
|
260 |
reportError(null, ex, ErrorManager.FLUSH_FAILURE); |
|
261 |
} |
|
262 |
} |
|
263 |
} |
|
264 |
||
265 |
private synchronized void flushAndClose() throws SecurityException { |
|
14216
23714b376286
7169884: LogManager checks do not work correctly for sub-types
alanb
parents:
5506
diff
changeset
|
266 |
checkPermission(); |
2 | 267 |
if (writer != null) { |
268 |
try { |
|
269 |
if (!doneHeader) { |
|
270 |
writer.write(getFormatter().getHead(this)); |
|
271 |
doneHeader = true; |
|
272 |
} |
|
273 |
writer.write(getFormatter().getTail(this)); |
|
274 |
writer.flush(); |
|
275 |
writer.close(); |
|
276 |
} catch (Exception ex) { |
|
277 |
// We don't want to throw an exception here, but we |
|
278 |
// report the exception to any registered ErrorManager. |
|
279 |
reportError(null, ex, ErrorManager.CLOSE_FAILURE); |
|
280 |
} |
|
281 |
writer = null; |
|
282 |
output = null; |
|
283 |
} |
|
284 |
} |
|
285 |
||
286 |
/** |
|
287 |
* Close the current output stream. |
|
288 |
* <p> |
|
289 |
* The <tt>Formatter</tt>'s "tail" string is written to the stream before it |
|
290 |
* is closed. In addition, if the <tt>Formatter</tt>'s "head" string has not |
|
291 |
* yet been written to the stream, it will be written before the |
|
292 |
* "tail" string. |
|
293 |
* |
|
294 |
* @exception SecurityException if a security manager exists and if |
|
295 |
* the caller does not have LoggingPermission("control"). |
|
296 |
*/ |
|
297 |
public synchronized void close() throws SecurityException { |
|
298 |
flushAndClose(); |
|
299 |
} |
|
300 |
} |