01: /*
02: * Copyright 2001-2007 Geert Bevin <gbevin[remove] at uwyn dot com>
03: * Distributed under the terms of either:
04: * - the common development and distribution license (CDDL), v1.0; or
05: * - the GNU Lesser General Public License, v2.1 or later
06: * $Id: ConciseLogFormatter.java 3714 2007-04-08 02:57:38Z gbevin $
07: */
08: package com.uwyn.rife.tools;
09:
10: import com.uwyn.rife.config.RifeConfig;
11: import java.io.PrintWriter;
12: import java.io.StringWriter;
13: import java.text.SimpleDateFormat;
14: import java.util.Date;
15: import java.util.logging.Formatter;
16: import java.util.logging.LogRecord;
17:
18: public class ConciseLogFormatter extends Formatter {
19: private static SimpleDateFormat DATE_FORMAT = null;
20:
21: static {
22: DATE_FORMAT = new SimpleDateFormat("yyyyMMdd HHmm");
23: DATE_FORMAT.setTimeZone(RifeConfig.Tools.getDefaultTimeZone());
24: }
25:
26: // Line separator string. This is the value of the line.separator
27: // property at the moment that the ConciseLogFormatter was created.
28: private String lineSeparator = (String) java.security.AccessController
29: .doPrivileged(new java.security.PrivilegedAction() {
30: public Object run() {
31: return System.getProperty("line.separator");
32: }
33: });
34:
35: /**
36: * Format the given LogRecord.
37: * @param record the log record to be formatted.
38: * @return a formatted log record
39: */
40: public String format(LogRecord record) {
41: StringBuilder sb = new StringBuilder();
42: String message = formatMessage(record);
43: sb.append(DATE_FORMAT.format(new Date()));
44: sb.append(" ");
45: sb.append(record.getLevel());
46: sb.append(" ");
47: sb.append(message);
48: sb.append(lineSeparator);
49: if (record.getThrown() != null) {
50: try {
51: StringWriter sw = new StringWriter();
52: PrintWriter pw = new PrintWriter(sw);
53: record.getThrown().printStackTrace(pw);
54: pw.close();
55: sb.append(sw.toString());
56: } catch (Exception ex) {
57: }
58: }
59: return sb.toString();
60: }
61: }
|