Skip to content

Commit 8718d7f

Browse files
hansonrhansonr
authored andcommitted
adds java.util.logging package
1 parent fca41d7 commit 8718d7f

18 files changed

+5711
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/*
2+
* Copyright (c) 2000, 2013, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
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+
/**
30+
* This <tt>Handler</tt> publishes log records to <tt>System.err</tt>.
31+
* By default the <tt>SimpleFormatter</tt> is used to generate brief summaries.
32+
* <p>
33+
* <b>Configuration:</b>
34+
* By default each <tt>ConsoleHandler</tt> is initialized using the following
35+
* <tt>LogManager</tt> configuration properties where {@code <handler-name>}
36+
* refers to the fully-qualified class name of the handler.
37+
* If properties are not defined
38+
* (or have invalid values) then the specified default values are used.
39+
* <ul>
40+
* <li> &lt;handler-name&gt;.level
41+
* specifies the default level for the <tt>Handler</tt>
42+
* (defaults to <tt>Level.INFO</tt>). </li>
43+
* <li> &lt;handler-name&gt;.filter
44+
* specifies the name of a <tt>Filter</tt> class to use
45+
* (defaults to no <tt>Filter</tt>). </li>
46+
* <li> &lt;handler-name&gt;.formatter
47+
* specifies the name of a <tt>Formatter</tt> class to use
48+
* (defaults to <tt>java.util.logging.SimpleFormatter</tt>). </li>
49+
* <li> &lt;handler-name&gt;.encoding
50+
* the name of the character set encoding to use (defaults to
51+
* the default platform encoding). </li>
52+
* </ul>
53+
* <p>
54+
* For example, the properties for {@code ConsoleHandler} would be:
55+
* <ul>
56+
* <li> java.util.logging.ConsoleHandler.level=INFO </li>
57+
* <li> java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter </li>
58+
* </ul>
59+
* <p>
60+
* For a custom handler, e.g. com.foo.MyHandler, the properties would be:
61+
* <ul>
62+
* <li> com.foo.MyHandler.level=INFO </li>
63+
* <li> com.foo.MyHandler.formatter=java.util.logging.SimpleFormatter </li>
64+
* </ul>
65+
* <p>
66+
* @since 1.4
67+
*/
68+
public class ConsoleHandler extends StreamHandler {
69+
// Private method to configure a ConsoleHandler from LogManager
70+
// properties and/or default values as specified in the class
71+
// javadoc.
72+
private void configure() {
73+
LogManager manager = LogManager.getLogManager();
74+
String cname = getClass().getName();
75+
76+
setLevel(manager.getLevelProperty(cname +".level", Level.INFO));
77+
setFilter(manager.getFilterProperty(cname +".filter", null));
78+
setFormatter(manager.getFormatterProperty(cname +".formatter", new SimpleFormatter()));
79+
try {
80+
setEncoding(manager.getStringProperty(cname +".encoding", null));
81+
} catch (Exception ex) {
82+
try {
83+
setEncoding(null);
84+
} catch (Exception ex2) {
85+
// doing a setEncoding with null should always work.
86+
// assert false;
87+
}
88+
}
89+
}
90+
91+
/**
92+
* Create a <tt>ConsoleHandler</tt> for <tt>System.err</tt>.
93+
* <p>
94+
* The <tt>ConsoleHandler</tt> is configured based on
95+
* <tt>LogManager</tt> properties (or their default values).
96+
*
97+
*/
98+
public ConsoleHandler() {
99+
sealed = false;
100+
configure();
101+
setOutputStream(System.err);
102+
sealed = true;
103+
}
104+
105+
/**
106+
* Publish a <tt>LogRecord</tt>.
107+
* <p>
108+
* The logging request was made initially to a <tt>Logger</tt> object,
109+
* which initialized the <tt>LogRecord</tt> and forwarded it here.
110+
* <p>
111+
* @param record description of the log event. A null record is
112+
* silently ignored and is not published
113+
*/
114+
@Override
115+
public void publish(LogRecord record) {
116+
super.publish(record);
117+
flush();
118+
}
119+
120+
/**
121+
* Override <tt>StreamHandler.close</tt> to do a flush but not
122+
* to close the output stream. That is, we do <b>not</b>
123+
* close <tt>System.err</tt>.
124+
*/
125+
@Override
126+
public void close() {
127+
flush();
128+
}
129+
}
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
/*
2+
* Copyright (c) 2001, 2004, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
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+
/**
30+
* ErrorManager objects can be attached to Handlers to process
31+
* any error that occurs on a Handler during Logging.
32+
* <p>
33+
* When processing logging output, if a Handler encounters problems
34+
* then rather than throwing an Exception back to the issuer of
35+
* the logging call (who is unlikely to be interested) the Handler
36+
* should call its associated ErrorManager.
37+
*/
38+
39+
public class ErrorManager {
40+
private boolean reported = false;
41+
42+
/*
43+
* We declare standard error codes for important categories of errors.
44+
*/
45+
46+
/**
47+
* GENERIC_FAILURE is used for failure that don't fit
48+
* into one of the other categories.
49+
*/
50+
public final static int GENERIC_FAILURE = 0;
51+
/**
52+
* WRITE_FAILURE is used when a write to an output stream fails.
53+
*/
54+
public final static int WRITE_FAILURE = 1;
55+
/**
56+
* FLUSH_FAILURE is used when a flush to an output stream fails.
57+
*/
58+
public final static int FLUSH_FAILURE = 2;
59+
/**
60+
* CLOSE_FAILURE is used when a close of an output stream fails.
61+
*/
62+
public final static int CLOSE_FAILURE = 3;
63+
/**
64+
* OPEN_FAILURE is used when an open of an output stream fails.
65+
*/
66+
public final static int OPEN_FAILURE = 4;
67+
/**
68+
* FORMAT_FAILURE is used when formatting fails for any reason.
69+
*/
70+
public final static int FORMAT_FAILURE = 5;
71+
72+
/**
73+
* The error method is called when a Handler failure occurs.
74+
* <p>
75+
* This method may be overridden in subclasses. The default
76+
* behavior in this base class is that the first call is
77+
* reported to System.err, and subsequent calls are ignored.
78+
*
79+
* @param msg a descriptive string (may be null)
80+
* @param ex an exception (may be null)
81+
* @param code an error code defined in ErrorManager
82+
*/
83+
public synchronized void error(String msg, Exception ex, int code) {
84+
if (reported) {
85+
// We only report the first error, to avoid clogging
86+
// the screen.
87+
return;
88+
}
89+
reported = true;
90+
String text = "java.util.logging.ErrorManager: " + code;
91+
if (msg != null) {
92+
text = text + ": " + msg;
93+
}
94+
System.err.println(text);
95+
if (ex != null) {
96+
ex.printStackTrace();
97+
}
98+
}
99+
}

0 commit comments

Comments
 (0)