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
29/**
30 * This {@code Handler} publishes log records to {@code System.err}.
31 * By default the {@code SimpleFormatter} is used to generate brief summaries.
32 * <p>
33 * <b>Configuration:</b>
34 * By default each {@code ConsoleHandler} is initialized using the following
35 * {@code LogManager} configuration properties where {@code <handler-name>}
36 * refers to the fully-qualified class name of the handler.
37 * If properties are not defined
38 * (or have invalid values) then the specified default values are used.
39 * <ul>
40 * <li>   &lt;handler-name&gt;.level
41 *        specifies the default level for the {@code Handler}
42 *        (defaults to {@code Level.INFO}). </li>
43 * <li>   &lt;handler-name&gt;.filter
44 *        specifies the name of a {@code Filter} class to use
45 *        (defaults to no {@code Filter}). </li>
46 * <li>   &lt;handler-name&gt;.formatter
47 *        specifies the name of a {@code Formatter} class to use
48 *        (defaults to {@code java.util.logging.SimpleFormatter}). </li>
49 * <li>   &lt;handler-name&gt;.encoding
50 *        the name of the character set encoding to use (defaults to
51 *        the default platform encoding). </li>
52 * </ul>
53 * <p>
54 * For example, the properties for {@code ConsoleHandler} would be:
55 * <ul>
56 * <li>   java.util.logging.ConsoleHandler.level=INFO </li>
57 * <li>   java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter </li>
58 * </ul>
59 * <p>
60 * For a custom handler, e.g. com.foo.MyHandler, the properties would be:
61 * <ul>
62 * <li>   com.foo.MyHandler.level=INFO </li>
63 * <li>   com.foo.MyHandler.formatter=java.util.logging.SimpleFormatter </li>
64 * </ul>
65 *
66 * @since 1.4
67 */
68public class ConsoleHandler extends StreamHandler {
69
70    /**
71     * Create a {@code ConsoleHandler} for {@code System.err}.
72     * <p>
73     * The {@code ConsoleHandler} is configured based on
74     * {@code LogManager} properties (or their default values).
75     *
76     */
77    public ConsoleHandler() {
78        // configure with specific defaults for ConsoleHandler
79        super(Level.INFO, new SimpleFormatter(), null);
80
81        setOutputStreamPrivileged(System.err);
82    }
83
84    /**
85     * Publish a {@code LogRecord}.
86     * <p>
87     * The logging request was made initially to a {@code Logger} object,
88     * which initialized the {@code LogRecord} and forwarded it here.
89     *
90     * @param  record  description of the log event. A null record is
91     *                 silently ignored and is not published
92     */
93    @Override
94    public void publish(LogRecord record) {
95        super.publish(record);
96        flush();
97    }
98
99    /**
100     * Override {@code StreamHandler.close} to do a flush but not
101     * to close the output stream.  That is, we do <b>not</b>
102     * close {@code System.err}.
103     */
104    @Override
105    public void close() {
106        flush();
107    }
108}
109