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
31 /**
32 * A <tt>Handler</tt> object takes log messages from a <tt>Logger</tt> and
33 * exports them. It might for example, write them to a console
34 * or write them to a file, or send them to a network logging service,
35 * or forward them to an OS log, or whatever.
36 * <p>
37 * A <tt>Handler</tt> can be disabled by doing a <tt>setLevel(Level.OFF)</tt>
38 * and can be re-enabled by doing a <tt>setLevel</tt> with an appropriate level.
39 * <p>
40 * <tt>Handler</tt> classes typically use <tt>LogManager</tt> properties to set
41 * default values for the <tt>Handler</tt>'s <tt>Filter</tt>, <tt>Formatter</tt>,
42 * and <tt>Level</tt>. See the specific documentation for each concrete
43 * <tt>Handler</tt> class.
44 *
45 *
46 * @since 1.4
47 */
48
49 public abstract class Handler {
50 private static final int offValue = Level.OFF.intValue();
51 private final LogManager manager = LogManager.getLogManager();
52
53 // We're using volatile here to avoid synchronizing getters, which
54 // would prevent other threads from calling isLoggable()
55 // while publish() is executing.
56 // On the other hand, setters will be synchronized to exclude concurrent
57 // execution with more complex methods, such as StreamHandler.publish().
58 // We wouldn't want 'level' to be changed by another thread in the middle
59 // of the execution of a 'publish' call.
60 private volatile Filter filter;
61 private volatile Formatter formatter;
62 private volatile Level logLevel = Level.ALL;
63 private volatile ErrorManager errorManager = new ErrorManager();
64 private volatile String encoding;
65
66 /**
67 * Default constructor. The resulting <tt>Handler</tt> has a log
68 * level of <tt>Level.ALL</tt>, no <tt>Formatter</tt>, and no
69 * <tt>Filter</tt>. A default <tt>ErrorManager</tt> instance is installed
70 * as the <tt>ErrorManager</tt>.
71 */
72 protected Handler() {
73 }
74
75 /**
76 * Publish a <tt>LogRecord</tt>.
77 * <p>
78 * The logging request was made initially to a <tt>Logger</tt> object,
79 * which initialized the <tt>LogRecord</tt> and forwarded it here.
80 * <p>
81 * The <tt>Handler</tt> is responsible for formatting the message, when and
82 * if necessary. The formatting should include localization.
83 *
84 * @param record description of the log event. A null record is
85 * silently ignored and is not published
282 * handler from logging the <tt>LogRecord</tt>. It will return false if
283 * the <tt>LogRecord</tt> is null.
284 * <p>
285 * @param record a <tt>LogRecord</tt>
286 * @return true if the <tt>LogRecord</tt> would be logged.
287 *
288 */
289 public boolean isLoggable(LogRecord record) {
290 final int levelValue = getLevel().intValue();
291 if (record.getLevel().intValue() < levelValue || levelValue == offValue) {
292 return false;
293 }
294 final Filter filter = getFilter();
295 if (filter == null) {
296 return true;
297 }
298 return filter.isLoggable(record);
299 }
300
301 // Package-private support method for security checks.
302 // We check that the caller has appropriate security privileges
303 // to update Handler state and if not throw a SecurityException.
304 void checkPermission() throws SecurityException {
305 manager.checkPermission();
306 }
307 }
|