1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package net.sf.statcvs.util;
25
26 import java.io.PrintWriter;
27 import java.io.StringWriter;
28 import java.util.logging.Formatter;
29 import java.util.logging.Level;
30 import java.util.logging.LogRecord;
31
32 /**
33 * Logging formatter for StatCvs
34 * @author Lukasz Pekacki <lukasz@pekacki.de>
35 * @version $Id: LogFormatter.java,v 1.9 2008/04/02 11:22:15 benoitx Exp $
36 */
37 public class LogFormatter extends Formatter {
38
39 private final String lineSeparator = System.getProperty("line.separator");
40
41 /**
42 * @see java.util.logging.Formatter#format(LogRecord)
43 */
44 public String format(final LogRecord record) {
45 final StringBuffer sb = new StringBuffer();
46 if (record.getLevel().intValue() < Level.INFO.intValue()) {
47 sb.append(record.getLevel().getLocalizedName());
48 sb.append(" ");
49 if (record.getSourceClassName() != null) {
50 String className = record.getSourceClassName();
51 className = className.substring(7);
52 sb.append(className);
53 } else {
54 sb.append(record.getLoggerName());
55 }
56 if (record.getSourceMethodName() != null) {
57 sb.append(" ");
58 sb.append(record.getSourceMethodName());
59 }
60 sb.append("(): ");
61 }
62 final String message = formatMessage(record);
63 sb.append(message);
64 sb.append(lineSeparator);
65 if (record.getThrown() != null) {
66 try {
67 final StringWriter sw = new StringWriter();
68 final PrintWriter pw = new PrintWriter(sw);
69 record.getThrown().printStackTrace(pw);
70 pw.close();
71 sb.append(sw.toString());
72 } catch (final Exception ex) {
73 System.err.println("Error formatting logmessage! " + ex.toString());
74 }
75 }
76 return sb.toString();
77 }
78
79 }