src/share/classes/java/util/logging/Handler.java

Print this page




  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.io.UnsupportedEncodingException;



  30 /**
  31  * A <tt>Handler</tt> object takes log messages from a <tt>Logger</tt> and
  32  * exports them.  It might for example, write them to a console
  33  * or write them to a file, or send them to a network logging service,
  34  * or forward them to an OS log, or whatever.
  35  * <p>
  36  * A <tt>Handler</tt> can be disabled by doing a <tt>setLevel(Level.OFF)</tt>
  37  * and can  be re-enabled by doing a <tt>setLevel</tt> with an appropriate level.
  38  * <p>
  39  * <tt>Handler</tt> classes typically use <tt>LogManager</tt> properties to set
  40  * default values for the <tt>Handler</tt>'s <tt>Filter</tt>, <tt>Formatter</tt>,
  41  * and <tt>Level</tt>.  See the specific documentation for each concrete
  42  * <tt>Handler</tt> class.
  43  *
  44  *
  45  * @since 1.4
  46  */
  47 
  48 public abstract class Handler {
  49     private static final int offValue = Level.OFF.intValue();
  50     private final LogManager manager = LogManager.getLogManager();
  51 
  52     // We're using volatile here to avoid synchronizing getters, which
  53     // would prevent other threads from calling isLoggable()
  54     // while publish() is executing.
  55     // On the other hand, setters will be synchronized to exclude concurrent
  56     // execution with more complex methods, such as StreamHandler.publish().
  57     // We wouldn't want 'level' to be changed by another thread in the middle
  58     // of the execution of a 'publish' call.
  59     private volatile Filter filter;
  60     private volatile Formatter formatter;
  61     private volatile Level logLevel = Level.ALL;
  62     private volatile ErrorManager errorManager = new ErrorManager();
  63     private volatile String encoding;
  64 
  65     // Package private support for security checking.  When sealed
  66     // is true, we access check updates to the class.
  67     boolean sealed = true;
  68 
  69     /**
  70      * Default constructor.  The resulting <tt>Handler</tt> has a log
  71      * level of <tt>Level.ALL</tt>, no <tt>Formatter</tt>, and no
  72      * <tt>Filter</tt>.  A default <tt>ErrorManager</tt> instance is installed
  73      * as the <tt>ErrorManager</tt>.
  74      */
  75     protected Handler() {
  76     }
  77 
  78     /**
  79      * Publish a <tt>LogRecord</tt>.
  80      * <p>
  81      * The logging request was made initially to a <tt>Logger</tt> object,
  82      * which initialized the <tt>LogRecord</tt> and forwarded it here.
  83      * <p>
  84      * The <tt>Handler</tt>  is responsible for formatting the message, when and
  85      * if necessary.  The formatting should include localization.
  86      *
  87      * @param  record  description of the log event. A null record is
  88      *                 silently ignored and is not published


 285      * handler from logging the <tt>LogRecord</tt>. It will return false if
 286      * the <tt>LogRecord</tt> is null.
 287      * <p>
 288      * @param record  a <tt>LogRecord</tt>
 289      * @return true if the <tt>LogRecord</tt> would be logged.
 290      *
 291      */
 292     public boolean isLoggable(LogRecord record) {
 293         final int levelValue = getLevel().intValue();
 294         if (record.getLevel().intValue() < levelValue || levelValue == offValue) {
 295             return false;
 296         }
 297         final Filter filter = getFilter();
 298         if (filter == null) {
 299             return true;
 300         }
 301         return filter.isLoggable(record);
 302     }
 303 
 304     // Package-private support method for security checks.
 305     // If "sealed" is true, we check that the caller has
 306     // appropriate security privileges to update Handler
 307     // state and if not throw a SecurityException.
 308     void checkPermission() throws SecurityException {
 309         if (sealed) {
 310             manager.checkPermission();
 311         }






































 312     }
 313 }


  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.io.UnsupportedEncodingException;
  30 import java.security.AccessController;
  31 import java.security.PrivilegedAction;
  32 
  33 /**
  34  * A <tt>Handler</tt> object takes log messages from a <tt>Logger</tt> and
  35  * exports them.  It might for example, write them to a console
  36  * or write them to a file, or send them to a network logging service,
  37  * or forward them to an OS log, or whatever.
  38  * <p>
  39  * A <tt>Handler</tt> can be disabled by doing a <tt>setLevel(Level.OFF)</tt>
  40  * and can  be re-enabled by doing a <tt>setLevel</tt> with an appropriate level.
  41  * <p>
  42  * <tt>Handler</tt> classes typically use <tt>LogManager</tt> properties to set
  43  * default values for the <tt>Handler</tt>'s <tt>Filter</tt>, <tt>Formatter</tt>,
  44  * and <tt>Level</tt>.  See the specific documentation for each concrete
  45  * <tt>Handler</tt> class.
  46  *
  47  *
  48  * @since 1.4
  49  */
  50 
  51 public abstract class Handler {
  52     private static final int offValue = Level.OFF.intValue();
  53     private final LogManager manager = LogManager.getLogManager();
  54 
  55     // We're using volatile here to avoid synchronizing getters, which
  56     // would prevent other threads from calling isLoggable()
  57     // while publish() is executing.
  58     // On the other hand, setters will be synchronized to exclude concurrent
  59     // execution with more complex methods, such as StreamHandler.publish().
  60     // We wouldn't want 'level' to be changed by another thread in the middle
  61     // of the execution of a 'publish' call.
  62     private volatile Filter filter;
  63     private volatile Formatter formatter;
  64     private volatile Level logLevel = Level.ALL;
  65     private volatile ErrorManager errorManager = new ErrorManager();
  66     private volatile String encoding;
  67 




  68     /**
  69      * Default constructor.  The resulting <tt>Handler</tt> has a log
  70      * level of <tt>Level.ALL</tt>, no <tt>Formatter</tt>, and no
  71      * <tt>Filter</tt>.  A default <tt>ErrorManager</tt> instance is installed
  72      * as the <tt>ErrorManager</tt>.
  73      */
  74     protected Handler() {
  75     }
  76 
  77     /**
  78      * Publish a <tt>LogRecord</tt>.
  79      * <p>
  80      * The logging request was made initially to a <tt>Logger</tt> object,
  81      * which initialized the <tt>LogRecord</tt> and forwarded it here.
  82      * <p>
  83      * The <tt>Handler</tt>  is responsible for formatting the message, when and
  84      * if necessary.  The formatting should include localization.
  85      *
  86      * @param  record  description of the log event. A null record is
  87      *                 silently ignored and is not published


 284      * handler from logging the <tt>LogRecord</tt>. It will return false if
 285      * the <tt>LogRecord</tt> is null.
 286      * <p>
 287      * @param record  a <tt>LogRecord</tt>
 288      * @return true if the <tt>LogRecord</tt> would be logged.
 289      *
 290      */
 291     public boolean isLoggable(LogRecord record) {
 292         final int levelValue = getLevel().intValue();
 293         if (record.getLevel().intValue() < levelValue || levelValue == offValue) {
 294             return false;
 295         }
 296         final Filter filter = getFilter();
 297         if (filter == null) {
 298             return true;
 299         }
 300         return filter.isLoggable(record);
 301     }
 302 
 303     // Package-private support method for security checks.
 304     // We check that the caller has appropriate security privileges
 305     // to update Handler state and if not throw a SecurityException.

 306     void checkPermission() throws SecurityException {

 307         manager.checkPermission();
 308     }
 309 
 310     // Package-private support for configuring various properties
 311     // with specified/configured/default values
 312 
 313     Level getDefaultLevel() { return Level.INFO; }
 314 
 315     Formatter getDefaultFormatter() { return new SimpleFormatter(); }
 316 
 317     void configure(Formatter specifiedFormatter) {
 318         LogManager manager = LogManager.getLogManager();
 319         String cname = getClass().getName();
 320 
 321         final Level level = manager.getLevelProperty(cname + ".level", getDefaultLevel());
 322         final Filter filter = manager.getFilterProperty(cname + ".filter", null);
 323         final Formatter formatter = specifiedFormatter == null
 324                                     ? manager.getFormatterProperty(cname + ".formatter", getDefaultFormatter())
 325                                     : specifiedFormatter;
 326         final String encoding = manager.getStringProperty(cname + ".encoding", null);
 327 
 328         AccessController.doPrivileged(new PrivilegedAction<Void>() {
 329             @Override
 330             public Void run() {
 331                 setLevel(level);
 332                 setFilter(filter);
 333                 setFormatter(formatter);
 334                 try {
 335                     setEncoding(encoding);
 336                 } catch (Exception ex) {
 337                     try {
 338                         setEncoding(null);
 339                     } catch (Exception ex2) {
 340                         // doing a setEncoding with null should always work.
 341                         // assert false;
 342                     }
 343                 }
 344                 return null;
 345             }
 346         }, null, LogManager.controlPermission);
 347     }
 348 }