1 /*
   2  * Copyright (c) 2000, 2012, 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 
  27 package java.util.logging;
  28 
  29 import java.security.AccessController;
  30 import java.security.PrivilegedAction;
  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 where {@code <handler-name>}
  39  * refers to the fully-qualified class name of the handler.
  40  * If properties are not defined
  41  * (or have invalid values) then the specified default values are used.
  42  * <ul>
  43  * <li>   &lt;handler-name&gt;.level
  44  *        specifies the default level for the <tt>Handler</tt>
  45  *        (defaults to <tt>Level.INFO</tt>). </li>
  46  * <li>   &lt;handler-name&gt;.filter
  47  *        specifies the name of a <tt>Filter</tt> class to use
  48  *        (defaults to no <tt>Filter</tt>). </li>
  49  * <li>   &lt;handler-name&gt;.formatter
  50  *        specifies the name of a <tt>Formatter</tt> class to use
  51  *        (defaults to <tt>java.util.logging.SimpleFormatter</tt>). </li>
  52  * <li>   &lt;handler-name&gt;.encoding
  53  *        the name of the character set encoding to use (defaults to
  54  *        the default platform encoding). </li>
  55  * </ul>
  56  * <p>
  57  * For example, the properties for {@code ConsoleHandler} would be:
  58  * <ul>
  59  * <li>   java.util.logging.ConsoleHandler.level=INFO </li>
  60  * <li>   java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter </li>
  61  * </ul>
  62  * <p>
  63  * For a custom handler, e.g. com.foo.MyHandler, the properties would be:
  64  * <ul>
  65  * <li>   com.foo.MyHandler.level=INFO </li>
  66  * <li>   com.foo.MyHandler.formatter=java.util.logging.SimpleFormatter </li>
  67  * </ul>
  68  * <p>
  69  * @since 1.4
  70  */
  71 public class ConsoleHandler extends StreamHandler {
  72     // Private PrivilegedAction to configure a ConsoleHandler from LogManager
  73     // properties and/or default values as specified in the class
  74     // javadoc.
  75     private class ConfigureAction implements PrivilegedAction<Void> {
  76         @Override
  77         public Void run() {
  78             LogManager manager = LogManager.getLogManager();
  79             String cname = ConsoleHandler.this.getClass().getName();
  80 
  81             setLevel(manager.getLevelProperty(cname +".level", Level.INFO));
  82             setFilter(manager.getFilterProperty(cname +".filter", null));
  83             setFormatter(manager.getFormatterProperty(cname +".formatter", new SimpleFormatter()));
  84             try {
  85                 setEncoding(manager.getStringProperty(cname +".encoding", null));
  86             } catch (Exception ex) {
  87                 try {
  88                     setEncoding(null);
  89                 } catch (Exception ex2) {
  90                     // doing a setEncoding with null should always work.
  91                     // assert false;
  92                 }
  93             }
  94             setOutputStream(System.err);
  95             return null;
  96         }
  97     }
  98 
  99     /**
 100      * Create a <tt>ConsoleHandler</tt> for <tt>System.err</tt>.
 101      * <p>
 102      * The <tt>ConsoleHandler</tt> is configured based on
 103      * <tt>LogManager</tt> properties (or their default values).
 104      *
 105      */
 106     public ConsoleHandler() {
 107         AccessController.doPrivileged(new ConfigureAction(),
 108                                       null, LogManager.controlPermission);
 109     }
 110 
 111     /**
 112      * Publish a <tt>LogRecord</tt>.
 113      * <p>
 114      * The logging request was made initially to a <tt>Logger</tt> object,
 115      * which initialized the <tt>LogRecord</tt> and forwarded it here.
 116      * <p>
 117      * @param  record  description of the log event. A null record is
 118      *                 silently ignored and is not published
 119      */
 120     @Override
 121     public void publish(LogRecord record) {
 122         super.publish(record);
 123         flush();
 124     }
 125 
 126     /**
 127      * Override <tt>StreamHandler.close</tt> to do a flush but not
 128      * to close the output stream.  That is, we do <b>not</b>
 129      * close <tt>System.err</tt>.
 130      */
 131     @Override
 132     public void close() {
 133         flush();
 134     }
 135 }