jdk/src/share/classes/java/util/logging/ConsoleHandler.java
author alanb
Mon, 10 Jun 2013 12:58:32 +0100
changeset 18156 edb590d448c5
parent 14321 7f9f265ac11e
child 19808 39cb79123ab2
permissions -rw-r--r--
8016217: More javadoc warnings Reviewed-by: lancea, chegar, psandoz
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     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
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
package java.util.logging;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.net.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * This <tt>Handler</tt> publishes log records to <tt>System.err</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 * By default the <tt>SimpleFormatter</tt> is used to generate brief summaries.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * <b>Configuration:</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * By default each <tt>ConsoleHandler</tt> is initialized using the following
18156
edb590d448c5 8016217: More javadoc warnings
alanb
parents: 14321
diff changeset
    38
 * <tt>LogManager</tt> configuration properties where {@code <handler-name>}
14321
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    39
 * refers to the fully-qualified class name of the handler.
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    40
 * If properties are not defined
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 * (or have invalid values) then the specified default values are used.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * <ul>
14321
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    43
 * <li>   &lt;handler-name&gt;.level
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 *        specifies the default level for the <tt>Handler</tt>
14321
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    45
 *        (defaults to <tt>Level.INFO</tt>). </li>
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    46
 * <li>   &lt;handler-name&gt;.filter
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *        specifies the name of a <tt>Filter</tt> class to use
14321
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    48
 *        (defaults to no <tt>Filter</tt>). </li>
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    49
 * <li>   &lt;handler-name&gt;.formatter
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *        specifies the name of a <tt>Formatter</tt> class to use
14321
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    51
 *        (defaults to <tt>java.util.logging.SimpleFormatter</tt>). </li>
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    52
 * <li>   &lt;handler-name&gt;.encoding
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
 *        the name of the character set encoding to use (defaults to
14321
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    54
 *        the default platform encoding). </li>
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    55
 * </ul>
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    56
 * <p>
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    57
 * For example, the properties for {@code ConsoleHandler} would be:
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
 * <li>   java.util.logging.ConsoleHandler.level=INFO </li>
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    60
 * <li>   java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter </li>
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
 * <p>
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    63
 * For a custom handler, e.g. com.foo.MyHandler, the properties would be:
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    64
 * <ul>
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    65
 * <li>   com.foo.MyHandler.level=INFO </li>
7f9f265ac11e 7159567: inconsistent configuration of MemoryHandler
jgish
parents: 5506
diff changeset
    66
 * <li>   com.foo.MyHandler.formatter=java.util.logging.SimpleFormatter </li>
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
 * </ul>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
 * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
 * @since 1.4
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
public class ConsoleHandler extends StreamHandler {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
    // Private method to configure a ConsoleHandler from LogManager
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    // properties and/or default values as specified in the class
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
    // javadoc.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
    private void configure() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        LogManager manager = LogManager.getLogManager();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        String cname = getClass().getName();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        setLevel(manager.getLevelProperty(cname +".level", Level.INFO));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        setFilter(manager.getFilterProperty(cname +".filter", null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        setFormatter(manager.getFormatterProperty(cname +".formatter", new SimpleFormatter()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            setEncoding(manager.getStringProperty(cname +".encoding", null));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        } catch (Exception ex) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                setEncoding(null);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            } catch (Exception ex2) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                // doing a setEncoding with null should always work.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                // assert false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     * Create a <tt>ConsoleHandler</tt> for <tt>System.err</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
     * The <tt>ConsoleHandler</tt> is configured based on
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
     * <tt>LogManager</tt> properties (or their default values).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
    public ConsoleHandler() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        sealed = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        configure();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        setOutputStream(System.err);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
        sealed = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     * Publish a <tt>LogRecord</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * The logging request was made initially to a <tt>Logger</tt> object,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * which initialized the <tt>LogRecord</tt> and forwarded it here.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     * <p>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * @param  record  description of the log event. A null record is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     *                 silently ignored and is not published
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    public void publish(LogRecord record) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        super.publish(record);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
     * Override <tt>StreamHandler.close</tt> to do a flush but not
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
     * to close the output stream.  That is, we do <b>not</b>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
     * close <tt>System.err</tt>.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    public void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
        flush();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
}