1
2
3
4
5
6
7 package nl.tudelft.simulation.logger.formatters;
8
9 import java.io.PrintWriter;
10 import java.io.StringWriter;
11 import java.text.DateFormat;
12 import java.util.Date;
13 import java.util.logging.Formatter;
14 import java.util.logging.Level;
15 import java.util.logging.LogRecord;
16
17 /***
18 * A StyledTextFormatter <br>
19 * (c) copyright 2002-2005 <a href="http://www.simulation.tudelft.nl">Delft
20 * University of Technology </a>, the Netherlands. <br>
21 * See for project information <a
22 * href="http://www.simulation.tudelft.nl">www.simulation.tudelft.nl </a> <br>
23 * License of use: <a href="http://www.gnu.org/copyleft/lesser.html">Lesser
24 * General Public License (LGPL) </a>, no warranty.
25 *
26 * @version $Revision: 1.7 $ $Date: 2005/08/04 12:09:01 $
27 * @author <a href="http://www.peter-jacobs.com">Peter Jacobs </a>, <a
28 * href="mailto:nlang@fbk.eur.nl">Niels Lang </a>
29 */
30 public class StyledTextFormatter extends Formatter
31 {
32
33 /*** the DEFAULT_STYLE */
34 public static final String STYLE_DEFAULT = "STYLE_DEFAULT";
35
36 /*** the SOURCE_STYLE */
37 public static final String STYLE_SOURCE = "STYLE_SOURCE";
38
39 /*** the WARNING_STYLE */
40 public static final String STYLE_WARNING = "STYLE_WARNING";
41
42 /*** THE FINE_STYLE */
43 public static final String STYLE_FINE = "STYLE_FINE";
44
45 /*** THE ORIGIN_STYLE */
46 public static final String STYLE_ORIGIN = "STYLE_ORIGIN";
47
48 /*** The separator used */
49 public static final String SEPARATOR = "!!@@!!";
50
51 /*** show the origin */
52 private boolean showOrigin = true;
53
54 /*** a date to use */
55 private Date date = new Date();
56
57 /*** a dateFormatter to use */
58 private DateFormat dateFormatter = DateFormat.getTimeInstance();
59
60 /***
61 * constructs a new StyledTextFormatter
62 *
63 * @param showOrigin whether or not to show the origin
64 */
65 public StyledTextFormatter(final boolean showOrigin)
66 {
67 this.showOrigin = showOrigin;
68 }
69
70 /***
71 * tags a message
72 *
73 * @param tag the tag
74 * @param message the message
75 * @return String
76 */
77 private String tag(final String tag, final String message)
78 {
79 return "<" + tag + ">" + message + "</" + tag + ">"
80 + StyledTextFormatter.SEPARATOR;
81 }
82
83 /***
84 * @see java.util.logging.Formatter#format(java.util.logging.LogRecord)
85 */
86 @Override
87 public String format(final LogRecord record)
88 {
89 StringBuffer message = new StringBuffer();
90 try
91 {
92
93 Level level = record.getLevel();
94 this.date.setTime(record.getMillis());
95 String levelLabel = this.dateFormatter.format(this.date) + " "
96 + level.getName() + ": ";
97 if (level.equals(Level.WARNING) || level.equals(Level.SEVERE))
98 {
99 message.append(this.tag(STYLE_WARNING, levelLabel));
100 } else if (!level.equals(Level.INFO))
101 {
102 message.append(this.tag(STYLE_FINE, levelLabel));
103 }
104 String body = record.getMessage();
105 if (body == null)
106 {
107 body = "null";
108 }
109
110 int sepIndex = body.indexOf(':');
111 if (sepIndex != -1)
112 {
113 String source = body.substring(0, body.indexOf(':'));
114 message.append(this.tag(STYLE_SOURCE, source + " "));
115 body = body.substring(body.indexOf(':'));
116 }
117
118 message.append(this.tag(STYLE_DEFAULT, body + "\n"));
119 if (this.showOrigin)
120 {
121 String sourceName = record.getLoggerName();
122 if (record.getSourceClassName() != null)
123 {
124 sourceName = record.getSourceClassName();
125 }
126 String methodName = "unknown";
127 if (record.getSourceMethodName() != null)
128 {
129 methodName = record.getSourceMethodName();
130 }
131 String originLog = "Origin: " + sourceName + "#" + methodName
132 + "\n";
133 message.append(this.tag(STYLE_ORIGIN, originLog));
134 if (record.getThrown() != null)
135 {
136 try
137 {
138 StringWriter stringWriter = new StringWriter();
139 PrintWriter printWriter = new PrintWriter(stringWriter);
140 record.getThrown().printStackTrace(printWriter);
141 printWriter.close();
142 message.append(this.tag(STYLE_ORIGIN, stringWriter
143 .toString()));
144 } catch (Exception exception)
145 {
146
147 exception = null;
148 }
149 }
150 }
151 message.append(this.tag(STYLE_DEFAULT, "\n"));
152 } catch (Throwable exception)
153 {
154 System.out.println("Logger formatter exception " + record);
155 }
156 return message.toString();
157 }
158 }