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

Print this page




  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


 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 }


  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 isSealed
  66     // returns true, we access check updates to the class.
  67     boolean isSealed() {
  68         return true;
  69     }
  70 
  71     /**
  72      * Default constructor.  The resulting <tt>Handler</tt> has a log
  73      * level of <tt>Level.ALL</tt>, no <tt>Formatter</tt>, and no
  74      * <tt>Filter</tt>.  A default <tt>ErrorManager</tt> instance is installed
  75      * as the <tt>ErrorManager</tt>.
  76      */
  77     protected Handler() {
  78     }
  79 
  80     /**
  81      * Publish a <tt>LogRecord</tt>.
  82      * <p>
  83      * The logging request was made initially to a <tt>Logger</tt> object,
  84      * which initialized the <tt>LogRecord</tt> and forwarded it here.
  85      * <p>
  86      * The <tt>Handler</tt>  is responsible for formatting the message, when and
  87      * if necessary.  The formatting should include localization.
  88      *
  89      * @param  record  description of the log event. A null record is


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