J. Duke | 319a3b9 | 2007-12-01 00:00:00 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2000-2003 Sun Microsystems, Inc. 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. Sun designates this |
| 8 | * particular file as subject to the "Classpath" exception as provided |
| 9 | * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, |
| 22 | * CA 95054 USA or visit www.sun.com if you need additional information or |
| 23 | * have any questions. |
| 24 | */ |
| 25 | |
| 26 | |
| 27 | package java.util.logging; |
| 28 | |
| 29 | import java.io.*; |
| 30 | import java.net.*; |
| 31 | |
| 32 | /** |
| 33 | * This <tt>Handler</tt> publishes log records to <tt>System.err</tt>. |
| 34 | * By default the <tt>SimpleFormatter</tt> is used to generate brief summaries. |
| 35 | * <p> |
| 36 | * <b>Configuration:</b> |
| 37 | * By default each <tt>ConsoleHandler</tt> is initialized using the following |
| 38 | * <tt>LogManager</tt> configuration properties. If properties are not defined |
| 39 | * (or have invalid values) then the specified default values are used. |
| 40 | * <ul> |
| 41 | * <li> java.util.logging.ConsoleHandler.level |
| 42 | * specifies the default level for the <tt>Handler</tt> |
| 43 | * (defaults to <tt>Level.INFO</tt>). |
| 44 | * <li> java.util.logging.ConsoleHandler.filter |
| 45 | * specifies the name of a <tt>Filter</tt> class to use |
| 46 | * (defaults to no <tt>Filter</tt>). |
| 47 | * <li> java.util.logging.ConsoleHandler.formatter |
| 48 | * specifies the name of a <tt>Formatter</tt> class to use |
| 49 | * (defaults to <tt>java.util.logging.SimpleFormatter</tt>). |
| 50 | * <li> java.util.logging.ConsoleHandler.encoding |
| 51 | * the name of the character set encoding to use (defaults to |
| 52 | * the default platform encoding). |
| 53 | * </ul> |
| 54 | * <p> |
| 55 | * @since 1.4 |
| 56 | */ |
| 57 | |
| 58 | public class ConsoleHandler extends StreamHandler { |
| 59 | // Private method to configure a ConsoleHandler from LogManager |
| 60 | // properties and/or default values as specified in the class |
| 61 | // javadoc. |
| 62 | private void configure() { |
| 63 | LogManager manager = LogManager.getLogManager(); |
| 64 | String cname = getClass().getName(); |
| 65 | |
| 66 | setLevel(manager.getLevelProperty(cname +".level", Level.INFO)); |
| 67 | setFilter(manager.getFilterProperty(cname +".filter", null)); |
| 68 | setFormatter(manager.getFormatterProperty(cname +".formatter", new SimpleFormatter())); |
| 69 | try { |
| 70 | setEncoding(manager.getStringProperty(cname +".encoding", null)); |
| 71 | } catch (Exception ex) { |
| 72 | try { |
| 73 | setEncoding(null); |
| 74 | } catch (Exception ex2) { |
| 75 | // doing a setEncoding with null should always work. |
| 76 | // assert false; |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * Create a <tt>ConsoleHandler</tt> for <tt>System.err</tt>. |
| 83 | * <p> |
| 84 | * The <tt>ConsoleHandler</tt> is configured based on |
| 85 | * <tt>LogManager</tt> properties (or their default values). |
| 86 | * |
| 87 | */ |
| 88 | public ConsoleHandler() { |
| 89 | sealed = false; |
| 90 | configure(); |
| 91 | setOutputStream(System.err); |
| 92 | sealed = true; |
| 93 | } |
| 94 | |
| 95 | /** |
| 96 | * Publish a <tt>LogRecord</tt>. |
| 97 | * <p> |
| 98 | * The logging request was made initially to a <tt>Logger</tt> object, |
| 99 | * which initialized the <tt>LogRecord</tt> and forwarded it here. |
| 100 | * <p> |
| 101 | * @param record description of the log event. A null record is |
| 102 | * silently ignored and is not published |
| 103 | */ |
| 104 | public void publish(LogRecord record) { |
| 105 | super.publish(record); |
| 106 | flush(); |
| 107 | } |
| 108 | |
| 109 | /** |
| 110 | * Override <tt>StreamHandler.close</tt> to do a flush but not |
| 111 | * to close the output stream. That is, we do <b>not</b> |
| 112 | * close <tt>System.err</tt>. |
| 113 | */ |
| 114 | public void close() { |
| 115 | flush(); |
| 116 | } |
| 117 | } |