01: /*
02: * This file is part of the QuickServer library
03: * Copyright (C) 2003-2005 QuickServer.org
04: *
05: * Use, modification, copying and distribution of this software is subject to
06: * the terms and conditions of the GNU Lesser General Public License.
07: * You should have received a copy of the GNU LGP License along with this
08: * library; if not, you can download a copy from <http://www.quickserver.org/>.
09: *
10: * For questions, suggestions, bug-reports, enhancement-requests etc.
11: * visit http://www.quickserver.org
12: *
13: */
14:
15: package org.quickserver.util.logging;
16:
17: import java.util.Date;
18: import java.text.SimpleDateFormat;
19: import java.util.logging.*;
20: import java.io.*;
21: import org.quickserver.util.MyString;
22:
23: /**
24: * Formats the LogRecord as "MM-dd-yyyy hh:mm:ss,SSS [LEVEL] - Class.method() - MESSAGE"
25: * @since 1.3.2
26: */
27: public class SimpleTextFormatter extends Formatter {
28: private Date date = new Date();
29: private SimpleDateFormat df = new SimpleDateFormat(
30: "MM-dd-yyyy hh:mm:ss,SSS");
31:
32: private String lineSeparator = (String) java.security.AccessController
33: .doPrivileged(new sun.security.action.GetPropertyAction(
34: "line.separator"));
35:
36: public synchronized String format(LogRecord record) {
37: date.setTime(record.getMillis());
38:
39: StringBuffer sb = new StringBuffer();
40: sb.append(df.format(date));
41: sb.append(" [");
42: sb.append(MyString.alignLeft(record.getLevel()
43: .getLocalizedName(), 7));
44: sb.append("] ");
45: if (record.getSourceClassName() != null) {
46: sb.append(record.getSourceClassName());
47: } else {
48: sb.append(record.getLoggerName());
49: }
50: if (record.getSourceMethodName() != null) {
51: sb.append('.');
52: sb.append(record.getSourceMethodName());
53: }
54: sb.append(" - ");
55: sb.append(formatMessage(record));
56:
57: if (record.getThrown() != null) {
58: sb.append(lineSeparator);
59: sb.append("[StackTrace: ");
60: try {
61: StringWriter sw = new StringWriter();
62: PrintWriter pw = new PrintWriter(sw);
63: record.getThrown().printStackTrace(pw);
64: pw.close();
65: sb.append(sw.toString());
66: } catch (Exception ex) {
67: sb.append(record.getThrown().toString());
68: }
69: sb.append(']');
70: }
71:
72: sb.append(lineSeparator);
73: return sb.toString();
74: }
75: }
|