| 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 | | | package org.jcoderz.commons.logging; |
| 34 | | | |
| 35 | | | |
| 36 | | | import java.text.Format; |
| 37 | | | import java.text.ParseException; |
| 38 | | | import java.util.ArrayList; |
| 39 | | | import java.util.Arrays; |
| 40 | | | import java.util.Iterator; |
| 41 | | | import java.util.List; |
| 42 | | | import java.util.logging.LogRecord; |
| 43 | | | |
| 44 | | | import org.jcoderz.commons.Loggable; |
| 45 | | | import org.jcoderz.commons.util.StringUtil; |
| 46 | | | |
| 47 | | | |
| 48 | | | |
| 49 | | | |
| 50 | | | |
| 51 | | | |
| 52 | | (1) | public class ParameterLineFormat |
| 53 | | | extends ContinuationLineFormat |
| 54 | | | { |
| 55 | | | |
| 56 | | | |
| 57 | | | |
| 58 | | | private static final int NUMBER_OF_ADDITIONAL_PARAMETERS = 2; |
| 59 | | | |
| 60 | | | private static final int PARAMETER_NAME_INDEX = NUMBER_OF_PARAMETERS; |
| 61 | | | private static final int PARAMETER_VALUE_INDEX = PARAMETER_NAME_INDEX + 1; |
| 62 | | | |
| 63 | | | |
| 64 | | | |
| 65 | | | |
| 66 | | | private static final String ADDITIONAL_LOGLINE_FORMAT_PATTERN |
| 67 | | | = " {" + PARAMETER_NAME_INDEX + "}: \t{" + PARAMETER_VALUE_INDEX + "}"; |
| 68 | | | |
| 69 | | | |
| 70 | | | |
| 71 | | | private static final String SYMBOL_TAG = LogItem.INTERNAL_PARAMETER_PREFIX |
| 72 | | | + "SYMBOL_"; |
| 73 | | | |
| 74 | | | |
| 75 | | | |
| 76 | | | private static final String SOURCE_TAG = LogItem.INTERNAL_PARAMETER_PREFIX |
| 77 | | | + "LOGGED_AT_"; |
| 78 | | | |
| 79 | | | |
| 80 | | | |
| 81 | | | private static final String SOLUTION_TAG = LogItem.INTERNAL_PARAMETER_PREFIX |
| 82 | | | + "SOLUTION_"; |
| 83 | | | |
| 84 | | | |
| 85 | | | |
| 86 | | | |
| 87 | | | public ParameterLineFormat () |
| 88 | | | { |
| 89 | 100 | | super(LogLineFormat.PARAMETER_LINE, ADDITIONAL_LOGLINE_FORMAT_PATTERN, |
| 90 | | | NUMBER_OF_ADDITIONAL_PARAMETERS); |
| 91 | 100 | | setFormats(getFormatList(null, true)); |
| 92 | 100 | | } |
| 93 | | | |
| 94 | | | |
| 95 | | | |
| 96 | | | |
| 97 | | | @param |
| 98 | | | <code></code> |
| 99 | | | @param |
| 100 | | | |
| 101 | | | |
| 102 | | | @return |
| 103 | | | |
| 104 | | | |
| 105 | | | static final Format[] getFormatList ( |
| 106 | | | final DisplayOptions options, |
| 107 | | | final boolean ignoreOptions) |
| 108 | | | { |
| 109 | 100 | | final List formatList = new ArrayList(); |
| 110 | 50 | | if (ignoreOptions || options.displayThreadId()) |
| 111 | | | { |
| 112 | | | |
| 113 | 100 | | formatList.add(getThreadIdFormat()); |
| 114 | | | } |
| 115 | 50 | | if (ignoreOptions || options.displayTrackingNumber()) |
| 116 | | | { |
| 117 | | | |
| 118 | 100 | | formatList.add(getTrackingNumberFormat()); |
| 119 | | | } |
| 120 | | | |
| 121 | 100 | | formatList.add(new AsItIsFormat(": \t")); |
| 122 | | | |
| 123 | | | |
| 124 | 100 | | formatList.add(new WhitespaceFormat( |
| 125 | | | new CollectionFormat(new StringEscapeFormat(",]")))); |
| 126 | 100 | | return (Format[]) formatList.toArray(EMPTY_FORMATTERS); |
| 127 | | | } |
| 128 | | | |
| 129 | | | |
| 130 | | | |
| 131 | | | |
| 132 | | | |
| 133 | | | |
| 134 | | | |
| 135 | | | |
| 136 | | | @param |
| 137 | | | @param |
| 138 | | | @param |
| 139 | | | @param |
| 140 | | | @param |
| 141 | | | @param |
| 142 | | | |
| 143 | | | public void format ( |
| 144 | | | final StringBuffer sb, |
| 145 | | | final LogRecord record, |
| 146 | | | final Loggable loggable, |
| 147 | | | final List trackingIdSequence, |
| 148 | | | final Throwable thrown, |
| 149 | | | final Object parameter) |
| 150 | | | { |
| 151 | 50 | | if ((parameter != null) && ! (parameter instanceof Throwable)) |
| 152 | | | { |
| 153 | 0 | | throw new IllegalArgumentException( |
| 154 | | | "Parameter must be null or a Throwable, but is: " + parameter); |
| 155 | | | } |
| 156 | 100 | | if (!StringUtil.isEmptyOrNull(loggable.getLogMessageInfo().getSymbol())) |
| 157 | | | { |
| 158 | 100 | | setParameterName(SYMBOL_TAG); |
| 159 | 100 | | setParameterValues(Arrays.asList( |
| 160 | | | new String[]{loggable.getLogMessageInfo().getSymbol()})); |
| 161 | 100 | | basicFormat(sb, record, loggable, trackingIdSequence); |
| 162 | | | } |
| 163 | | | |
| 164 | 100 | | final String solution = loggable.getLogMessageInfo().getSolution(); |
| 165 | 100 | | if (!StringUtil.isEmptyOrNull(solution)) |
| 166 | | | { |
| 167 | 100 | | setParameterName(SOLUTION_TAG); |
| 168 | 100 | | setParameterValues(Arrays.asList( |
| 169 | | | new String[]{solution})); |
| 170 | 100 | | basicFormat(sb, record, loggable, trackingIdSequence); |
| 171 | | | } |
| 172 | | | |
| 173 | | | |
| 174 | 100 | | final String location = getLogLocation(record); |
| 175 | 100 | | if (!".".equals(location)) |
| 176 | | | { |
| 177 | 100 | | setParameterName(SOURCE_TAG); |
| 178 | 100 | | setParameterValues(Arrays.asList(new String[]{location})); |
| 179 | 100 | | basicFormat(sb, record, loggable, trackingIdSequence); |
| 180 | | | } |
| 181 | | | |
| 182 | 100 | | appendParameters(sb, record, loggable, trackingIdSequence); |
| 183 | 100 | | } |
| 184 | | | |
| 185 | | | |
| 186 | | | |
| 187 | | | |
| 188 | | | |
| 189 | | | @param |
| 190 | | | |
| 191 | | | @param |
| 192 | | | |
| 193 | | | @throws |
| 194 | | | |
| 195 | | | public void parse (StringBuffer sb, LogFileEntry entry) |
| 196 | | | throws ParseException |
| 197 | | | { |
| 198 | | | try |
| 199 | | | { |
| 200 | 0 | | basicParse(sb, entry); |
| 201 | 0 | (2) | if (getParameterName().equals(SOURCE_TAG)) |
| 202 | | | { |
| 203 | 0 | | final String[] source |
| 204 | | | = getLogSource((String) getParameterValues().get(0)); |
| 205 | 0 | | entry.setSourceClass(source[SOURCECLASS_INDEX]); |
| 206 | 0 | (3) | entry.setSourceMethod(source[SOURCEMETHOD_INDEX]); |
| 207 | 0 | | } |
| 208 | 0 | (4) | else if (getParameterName().equals(SOLUTION_TAG) |
| 209 | | | && (getParameterValues() != null) |
| 210 | | | && ! getParameterValues().isEmpty()) |
| 211 | | | { |
| 212 | 0 | | entry.setSolution((String) getParameterValues().get(0)); |
| 213 | | | } |
| 214 | 0 | (5) | else if (getParameterName().equals(SYMBOL_TAG)) |
| 215 | | | { |
| 216 | 0 | | entry.setSymbol((String) getParameterValues().get(0)); |
| 217 | | | } |
| 218 | | | else |
| 219 | | | { |
| 220 | 0 | | entry.addToParameters(getParameterName(), getParameterValues()); |
| 221 | | | } |
| 222 | | | } |
| 223 | 0 | | catch (ParseException pex) |
| 224 | | | { |
| 225 | | | |
| 226 | 0 | | throw pex; |
| 227 | | | } |
| 228 | 0 | | catch (Exception ex) |
| 229 | | | { |
| 230 | 0 | | final ParseException pex = new ParseException( |
| 231 | | | "Got an error parsing " + sb, 0); |
| 232 | 0 | | pex.initCause(ex); |
| 233 | 0 | | throw pex; |
| 234 | 0 | | } |
| 235 | 0 | | } |
| 236 | | | |
| 237 | | | private void appendParameters ( |
| 238 | | | final StringBuffer sb, |
| 239 | | | final LogRecord record, |
| 240 | | | final Loggable loggable, |
| 241 | | | final List trackingIds) |
| 242 | | | { |
| 243 | 100 | | final java.util.Set namesUnsorted |
| 244 | | | = loggable.getParameterNames(); |
| 245 | 100 | | final String[] names |
| 246 | | | = (String[]) namesUnsorted.toArray( |
| 247 | | | new String[namesUnsorted.size()]); |
| 248 | 100 | | Arrays.sort(names); |
| 249 | | | |
| 250 | 100 | | for (final Iterator nameIter = Arrays.asList(names).iterator(); |
| 251 | 100 | | nameIter.hasNext(); ) |
| 252 | | | { |
| 253 | 100 | | final String name = (String) nameIter.next(); |
| 254 | 100 | | if (! name.startsWith(LogItem.INTERNAL_PARAMETER_PREFIX)) |
| 255 | | | { |
| 256 | 100 | | setParameterName(name); |
| 257 | 100 | | setParameterValues(loggable.getParameter(name)); |
| 258 | 100 | | basicFormat(sb, record, loggable, trackingIds); |
| 259 | | | } |
| 260 | 100 | | } |
| 261 | 100 | | } |
| 262 | | | |
| 263 | | | private void setParameterName (final String name) |
| 264 | | | { |
| 265 | 100 | | setParameter(PARAMETER_NAME_INDEX, name); |
| 266 | 100 | | } |
| 267 | | | |
| 268 | | | private String getParameterName () |
| 269 | | | { |
| 270 | 0 | | return (String) getParameter(PARAMETER_NAME_INDEX); |
| 271 | | | } |
| 272 | | | |
| 273 | | | private void setParameterValues (final List values) |
| 274 | | | { |
| 275 | 100 | | setParameter(PARAMETER_VALUE_INDEX, values); |
| 276 | 100 | | } |
| 277 | | | |
| 278 | | | private List getParameterValues () |
| 279 | | | { |
| 280 | 0 | | return (List) getParameter(PARAMETER_VALUE_INDEX); |
| 281 | | | } |
| 282 | | | |
| 283 | | | |
| 284 | | | |
| 285 | | | |
| 286 | | | @param |
| 287 | | | |
| 288 | | | @return |
| 289 | | | |
| 290 | | | private String getLogLocation (final LogRecord record) |
| 291 | | | { |
| 292 | 100 | | return record.getSourceClassName() + "." + record.getSourceMethodName(); |
| 293 | | | } |
| 294 | | | } |