KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > util > logging > ConsoleHandler


1 /*
2  * @(#)ConsoleHandler.java 1.12 03/12/19
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8
9 package java.util.logging;
10
11 import java.io.*;
12 import java.net.*;
13
14 /**
15  * This <tt>Handler</tt> publishes log records to <tt>System.err</tt>.
16  * By default the <tt>SimpleFormatter</tt> is used to generate brief summaries.
17  * <p>
18  * <b>Configuration:</b>
19  * By default each <tt>ConsoleHandler</tt> is initialized using the following
20  * <tt>LogManager</tt> configuration properties. If properties are not defined
21  * (or have invalid values) then the specified default values are used.
22  * <ul>
23  * <li> java.util.logging.ConsoleHandler.level
24  * specifies the default level for the <tt>Handler</tt>
25  * (defaults to <tt>Level.INFO</tt>).
26  * <li> java.util.logging.ConsoleHandler.filter
27  * specifies the name of a <tt>Filter</tt> class to use
28  * (defaults to no <tt>Filter</tt>).
29  * <li> java.util.logging.ConsoleHandler.formatter
30  * specifies the name of a <tt>Formatter</tt> class to use
31  * (defaults to <tt>java.util.logging.SimpleFormatter</tt>).
32  * <li> java.util.logging.ConsoleHandler.encoding
33  * the name of the character set encoding to use (defaults to
34  * the default platform encoding).
35  * </ul>
36  * <p>
37  * @version 1.12, 12/19/03
38  * @since 1.4
39  */

40
41 public class ConsoleHandler extends StreamHandler JavaDoc {
42     // Private method to configure a ConsoleHandler from LogManager
43
// properties and/or default values as specified in the class
44
// javadoc.
45
private void configure() {
46         LogManager JavaDoc manager = LogManager.getLogManager();
47     String JavaDoc cname = getClass().getName();
48
49     setLevel(manager.getLevelProperty(cname +".level", Level.INFO));
50     setFilter(manager.getFilterProperty(cname +".filter", null));
51     setFormatter(manager.getFormatterProperty(cname +".formatter", new SimpleFormatter JavaDoc()));
52     try {
53         setEncoding(manager.getStringProperty(cname +".encoding", null));
54     } catch (Exception JavaDoc ex) {
55         try {
56             setEncoding(null);
57         } catch (Exception JavaDoc ex2) {
58         // doing a setEncoding with null should always work.
59
// assert false;
60
}
61     }
62     }
63
64     /**
65      * Create a <tt>ConsoleHandler</tt> for <tt>System.err</tt>.
66      * <p>
67      * The <tt>ConsoleHandler</tt> is configured based on
68      * <tt>LogManager</tt> properties (or their default values).
69      *
70      */

71     public ConsoleHandler() {
72     sealed = false;
73     configure();
74     setOutputStream(System.err);
75     sealed = true;
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      * @param record description of the log event. A null record is
85      * silently ignored and is not published
86      */

87     public void publish(LogRecord JavaDoc record) {
88     super.publish(record);
89     flush();
90     }
91
92     /**
93      * Override <tt>StreamHandler.close</tt> to do a flush but not
94      * to close the output stream. That is, we do <b>not</b>
95      * close <tt>System.err</tt>.
96      */

97     public void close() {
98     flush();
99     }
100 }
101
102
Popular Tags