54 * (defaults to no <tt>Filter</tt>). </li>
55 * <li> <handler-name>.formatter
56 * specifies the name of a <tt>Formatter</tt> class to use
57 * (defaults to <tt>java.util.logging.SimpleFormatter</tt>). </li>
58 * <li> <handler-name>.encoding
59 * the name of the character set encoding to use (defaults to
60 * the default platform encoding). </li>
61 * </ul>
62 * <p>
63 * For example, the properties for {@code StreamHandler} would be:
64 * <ul>
65 * <li> java.util.logging.StreamHandler.level=INFO </li>
66 * <li> java.util.logging.StreamHandler.formatter=java.util.logging.SimpleFormatter </li>
67 * </ul>
68 * <p>
69 * For a custom handler, e.g. com.foo.MyHandler, the properties would be:
70 * <ul>
71 * <li> com.foo.MyHandler.level=INFO </li>
72 * <li> com.foo.MyHandler.formatter=java.util.logging.SimpleFormatter </li>
73 * </ul>
74 * <p>
75 * @since 1.4
76 */
77
78 public class StreamHandler extends Handler {
79 private OutputStream output;
80 private boolean doneHeader;
81 private volatile Writer writer;
82
83 /**
84 * Create a <tt>StreamHandler</tt>, with no current output stream.
85 */
86 public StreamHandler() {
87 // configure with specific defaults for StreamHandler
88 super(Level.INFO, new SimpleFormatter(), null);
89 }
90
91 /**
92 * Create a <tt>StreamHandler</tt> with a given <tt>Formatter</tt>
93 * and output stream.
94 * <p>
95 * @param out the target output stream
96 * @param formatter Formatter to be used to format output
97 */
98 public StreamHandler(OutputStream out, Formatter formatter) {
99 // configure with default level but use specified formatter
100 super(Level.INFO, null, Objects.requireNonNull(formatter));
101
102 setOutputStreamPrivileged(out);
103 }
104
105 /**
106 * @see Handler#Handler(Level, Formatter, Formatter)
107 */
108 StreamHandler(Level defaultLevel,
109 Formatter defaultFormatter,
110 Formatter specifiedFormatter) {
111 super(defaultLevel, defaultFormatter, specifiedFormatter);
112 }
113
114 /**
207 try {
208 if (!doneHeader) {
209 writer.write(getFormatter().getHead(this));
210 doneHeader = true;
211 }
212 writer.write(msg);
213 } catch (Exception ex) {
214 // We don't want to throw an exception here, but we
215 // report the exception to any registered ErrorManager.
216 reportError(null, ex, ErrorManager.WRITE_FAILURE);
217 }
218 }
219
220
221 /**
222 * Check if this <tt>Handler</tt> would actually log a given <tt>LogRecord</tt>.
223 * <p>
224 * This method checks if the <tt>LogRecord</tt> has an appropriate level and
225 * whether it satisfies any <tt>Filter</tt>. It will also return false if
226 * no output stream has been assigned yet or the LogRecord is null.
227 * <p>
228 * @param record a <tt>LogRecord</tt>
229 * @return true if the <tt>LogRecord</tt> would be logged.
230 *
231 */
232 @Override
233 public boolean isLoggable(LogRecord record) {
234 if (writer == null || record == null) {
235 return false;
236 }
237 return super.isLoggable(record);
238 }
239
240 /**
241 * Flush any buffered messages.
242 */
243 @Override
244 public synchronized void flush() {
245 if (writer != null) {
246 try {
247 writer.flush();
|
54 * (defaults to no <tt>Filter</tt>). </li>
55 * <li> <handler-name>.formatter
56 * specifies the name of a <tt>Formatter</tt> class to use
57 * (defaults to <tt>java.util.logging.SimpleFormatter</tt>). </li>
58 * <li> <handler-name>.encoding
59 * the name of the character set encoding to use (defaults to
60 * the default platform encoding). </li>
61 * </ul>
62 * <p>
63 * For example, the properties for {@code StreamHandler} would be:
64 * <ul>
65 * <li> java.util.logging.StreamHandler.level=INFO </li>
66 * <li> java.util.logging.StreamHandler.formatter=java.util.logging.SimpleFormatter </li>
67 * </ul>
68 * <p>
69 * For a custom handler, e.g. com.foo.MyHandler, the properties would be:
70 * <ul>
71 * <li> com.foo.MyHandler.level=INFO </li>
72 * <li> com.foo.MyHandler.formatter=java.util.logging.SimpleFormatter </li>
73 * </ul>
74 *
75 * @since 1.4
76 */
77
78 public class StreamHandler extends Handler {
79 private OutputStream output;
80 private boolean doneHeader;
81 private volatile Writer writer;
82
83 /**
84 * Create a <tt>StreamHandler</tt>, with no current output stream.
85 */
86 public StreamHandler() {
87 // configure with specific defaults for StreamHandler
88 super(Level.INFO, new SimpleFormatter(), null);
89 }
90
91 /**
92 * Create a <tt>StreamHandler</tt> with a given <tt>Formatter</tt>
93 * and output stream.
94 *
95 * @param out the target output stream
96 * @param formatter Formatter to be used to format output
97 */
98 public StreamHandler(OutputStream out, Formatter formatter) {
99 // configure with default level but use specified formatter
100 super(Level.INFO, null, Objects.requireNonNull(formatter));
101
102 setOutputStreamPrivileged(out);
103 }
104
105 /**
106 * @see Handler#Handler(Level, Formatter, Formatter)
107 */
108 StreamHandler(Level defaultLevel,
109 Formatter defaultFormatter,
110 Formatter specifiedFormatter) {
111 super(defaultLevel, defaultFormatter, specifiedFormatter);
112 }
113
114 /**
207 try {
208 if (!doneHeader) {
209 writer.write(getFormatter().getHead(this));
210 doneHeader = true;
211 }
212 writer.write(msg);
213 } catch (Exception ex) {
214 // We don't want to throw an exception here, but we
215 // report the exception to any registered ErrorManager.
216 reportError(null, ex, ErrorManager.WRITE_FAILURE);
217 }
218 }
219
220
221 /**
222 * Check if this <tt>Handler</tt> would actually log a given <tt>LogRecord</tt>.
223 * <p>
224 * This method checks if the <tt>LogRecord</tt> has an appropriate level and
225 * whether it satisfies any <tt>Filter</tt>. It will also return false if
226 * no output stream has been assigned yet or the LogRecord is null.
227 *
228 * @param record a <tt>LogRecord</tt>
229 * @return true if the <tt>LogRecord</tt> would be logged.
230 *
231 */
232 @Override
233 public boolean isLoggable(LogRecord record) {
234 if (writer == null || record == null) {
235 return false;
236 }
237 return super.isLoggable(record);
238 }
239
240 /**
241 * Flush any buffered messages.
242 */
243 @Override
244 public synchronized void flush() {
245 if (writer != null) {
246 try {
247 writer.flush();
|