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 package com.mindtree.techworks.insight.preferences.util;
26
27 import java.util.HashMap;
28 import java.util.StringTokenizer;
29
30 import com.mindtree.techworks.insight.InsightConstants;
31 import com.mindtree.techworks.insight.gui.widgets.StatusBar;
32 import com.mindtree.techworks.insight.model.ReceiverFormat;
33 import com.mindtree.techworks.insight.preferences.PreferenceManager;
34 import com.mindtree.techworks.insight.preferences.model.Preference;
35 import com.mindtree.techworks.insight.preferences.model.PreferenceAttribute;
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 public class Log4JPatternInterpeter {
66
67
68
69
70 private static final String PREFERENCE_ID = "log4jPattern";
71
72
73
74
75 private static final String[] LOGGING_PATTERNS = {"primaryPattern","secondaryPattern"};
76
77
78
79
80
81 private static HashMap PATTERN_MATCH = new HashMap();
82 static {
83 PATTERN_MATCH.put("c", "LOGGER");
84 PATTERN_MATCH.put("C", "CLASS");
85 PATTERN_MATCH.put("d", "TIMESTAMP");
86 PATTERN_MATCH.put("l", "FILE");
87 PATTERN_MATCH.put("F", "FILE");
88 PATTERN_MATCH.put("L", "LINE");
89 PATTERN_MATCH.put("m", "MESSAGE");
90 PATTERN_MATCH.put("M", "METHOD");
91 PATTERN_MATCH.put("p", "LEVEL");
92 PATTERN_MATCH.put("r", "RELATIVETIME");
93 PATTERN_MATCH.put("t", "THREAD");
94 PATTERN_MATCH.put("x", "NDC");
95
96 PATTERN_MATCH.put("n", "");
97 }
98
99
100
101
102 private static final String [] RECOGNIZED_PATTERNS = {
103 "c",
104 "C",
105 "d",
106 "l",
107 "F",
108 "L",
109 "m",
110 "M",
111 "p",
112 "r",
113 "t",
114 "x",
115 "n",
116 };
117
118
119
120
121
122 private static final int TIMESTAMP_INDEX = 2;
123
124
125
126
127 private static final String PERCENT = "%";
128
129
130
131
132 private static final char BRACKET_START = '{';
133
134
135
136
137 private static final char BRACKET_END = '}';
138
139
140
141
142
143
144
145
146
147
148
149
150 public static ReceiverFormat getInterpretedFormat (String log4jPattern) {
151
152 StringBuffer interpretedPattern = new StringBuffer();
153 String timeFormat = null;
154 StringTokenizer tokenizer = new StringTokenizer(log4jPattern, PERCENT);
155 try {
156 while (tokenizer.hasMoreTokens()) {
157 String token = tokenizer.nextToken();
158 for (int i = 0; i < RECOGNIZED_PATTERNS.length; i++ ) {
159 int keyCharIndex = token.indexOf(RECOGNIZED_PATTERNS[i]);
160 if (keyCharIndex > -1) {
161 token = PATTERN_MATCH.get(RECOGNIZED_PATTERNS[i])
162 + token.substring(keyCharIndex + 1, token
163 .length());
164 int bracketIndex = token.indexOf(BRACKET_START);
165 if (bracketIndex > -1) {
166 if (i == TIMESTAMP_INDEX) {
167
168 timeFormat = token.substring(bracketIndex + 1,
169 token.indexOf(BRACKET_END));
170 }
171 token = token.substring(0, bracketIndex)
172 + token.substring(token
173 .indexOf(BRACKET_END) + 1, token
174 .length());
175 }
176 break;
177 }
178 }
179 interpretedPattern.append(token);
180 }
181 } catch (Exception e) {
182
183 e.printStackTrace();
184 StatusBar.getInstance().setDisplayText(0,
185 InsightConstants.getLiteral("ERROR_LOG4J_PATTERN"), false);
186 return new ReceiverFormat("", "", "");
187 }
188 return new ReceiverFormat(log4jPattern, interpretedPattern.toString()
189 .trim(), timeFormat);
190 }
191
192
193
194
195
196
197 public static ReceiverFormat[] getInterpretedRecieverFormat () {
198
199 Preference preference = PreferenceManager.getInstance().getPreference(
200 PREFERENCE_ID);
201 ReceiverFormat[] receiverFormat = new ReceiverFormat[LOGGING_PATTERNS.length];
202
203
204
205 for(int i=0; i<LOGGING_PATTERNS.length; i++){
206 PreferenceAttribute preferenceAttribute = preference.getPreferenceAttributeById(LOGGING_PATTERNS[i]);
207 receiverFormat[i] = getInterpretedFormat(preferenceAttribute.getValue());
208 }
209 return receiverFormat;
210 }
211
212
213
214
215
216
217 public static void main (String [] args) {
218
219
220
221
222
223
224
225 System.out.println(Log4JPatternInterpeter
226 .getInterpretedFormat("####<%d{MMM dd, yyyy hh:mm:ss a z}> %m%n"));
227
228 }
229
230 }