01: /*
02: * Licensed to the Apache Software Foundation (ASF) under one or more
03: * contributor license agreements. See the NOTICE file distributed with
04: * this work for additional information regarding copyright ownership.
05: * The ASF licenses this file to You under the Apache License, Version 2.0
06: * (the "License"); you may not use this file except in compliance with
07: * the License. You may obtain a copy of the License at
08: *
09: * http://www.apache.org/licenses/LICENSE-2.0
10: *
11: * Unless required by applicable law or agreed to in writing, software
12: * distributed under the License is distributed on an "AS IS" BASIS,
13: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14: * See the License for the specific language governing permissions and
15: * limitations under the License.
16: */
17:
18: package java.util.logging;
19:
20: import java.io.PrintWriter;
21: import java.io.StringWriter;
22: import java.text.MessageFormat;
23: import java.util.Date;
24:
25: /**
26: * <code>SimpleFormatter</code> can be used to print a summary of the
27: * information contained in a <code>LogRecord</code> object in a human
28: * readable format.
29: */
30: public class SimpleFormatter extends Formatter {
31: /**
32: * Constructs a <code>SimpleFormatter</code> object.
33: */
34: public SimpleFormatter() {
35: super ();
36: }
37:
38: @Override
39: public String format(LogRecord r) {
40: StringBuilder sb = new StringBuilder();
41: sb.append(MessageFormat.format("{0, date} {0, time} ", //$NON-NLS-1$
42: new Object[] { new Date(r.getMillis()) }));
43: sb.append(r.getSourceClassName()).append(" "); //$NON-NLS-1$
44: sb.append(r.getSourceMethodName()).append(
45: LogManager.getSystemLineSeparator());
46: sb.append(r.getLevel().getName()).append(": "); //$NON-NLS-1$
47: sb.append(formatMessage(r)).append(
48: LogManager.getSystemLineSeparator());
49: if (null != r.getThrown()) {
50: sb.append("Throwable occurred: "); //$NON-NLS-1$
51: Throwable t = r.getThrown();
52: PrintWriter pw = null;
53: try {
54: StringWriter sw = new StringWriter();
55: pw = new PrintWriter(sw);
56: t.printStackTrace(pw);
57: sb.append(sw.toString());
58: } finally {
59: if (pw != null) {
60: try {
61: pw.close();
62: } catch (Exception e) {
63: // ignore
64: }
65: }
66: }
67: }
68: return sb.toString();
69: }
70: }
|