01: /*
02: * Copyright 2001-2006 C:1 Financial Services GmbH
03: *
04: * This software is free software; you can redistribute it and/or
05: * modify it under the terms of the GNU Lesser General Public
06: * License Version 2.1, as published by the Free Software Foundation.
07: *
08: * This software is distributed in the hope that it will be useful,
09: * but WITHOUT ANY WARRANTY; without even the implied warranty of
10: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11: * Lesser General Public License for more details.
12: *
13: * You should have received a copy of the GNU Lesser General Public
14: * License along with this library; if not, write to the Free Software
15: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA
16: */
17:
18: package de.finix.contelligent.client.util;
19:
20: import java.io.PrintWriter;
21: import java.io.StringWriter;
22: import java.text.SimpleDateFormat;
23: import java.util.Date;
24: import java.util.logging.LogRecord;
25:
26: public class LogfileFormatter extends java.util.logging.Formatter {
27:
28: private SimpleDateFormat dateFormatter;
29:
30: /** Creates a new instance of LogfileFormatter */
31: public LogfileFormatter(String dateFormatPattern) {
32: dateFormatter = new SimpleDateFormat(dateFormatPattern);
33: }
34:
35: /**
36: * Format the given log record and return the formatted string.
37: * <p>
38: * The resulting formatted String will normally include a localized and
39: * formated version of the LogRecord's message field. The
40: * Formatter.formatMessage convenience method can (optionally) be used to
41: * localize and format the message field.
42: *
43: * @param record
44: * the log record to be formatted.
45: * @return the formatted log record
46: */
47: public String format(LogRecord record) {
48: StringBuffer message = new StringBuffer();
49: Date logDate = new Date(record.getMillis());
50: message.append(record.getSequenceNumber());
51: message.append(" - ");
52: message.append(dateFormatter.format(logDate));
53: message.append(", ");
54: message.append(record.getLevel().getLocalizedName());
55: message.append(" [Thread: ");
56: message.append(Thread.currentThread().getName());
57: message.append("] [Class: ");
58: message.append(record.getSourceClassName());
59: message.append("] - [Method: ");
60: message.append(record.getSourceMethodName());
61: message.append("]:\n");
62: message.append(formatMessage(record));
63: message.append("\n");
64: if (record.getThrown() != null) {
65: try {
66: StringWriter sw = new StringWriter();
67: PrintWriter pw = new PrintWriter(sw);
68: record.getThrown().printStackTrace(pw);
69: pw.close();
70: message.append(sw.toString());
71: } catch (Exception ex) {
72: }
73: }
74: return message.toString();
75: }
76: }
|