01: /**
02: * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE, version 2.1, dated February 1999.
03: *
04: * This program is free software; you can redistribute it and/or modify
05: * it under the terms of the latest version of the GNU Lesser General
06: * Public License as published by the Free Software Foundation;
07: *
08: * This program 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
11: * GNU Lesser General Public License for more details.
12: *
13: * You should have received a copy of the GNU Lesser General Public License
14: * along with this program (LICENSE.txt); if not, write to the Free Software
15: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16: */package org.jamwiki.utils;
17:
18: import java.io.PrintWriter;
19: import java.io.StringWriter;
20: import java.text.SimpleDateFormat;
21: import java.util.Date;
22: import java.util.logging.Formatter;
23: import java.util.logging.LogRecord;
24:
25: /**
26: * This class extends the {@link java.util.logging.Formatter} class to format
27: * log messages.
28: *
29: * @see org.jamwiki.utils.WikiLogger
30: */
31: public class WikiLogFormatter extends Formatter {
32:
33: private static final String SEPARATOR = System
34: .getProperty("line.separator");
35: private final String datePattern;
36:
37: /**
38: *
39: */
40: public WikiLogFormatter(String datePattern) {
41: super ();
42: this .datePattern = datePattern;
43: }
44:
45: /**
46: *
47: */
48: public String format(LogRecord record) {
49: StringBuffer buffer = new StringBuffer();
50: SimpleDateFormat formatter = new SimpleDateFormat(datePattern);
51: buffer.append(formatter.format(new Date(record.getMillis())));
52: buffer.append(' ');
53: buffer.append(record.getLevel().getName());
54: buffer.append(": ");
55: buffer.append(record.getLoggerName());
56: buffer.append(" - ");
57: buffer.append(formatMessage(record));
58: buffer.append(SEPARATOR);
59: Throwable throwable = record.getThrown();
60: if (throwable != null) {
61: StringWriter sw = new StringWriter();
62: throwable.printStackTrace(new PrintWriter(sw, true));
63: buffer.append(sw.toString());
64: }
65: return buffer.toString();
66: }
67: }
|