My favorites | Sign in
Project Logo
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
package able.util;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.logging.Formatter;
import java.util.logging.Level;
import java.util.logging.LogRecord;

public class StandardFormatter extends Formatter {
private static final int CLASS_LENGTH = 20;

public String format(LogRecord record) {
try {
StringBuilder sb = new StringBuilder();

String level = "UNKN";
if (record.getLevel().equals(Level.WARNING)) {
level = "WARN";
} else if (record.getLevel().equals(Level.SEVERE)) {
level = "SEVR";
} else if (record.getLevel().equals(Level.INFO)) {
level = "INFO";
} else if (record.getLevel().equals(Level.FINE)) {
level = "FINE";
} else if (record.getLevel().equals(Level.FINEST)) {
level = "FNST";
} else if (record.getLevel().equals(Level.FINER)) {
level = "FINR";
} else if (record.getLevel().equals(Level.CONFIG)) {
level = "CONF";
} else if (record.getLevel().equals(Level.OFF)) {
level = "OFF ";
} else if (record.getLevel().equals(Level.ALL)) {
level = "ALL ";
}

sb.append(level).append(" ");

SimpleDateFormat sdf = new SimpleDateFormat("MM/dd HH:mm:ss");
sb.append(sdf.format(new Date(record.getMillis()))).append(" ");


int before = sb.length();
sb.append(getFormattedClassName(record.getLoggerName()));
int after = sb.length();

for (int i = (after - before); i <= CLASS_LENGTH - 1; i++) {
sb.append(' ');
}

sb.append(" - ");
if (record.getParameters() != null && record.getParameters().length > 0) {
java.util.Formatter formatter = new java.util.Formatter(sb);
formatter.format(record.getMessage(), record.getParameters());
formatter.format("\n");
} else {
sb.append(record.getMessage()).append("\n");
}

if (record.getThrown() != null) {
StringWriter sw = new StringWriter();
record.getThrown().printStackTrace(new PrintWriter(sw));
sb.append(sw.toString());
}

return sb.toString();
} catch (Exception e) {
e.printStackTrace();
return record.getMessage();
}
}

private static String getFormattedClassName(String className) {

StringBuilder sb = new StringBuilder(CLASS_LENGTH);
int before = sb.length();

int classNameLength = className.length();
if (classNameLength > CLASS_LENGTH) {

// First append the first letters of the packages
int index = className.indexOf(".", 0);
if (index != -1) {
sb.append(className.charAt(0));
sb.append('.');
}
while (index != -1) {
int newIndex = className.indexOf(".", index + 1);
if (newIndex != -1) {
sb.append(className.charAt(index + 1));
sb.append('.');
}
index = newIndex;
}
// Now append the formatted class name
String classSimpleName = className.substring(className.lastIndexOf('.') + 1);
int rem = CLASS_LENGTH - (sb.length() - before);
if (classSimpleName.length() > rem) {
classSimpleName = getFormattedSimpleClassName(classSimpleName, rem);
}
sb.append(classSimpleName);
} else {
sb.append(className);
}

return sb.toString();
}

private static String getFormattedSimpleClassName(String classSimpleName, int formattedNameLength) {
final int classSimpleNameLength = classSimpleName.length();
// split the words from the class name
List<String> wordsInClassName = new ArrayList<String>();
List<Integer> wordLengthsInClassName = new ArrayList<Integer>();
int start = 0;
for(int i = 1; i < classSimpleNameLength; i++) {
final char loopChar = classSimpleName.charAt(i);
if (Character.isUpperCase(loopChar) || !Character.isLetter(loopChar)) {
wordsInClassName.add(classSimpleName.substring(start, i));
wordLengthsInClassName.add(i - start);
start = i;
}
}
wordsInClassName.add(classSimpleName.substring(start));
wordLengthsInClassName.add(classSimpleNameLength - start);
final int numberOfWordsInName = wordsInClassName.size();
// Determine the length of shortened word, a value in [2, 4]
final int shortenLength = Math.max(2, Math.min(4, formattedNameLength / numberOfWordsInName));
// now keep adding only three characters until size is sufficient
StringBuilder sb = new StringBuilder(formattedNameLength);
int lengthProcessed = 0, charsRemaining = formattedNameLength;
boolean keepShortening = true;
for (int i = 0; i < numberOfWordsInName && sb.length() < formattedNameLength; i++) {
String currentWord = wordsInClassName.get(i);
int currentWordLength = wordLengthsInClassName.get(i);

if (keepShortening && (classSimpleNameLength - lengthProcessed <= charsRemaining)) {
keepShortening = false;
}

String loopWordToAppend = null;
if (keepShortening && currentWordLength > shortenLength) {
if (i + 1 == numberOfWordsInName) {
// the last word in the name
loopWordToAppend = currentWord.substring(0, charsRemaining - 1);
} else {
int remainingWordsLength = classSimpleNameLength - currentWordLength - lengthProcessed;
int substringLength = Math.max(shortenLength, charsRemaining - remainingWordsLength);
loopWordToAppend = currentWord.substring(0, substringLength);
}
} else {
loopWordToAppend = currentWord;
}
if (i + 1 != numberOfWordsInName && loopWordToAppend.length() >= charsRemaining) {
// there are more words and so we need space for the tilda
sb.append(loopWordToAppend.substring(0, charsRemaining - 1));
break;
}
sb.append(loopWordToAppend);
charsRemaining -= loopWordToAppend.length();

lengthProcessed += currentWordLength;
}
if (keepShortening) {
sb.append('~');
}
return sb.toString();
}

/*
* TODO Just for testing purposes, REMOVE this method
*/
public static void main(String[] args) {
String input = null, result = null;
System.out.printf("%-42s %-22s \n", "Input Class Name", "Result");
System.out.printf("%-42s %-22s \n", "----------------", "------");
input = "SimpleDateFormat";
result = getFormattedClassName(input);
System.out.printf("%-42s %-22s \n", input, result);
input = "java.util.logging.LogRecord";
result = getFormattedClassName(input);
System.out.printf("%-42s %-22s \n", input, result);
input = "java.net.InetSocketAddress";
result = getFormattedClassName(input);
System.out.printf("%-42s %-22s \n", input, result);
input = "able.util.SocketTestWaitCondition";
result = getFormattedClassName(input);
System.out.printf("%-42s %-22s \n", input, result);
input = "org.test.logging.AnotherClassToBeTested";
result = getFormattedClassName(input);
System.out.printf("%-42s %-22s \n", input, result);
input = "org.test.AnotherClassWithLotsOfWordsInName";
result = getFormattedClassName(input);
System.out.printf("%-42s %-22s \n", input, result);
input = Character.UnicodeBlock.class.getName();
result = getFormattedClassName(input);
System.out.printf("%-42s %-22s \n", input, result);

}
}
Show details Hide details

Change log

r18 by plightbo on Feb 21, 2009   Diff
tokenized class names thanks to shams
mahmood
Go to: 
Project members, sign in to write a code review

Older revisions

r6 by plightbo on Nov 05, 2008   Diff
more refactoring
r2 by plightbo on Oct 23, 2008   Diff
initial cut of Able
All revisions of this file

File info

Size: 7313 bytes, 203 lines
Hosted by Google Code