001 /*
002 * Copyright 2000-2003 Sun Microsystems, Inc. All Rights Reserved.
003 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004 *
005 * This code is free software; you can redistribute it and/or modify it
006 * under the terms of the GNU General Public License version 2 only, as
007 * published by the Free Software Foundation. Sun designates this
008 * particular file as subject to the "Classpath" exception as provided
009 * by Sun in the LICENSE file that accompanied this code.
010 *
011 * This code is distributed in the hope that it will be useful, but WITHOUT
012 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014 * version 2 for more details (a copy is included in the LICENSE file that
015 * accompanied this code).
016 *
017 * You should have received a copy of the GNU General Public License version
018 * 2 along with this work; if not, write to the Free Software Foundation,
019 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020 *
021 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022 * CA 95054 USA or visit www.sun.com if you need additional information or
023 * have any questions.
024 */
025
026 package java.util.logging;
027
028 import java.io.*;
029 import java.net.*;
030
031 /**
032 * This <tt>Handler</tt> publishes log records to <tt>System.err</tt>.
033 * By default the <tt>SimpleFormatter</tt> is used to generate brief summaries.
034 * <p>
035 * <b>Configuration:</b>
036 * By default each <tt>ConsoleHandler</tt> is initialized using the following
037 * <tt>LogManager</tt> configuration properties. If properties are not defined
038 * (or have invalid values) then the specified default values are used.
039 * <ul>
040 * <li> java.util.logging.ConsoleHandler.level
041 * specifies the default level for the <tt>Handler</tt>
042 * (defaults to <tt>Level.INFO</tt>).
043 * <li> java.util.logging.ConsoleHandler.filter
044 * specifies the name of a <tt>Filter</tt> class to use
045 * (defaults to no <tt>Filter</tt>).
046 * <li> java.util.logging.ConsoleHandler.formatter
047 * specifies the name of a <tt>Formatter</tt> class to use
048 * (defaults to <tt>java.util.logging.SimpleFormatter</tt>).
049 * <li> java.util.logging.ConsoleHandler.encoding
050 * the name of the character set encoding to use (defaults to
051 * the default platform encoding).
052 * </ul>
053 * <p>
054 * @version 1.19, 05/05/07
055 * @since 1.4
056 */
057
058 public class ConsoleHandler extends StreamHandler {
059 // Private method to configure a ConsoleHandler from LogManager
060 // properties and/or default values as specified in the class
061 // javadoc.
062 private void configure() {
063 LogManager manager = LogManager.getLogManager();
064 String cname = getClass().getName();
065
066 setLevel(manager.getLevelProperty(cname + ".level", Level.INFO));
067 setFilter(manager.getFilterProperty(cname + ".filter", null));
068 setFormatter(manager.getFormatterProperty(cname + ".formatter",
069 new SimpleFormatter()));
070 try {
071 setEncoding(manager.getStringProperty(cname + ".encoding",
072 null));
073 } catch (Exception ex) {
074 try {
075 setEncoding(null);
076 } catch (Exception ex2) {
077 // doing a setEncoding with null should always work.
078 // assert false;
079 }
080 }
081 }
082
083 /**
084 * Create a <tt>ConsoleHandler</tt> for <tt>System.err</tt>.
085 * <p>
086 * The <tt>ConsoleHandler</tt> is configured based on
087 * <tt>LogManager</tt> properties (or their default values).
088 *
089 */
090 public ConsoleHandler() {
091 sealed = false;
092 configure();
093 setOutputStream(System.err);
094 sealed = true;
095 }
096
097 /**
098 * Publish a <tt>LogRecord</tt>.
099 * <p>
100 * The logging request was made initially to a <tt>Logger</tt> object,
101 * which initialized the <tt>LogRecord</tt> and forwarded it here.
102 * <p>
103 * @param record description of the log event. A null record is
104 * silently ignored and is not published
105 */
106 public void publish(LogRecord record) {
107 super .publish(record);
108 flush();
109 }
110
111 /**
112 * Override <tt>StreamHandler.close</tt> to do a flush but not
113 * to close the output stream. That is, we do <b>not</b>
114 * close <tt>System.err</tt>.
115 */
116 public void close() {
117 flush();
118 }
119 }
|