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 89 */ 90 public abstract void publish(LogRecord record); 91 92 /** 93 * Flush any buffered output. 94 */ 95 public abstract void flush(); 96 97 /** 98 * Close the <tt>Handler</tt> and free all associated resources. 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 * Package-private constructor for chaining from subclass constructors 79 * that wish to configure the handler with specific default and/or 80 * specified values. 81 * 82 * @param defaultLevel a default {@link Level} to configure if one is 83 * not found in LogManager configuration properties 84 * @param defaultFormatter a default {@link Formatter} to configure if one is 85 * not specified by {@code specifiedFormatter} parameter 86 * nor found in LogManager configuration properties 87 * @param specifiedFormatter if not null, this is the formatter to configure 88 */ 89 Handler(Level defaultLevel, Formatter defaultFormatter, 90 Formatter specifiedFormatter) { 91 92 LogManager manager = LogManager.getLogManager(); 93 String cname = getClass().getName(); 94 95 final Level level = manager.getLevelProperty(cname + ".level", defaultLevel); 96 final Filter filter = manager.getFilterProperty(cname + ".filter", null); 97 final Formatter formatter = specifiedFormatter == null 98 ? manager.getFormatterProperty(cname + ".formatter", defaultFormatter) 99 : specifiedFormatter; 100 final String encoding = manager.getStringProperty(cname + ".encoding", null); 101 102 AccessController.doPrivileged(new PrivilegedAction<Void>() { 103 @Override 104 public Void run() { 105 setLevel(level); 106 setFilter(filter); 107 setFormatter(formatter); 108 try { 109 setEncoding(encoding); 110 } catch (Exception ex) { 111 try { 112 setEncoding(null); 113 } catch (Exception ex2) { 114 // doing a setEncoding with null should always work. 115 // assert false; 116 } 117 } 118 return null; 119 } 120 }, null, LogManager.controlPermission); 121 } 122 123 /** 124 * Publish a <tt>LogRecord</tt>. 125 * <p> 126 * The logging request was made initially to a <tt>Logger</tt> object, 127 * which initialized the <tt>LogRecord</tt> and forwarded it here. 128 * <p> 129 * The <tt>Handler</tt> is responsible for formatting the message, when and 130 * if necessary. The formatting should include localization. 131 * 132 * @param record description of the log event. A null record is 133 * silently ignored and is not published 134 */ 135 public abstract void publish(LogRecord record); 136 137 /** 138 * Flush any buffered output. 139 */ 140 public abstract void flush(); 141 142 /** 143 * Close the <tt>Handler</tt> and free all associated resources. 330 * handler from logging the <tt>LogRecord</tt>. It will return false if 331 * the <tt>LogRecord</tt> is null. 332 * <p> 333 * @param record a <tt>LogRecord</tt> 334 * @return true if the <tt>LogRecord</tt> would be logged. 335 * 336 */ 337 public boolean isLoggable(LogRecord record) { 338 final int levelValue = getLevel().intValue(); 339 if (record.getLevel().intValue() < levelValue || levelValue == offValue) { 340 return false; 341 } 342 final Filter filter = getFilter(); 343 if (filter == null) { 344 return true; 345 } 346 return filter.isLoggable(record); 347 } 348 349 // Package-private support method for security checks. 350 // We check that the caller has appropriate security privileges 351 // to update Handler state and if not throw a SecurityException. 352 void checkPermission() throws SecurityException { 353 manager.checkPermission(); 354 } 355 } |