StreamHandler.java revision 4ff539dbc5a809ef3eacbe2d9f2b97f640b7e9cf
1/*
2 * Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
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
7 * published by the Free Software Foundation.  Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
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 *
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.
24 */
25
26
27package java.util.logging;
28
29import 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
41 * <tt>LogManager</tt> configuration properties where <tt>&lt;handler-name&gt;</tt>
42 * refers to the fully-qualified class name of the handler.
43 * If properties are not defined
44 * (or have invalid values) then the specified default values are used.
45 * <ul>
46 * <li>   &lt;handler-name&gt;.level
47 *        specifies the default level for the <tt>Handler</tt>
48 *        (defaults to <tt>Level.INFO</tt>). </li>
49 * <li>   &lt;handler-name&gt;.filter
50 *        specifies the name of a <tt>Filter</tt> class to use
51 *         (defaults to no <tt>Filter</tt>). </li>
52 * <li>   &lt;handler-name&gt;.formatter
53 *        specifies the name of a <tt>Formatter</tt> class to use
54 *        (defaults to <tt>java.util.logging.SimpleFormatter</tt>). </li>
55 * <li>   &lt;handler-name&gt;.encoding
56 *        the name of the character set encoding to use (defaults to
57 *        the default platform encoding). </li>
58 * </ul>
59 * <p>
60 * For example, the properties for {@code StreamHandler} would be:
61 * <ul>
62 * <li>   java.util.logging.StreamHandler.level=INFO </li>
63 * <li>   java.util.logging.StreamHandler.formatter=java.util.logging.SimpleFormatter </li>
64 * </ul>
65 * <p>
66 * For a custom handler, e.g. com.foo.MyHandler, the properties would be:
67 * <ul>
68 * <li>   com.foo.MyHandler.level=INFO </li>
69 * <li>   com.foo.MyHandler.formatter=java.util.logging.SimpleFormatter </li>
70 * </ul>
71 * <p>
72 * @since 1.4
73 */
74
75public class StreamHandler extends Handler {
76    private OutputStream output;
77    private boolean doneHeader;
78    private volatile Writer writer;
79
80    // Private method to configure a StreamHandler from LogManager
81    // properties and/or default values as specified in the class
82    // javadoc.
83    private void configure() {
84        LogManager manager = LogManager.getLogManager();
85        String cname = getClass().getName();
86
87        setLevel(manager.getLevelProperty(cname +".level", Level.INFO));
88        setFilter(manager.getFilterProperty(cname +".filter", null));
89        setFormatter(manager.getFormatterProperty(cname +".formatter", new SimpleFormatter()));
90        try {
91            setEncoding(manager.getStringProperty(cname +".encoding", null));
92        } catch (Exception ex) {
93            try {
94                setEncoding(null);
95            } catch (Exception ex2) {
96                // doing a setEncoding with null should always work.
97                // assert false;
98            }
99        }
100    }
101
102    /**
103     * Create a <tt>StreamHandler</tt>, with no current output stream.
104     */
105    public StreamHandler() {
106        sealed = false;
107        configure();
108        sealed = true;
109    }
110
111    /**
112     * Create a <tt>StreamHandler</tt> with a given <tt>Formatter</tt>
113     * and output stream.
114     * <p>
115     * @param out         the target output stream
116     * @param formatter   Formatter to be used to format output
117     */
118    public StreamHandler(OutputStream out, Formatter formatter) {
119        sealed = false;
120        configure();
121        setFormatter(formatter);
122        setOutputStream(out);
123        sealed = true;
124    }
125
126    /**
127     * Change the output stream.
128     * <P>
129     * If there is a current output stream then the <tt>Formatter</tt>'s
130     * tail string is written and the stream is flushed and closed.
131     * Then the output stream is replaced with the new output stream.
132     *
133     * @param out   New output stream.  May not be null.
134     * @exception  SecurityException  if a security manager exists and if
135     *             the caller does not have <tt>LoggingPermission("control")</tt>.
136     */
137    protected synchronized void setOutputStream(OutputStream out) throws SecurityException {
138        if (out == null) {
139            throw new NullPointerException();
140        }
141        flushAndClose();
142        output = out;
143        doneHeader = false;
144        String encoding = getEncoding();
145        if (encoding == null) {
146            writer = new OutputStreamWriter(output);
147        } else {
148            try {
149                writer = new OutputStreamWriter(output, encoding);
150            } catch (UnsupportedEncodingException ex) {
151                // This shouldn't happen.  The setEncoding method
152                // should have validated that the encoding is OK.
153                throw new Error("Unexpected exception " + ex);
154            }
155        }
156    }
157
158    /**
159     * Set (or change) the character encoding used by this <tt>Handler</tt>.
160     * <p>
161     * The encoding should be set before any <tt>LogRecords</tt> are written
162     * to the <tt>Handler</tt>.
163     *
164     * @param encoding  The name of a supported character encoding.
165     *        May be null, to indicate the default platform encoding.
166     * @exception  SecurityException  if a security manager exists and if
167     *             the caller does not have <tt>LoggingPermission("control")</tt>.
168     * @exception  UnsupportedEncodingException if the named encoding is
169     *          not supported.
170     */
171    @Override
172    public synchronized 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    @Override
205    public synchronized void publish(LogRecord record) {
206        if (!isLoggable(record)) {
207            return;
208        }
209        String msg;
210        try {
211            msg = getFormatter().format(record);
212        } catch (Exception ex) {
213            // We don't want to throw an exception here, but we
214            // report the exception to any registered ErrorManager.
215            reportError(null, ex, ErrorManager.FORMAT_FAILURE);
216            return;
217        }
218
219        try {
220            if (!doneHeader) {
221                writer.write(getFormatter().getHead(this));
222                doneHeader = true;
223            }
224            writer.write(msg);
225        } catch (Exception ex) {
226            // We don't want to throw an exception here, but we
227            // report the exception to any registered ErrorManager.
228            reportError(null, ex, ErrorManager.WRITE_FAILURE);
229        }
230    }
231
232
233    /**
234     * Check if this <tt>Handler</tt> would actually log a given <tt>LogRecord</tt>.
235     * <p>
236     * This method checks if the <tt>LogRecord</tt> has an appropriate level and
237     * whether it satisfies any <tt>Filter</tt>.  It will also return false if
238     * no output stream has been assigned yet or the LogRecord is null.
239     * <p>
240     * @param record  a <tt>LogRecord</tt>
241     * @return true if the <tt>LogRecord</tt> would be logged.
242     *
243     */
244    @Override
245    public boolean isLoggable(LogRecord record) {
246        if (writer == null || record == null) {
247            return false;
248        }
249        return super.isLoggable(record);
250    }
251
252    /**
253     * Flush any buffered messages.
254     */
255    @Override
256    public synchronized void flush() {
257        if (writer != null) {
258            try {
259                writer.flush();
260            } catch (Exception ex) {
261                // We don't want to throw an exception here, but we
262                // report the exception to any registered ErrorManager.
263                reportError(null, ex, ErrorManager.FLUSH_FAILURE);
264            }
265        }
266    }
267
268    private synchronized void flushAndClose() throws SecurityException {
269        checkPermission();
270        if (writer != null) {
271            try {
272                if (!doneHeader) {
273                    writer.write(getFormatter().getHead(this));
274                    doneHeader = true;
275                }
276                writer.write(getFormatter().getTail(this));
277                writer.flush();
278                writer.close();
279            } catch (Exception ex) {
280                // We don't want to throw an exception here, but we
281                // report the exception to any registered ErrorManager.
282                reportError(null, ex, ErrorManager.CLOSE_FAILURE);
283            }
284            writer = null;
285            output = null;
286        }
287    }
288
289    /**
290     * Close the current output stream.
291     * <p>
292     * The <tt>Formatter</tt>'s "tail" string is written to the stream before it
293     * is closed.  In addition, if the <tt>Formatter</tt>'s "head" string has not
294     * yet been written to the stream, it will be written before the
295     * "tail" string.
296     *
297     * @exception  SecurityException  if a security manager exists and if
298     *             the caller does not have LoggingPermission("control").
299     */
300    @Override
301    public synchronized void close() throws SecurityException {
302        flushAndClose();
303    }
304}
305