1 /*
2 * Copyright (c) 2003-2008 by Cosylab d. d.
3 *
4 * This file is part of Java-Common.
5 *
6 * Java-Common is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * Java-Common is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with Java-Common. If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 package com.cosylab.logging;
21
22 import java.io.PrintWriter;
23 import java.io.StringWriter;
24
25 import java.text.SimpleDateFormat;
26
27 import java.util.Date;
28 import java.util.logging.Formatter;
29 import java.util.logging.LogRecord;
30
31
32 /**
33 * An implementation of <code>java.util.logging.Formatter</code>. Produces
34 * simple single line log reports meant to go to the console.
35 *
36 * @author Matej Sekoranja (matej.sekoranjaATcosylab.com)
37 */
38 public class ConsoleLogFormatter extends Formatter
39 {
40 /** System property key to enable trace messages. */
41 public static String KEY_TRACE = "TRACE";
42
43 /** Line separator string. */
44 private static boolean showTrace = System.getProperties().containsKey(KEY_TRACE);
45
46 /** Line separator string. */
47 private static String lineSeparator = System.getProperty("line.separator");
48
49 /** ISO 8601 date formatter. */
50 private SimpleDateFormat timeFormatter = new SimpleDateFormat(
51 "yyyy-MM-dd'T'HH:mm:ss.SSS");
52
53 /** Date object (used not to recreate it every time). */
54 private Date date = new Date();
55
56 /**
57 * Format the given LogRecord.
58 *
59 * @param record the log record to be formatted.
60 *
61 * @return a formatted log record
62 */
63 public String format(LogRecord record)
64 {
65 StringBuffer sb = new StringBuffer(128);
66
67 synchronized (date) {
68 date.setTime(record.getMillis());
69
70 //sb.append('(');
71 sb.append(timeFormatter.format(date));
72
73 //sb.append(')');
74 }
75
76 /*
77 if (record.getLoggerName() != null)
78 {
79 sb.append(' ');
80 sb.append('[');
81 sb.append(record.getLoggerName());
82 sb.append(']');
83 }
84 */
85 if (record.getLoggerName() != null
86 || record.getSourceClassName() != null
87 || record.getSourceMethodName() != null) {
88 sb.append(' ');
89 sb.append('[');
90
91 if (record.getLoggerName() != null) {
92 sb.append(record.getLoggerName());
93
94 if (record.getSourceClassName() != null) {
95 sb.append(':');
96 }
97 }
98
99 if (record.getSourceClassName() != null) {
100 sb.append(record.getSourceClassName().substring(record.getSourceClassName()
101 .lastIndexOf('.') + 1));
102 }
103
104 sb.append('#');
105
106 if (record.getSourceMethodName() != null) {
107 sb.append(record.getSourceMethodName());
108 }
109
110 sb.append(']');
111 }
112
113 sb.append(' ');
114 sb.append(record.getMessage());
115
116 //sb.append(' ');
117 //sb.append(record.getLevel().getLocalizedName());
118 // trace
119 /*if (showTrace)
120 {
121 // source
122 sb.append('[');
123 if (record.getSourceClassName() != null)
124 sb.append(record.getSourceClassName());
125
126 // method name
127 if (record.getSourceMethodName() != null)
128 {
129 sb.append('#');
130 sb.append(record.getSourceMethodName());
131 }
132 sb.append(']');
133 }*/
134 sb.append(lineSeparator);
135
136 // exceptions
137 if (showTrace) {
138 if (record.getThrown() != null) {
139 try {
140 StringWriter sw = new StringWriter();
141 PrintWriter pw = new PrintWriter(sw);
142 record.getThrown().printStackTrace(pw);
143 pw.close();
144 sb.append(sw.toString());
145 } catch (Exception ex) {
146 }
147
148 //record.getThrown().printStackTrace();
149 }
150 }
151
152 return new String(sb);
153 }
154 }
155
156 /* __oOo__ */