| 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 | | | import java.text.MessageFormat; |
| 36 | | | import java.text.ParseException; |
| 37 | | | import java.util.ArrayList; |
| 38 | | | import java.util.List; |
| 39 | | | import java.util.logging.Level; |
| 40 | | | import java.util.logging.LogRecord; |
| 41 | | | |
| 42 | | | import org.jcoderz.commons.BusinessImpact; |
| 43 | | | import org.jcoderz.commons.Category; |
| 44 | | | import org.jcoderz.commons.Loggable; |
| 45 | | | import org.jcoderz.commons.LoggableImpl; |
| 46 | | | import org.jcoderz.commons.types.Date; |
| 47 | | | import org.jcoderz.commons.util.Constants; |
| 48 | | | |
| 49 | | | |
| 50 | | | |
| 51 | | | |
| 52 | | | |
| 53 | | | |
| 54 | | | |
| 55 | | | |
| 56 | | | |
| 57 | | | |
| 58 | | (1) | public abstract class BasicLogLineFormat |
| 59 | | | extends LogLineFormat |
| 60 | | | { |
| 61 | | | |
| 62 | | | protected static final int NUMBER_OF_PARAMETERS = 9; |
| 63 | | | |
| 64 | | | private static final int TIMESTAMP_INDEX = 0; |
| 65 | | | private static final int NODEID_INDEX = 1; |
| 66 | | | private static final int INSTANCEID_INDEX = 2; |
| 67 | | | private static final int THREADID_INDEX = 3; |
| 68 | | | private static final int LEVEL_INDEX = 4; |
| 69 | | | private static final int MESSAGEID_INDEX = 5; |
| 70 | | | private static final int BUSINESSIMPACT_INDEX = 6; |
| 71 | | | private static final int CATEGORY_INDEX = 7; |
| 72 | | | private static final int THREAD_NAME_INDEX = 7; |
| 73 | | | private static final int TRACKINGID_INDEX = 8; |
| 74 | | | |
| 75 | | | |
| 76 | | | |
| 77 | | | |
| 78 | | | <ul> |
| 79 | | | <li> |
| 80 | | | <li> |
| 81 | | | <li> |
| 82 | | | <li> |
| 83 | | | <li> |
| 84 | | | <li> |
| 85 | | | <li> |
| 86 | | | <li> |
| 87 | | | <li> |
| 88 | | | <li> |
| 89 | | | </ul> |
| 90 | | | |
| 91 | | | private static final String LOGLINE_FORMAT_PATTERN |
| 92 | | | = "{0} {1} {2} {3} {4} {5} {6} {7} {8}"; |
| 93 | | | |
| 94 | | | private static final String NO_MSG_SYMBOL = "TRACEMSG"; |
| 95 | | | |
| 96 | | | private final int mNumParameters; |
| 97 | | | private final String mLogFormatPattern; |
| 98 | | | |
| 99 | | | |
| 100 | | | |
| 101 | | | |
| 102 | | | |
| 103 | | | @param |
| 104 | | | @param |
| 105 | | | |
| 106 | | | @param |
| 107 | | | |
| 108 | | | protected BasicLogLineFormat ( |
| 109 | | | final LogLineType type, |
| 110 | | | final String additionalPattern, |
| 111 | | | final int numAdditionalParameters) |
| 112 | | | { |
| 113 | 100 | | super(type, new MessageFormat(type.getTypeSpecifier() + " " |
| 114 | | | + LOGLINE_FORMAT_PATTERN + additionalPattern), |
| 115 | | | NUMBER_OF_PARAMETERS + numAdditionalParameters); |
| 116 | | | |
| 117 | 100 | (2)(3) | mNumParameters = NUMBER_OF_PARAMETERS + numAdditionalParameters; |
| 118 | 100 | (4)(5) | mLogFormatPattern = LOGLINE_FORMAT_PATTERN + additionalPattern; |
| 119 | 100 | | } |
| 120 | | | |
| 121 | | | |
| 122 | | | |
| 123 | | | |
| 124 | | | @param |
| 125 | | | <code></code> |
| 126 | | | @param |
| 127 | | | |
| 128 | | | |
| 129 | | | @return |
| 130 | | | |
| 131 | | | |
| 132 | | (6)(7)(8) | protected static List getBasicFormatList ( |
| 133 | | | final DisplayOptions options, |
| 134 | | | final boolean ignoreOptions) |
| 135 | | | { |
| 136 | 100 | | final List formatList = new ArrayList(); |
| 137 | 50 | | if (ignoreOptions || options.displayTimestamp()) |
| 138 | | | { |
| 139 | | | |
| 140 | 100 | | formatList.add(getTimestampFormat()); |
| 141 | | | } |
| 142 | 50 | | if (ignoreOptions || options.displayNodeId()) |
| 143 | | | { |
| 144 | | | |
| 145 | 100 | | formatList.add(getNodeIdFormat()); |
| 146 | | | } |
| 147 | 50 | | if (ignoreOptions || options.displayInstanceId()) |
| 148 | | | { |
| 149 | | | |
| 150 | 100 | | formatList.add(getInstanceIdFormat()); |
| 151 | | | } |
| 152 | 50 | | if (ignoreOptions || options.displayThreadId()) |
| 153 | | | { |
| 154 | | | |
| 155 | 100 | | formatList.add(getThreadIdFormat()); |
| 156 | | | } |
| 157 | 50 | | if (ignoreOptions || options.displayLoggerLevel()) |
| 158 | | | { |
| 159 | | | |
| 160 | 100 | | formatList.add(getLoggerLevelFormat()); |
| 161 | | | } |
| 162 | 50 | | if (ignoreOptions || options.displaySymbolId()) |
| 163 | | | { |
| 164 | | | |
| 165 | 100 | | formatList.add(getMessageSymbolFormat()); |
| 166 | | | } |
| 167 | 50 | | if (ignoreOptions || options.displayBusinessImpact()) |
| 168 | | | { |
| 169 | | | |
| 170 | 100 | | formatList.add(getBusinessImpactFormat()); |
| 171 | | | } |
| 172 | 50 | | if (ignoreOptions || options.displayThreadName()) |
| 173 | | | { |
| 174 | | | |
| 175 | 100 | | formatList.add(getThreadNameFormat()); |
| 176 | | | } |
| 177 | | | |
| 178 | 50 | | if (!ignoreOptions && options.displayCategory()) |
| 179 | | | { |
| 180 | | | |
| 181 | 0 | | formatList.add(getCategoryFormat()); |
| 182 | | | } |
| 183 | 50 | | if (ignoreOptions || options.displayTrackingNumber()) |
| 184 | | | { |
| 185 | | | |
| 186 | 100 | | formatList.add(getTrackingNumberFormat()); |
| 187 | | | } |
| 188 | 100 | | return formatList; |
| 189 | | | } |
| 190 | | | |
| 191 | | | |
| 192 | | | |
| 193 | | | |
| 194 | | | |
| 195 | | | |
| 196 | | | @param |
| 197 | | | @param |
| 198 | | | @param |
| 199 | | | @param |
| 200 | | | |
| 201 | | | |
| 202 | | | protected final void basicFormat ( |
| 203 | | | final StringBuffer sb, |
| 204 | | | final LogRecord record, |
| 205 | | | final Loggable loggable, |
| 206 | | | final List trackingIdSequence) |
| 207 | | | { |
| 208 | 100 | | setLevel(record.getLevel()); |
| 209 | 100 | | setTrackingIds(trackingIdSequence); |
| 210 | | | |
| 211 | 100 | | if (loggable != null) |
| 212 | | | { |
| 213 | 100 | | setTimestamp(new Date(loggable.getEventTime())); |
| 214 | 100 | | setNodeId(loggable.getNodeId()); |
| 215 | 100 | | setInstanceId(loggable.getInstanceId()); |
| 216 | 100 | | setThreadId(loggable.getThreadId()); |
| 217 | 100 | | setMessageId( |
| 218 | | | Integer.toHexString(loggable.getLogMessageInfo().toInt())); |
| 219 | 100 | | setBusinessImpact(loggable.getLogMessageInfo().getBusinessImpact()); |
| 220 | 100 | | setCategory(loggable.getLogMessageInfo().getCategory()); |
| 221 | | | try |
| 222 | | | { |
| 223 | 100 | | setThreadName(loggable.getThreadName()); |
| 224 | | | } |
| 225 | 0 | | catch (AbstractMethodError ex) |
| 226 | | | { |
| 227 | | | |
| 228 | | | |
| 229 | 0 | | setThreadName(Thread.currentThread().getName()); |
| 230 | 50 | | } |
| 231 | | | } |
| 232 | | | else |
| 233 | | | { |
| 234 | 100 | | setTimestamp(new Date(record.getMillis())); |
| 235 | 100 | | setNodeId(LoggableImpl.NODE_ID); |
| 236 | 100 | | setInstanceId(LoggableImpl.INSTANCE_ID); |
| 237 | 100 | | setThreadId(record.getThreadID()); |
| 238 | 100 | | setBusinessImpact(BusinessImpact.NONE); |
| 239 | 100 | | setCategory(Category.TECHNICAL); |
| 240 | | | |
| 241 | 100 | | setThreadName(Thread.currentThread().getName()); |
| 242 | 100 | | setMessageId(NO_MSG_SYMBOL); |
| 243 | | | } |
| 244 | 100 | | format(sb); |
| 245 | 100 | | sb.append(Constants.LINE_SEPARATOR); |
| 246 | 100 | | } |
| 247 | | | |
| 248 | | | |
| 249 | | | |
| 250 | | | |
| 251 | | | |
| 252 | | | |
| 253 | | | @param |
| 254 | | | @param |
| 255 | | | |
| 256 | | | @throws <code></code> |
| 257 | | | |
| 258 | | | @see |
| 259 | | | |
| 260 | | | protected final void basicParse (StringBuffer sb, LogFileEntry entry) |
| 261 | | | throws ParseException |
| 262 | | | { |
| 263 | | | try |
| 264 | | | { |
| 265 | 100 | | parse(sb); |
| 266 | | | |
| 267 | 100 | | entry.setBusinessImpact(getBusinessImpact()); |
| 268 | | | |
| 269 | 100 | | entry.setThreadName(getThreadName()); |
| 270 | 100 | | entry.setInstanceId(getInstanceId()); |
| 271 | 100 | | entry.setLoggerLevel(getLevel()); |
| 272 | 100 | | entry.setNodeId(getNodeId()); |
| 273 | 100 | | entry.setThreadId(getThreadId()); |
| 274 | 100 | | entry.setSymbolId(getMessageId()); |
| 275 | 100 | | entry.setTimestamp(getTimestamp()); |
| 276 | | | |
| 277 | | | |
| 278 | | | |
| 279 | 100 | | final List trackingIds = getTrackingIds(); |
| 280 | 100 | | entry.setTrackingNumber((String) trackingIds.get( |
| 281 | | | trackingIds.size() - 1)); |
| 282 | | | } |
| 283 | 0 | | catch (ParseException pex) |
| 284 | | | { |
| 285 | | | |
| 286 | 0 | | throw pex; |
| 287 | | | } |
| 288 | 0 | | catch (Exception ex) |
| 289 | | | { |
| 290 | 0 | | final ParseException pex = new ParseException( |
| 291 | | | "Got an error parsing " + sb, 0); |
| 292 | 0 | | pex.initCause(ex); |
| 293 | 0 | | throw pex; |
| 294 | 100 | | } |
| 295 | 100 | | } |
| 296 | | | |
| 297 | | | |
| 298 | | | |
| 299 | | | |
| 300 | | | |
| 301 | | | @param |
| 302 | | | |
| 303 | | | protected final void setTimestamp (final Date timestamp) |
| 304 | | | { |
| 305 | 100 | | setParameter(TIMESTAMP_INDEX, timestamp); |
| 306 | 100 | | } |
| 307 | | | |
| 308 | | | |
| 309 | | | |
| 310 | | | |
| 311 | | | @return |
| 312 | | | |
| 313 | | | protected final Date getTimestamp () |
| 314 | | | { |
| 315 | 100 | | return (Date) getParameter(TIMESTAMP_INDEX); |
| 316 | | | } |
| 317 | | | |
| 318 | | | |
| 319 | | | |
| 320 | | | |
| 321 | | | @param |
| 322 | | | |
| 323 | | | protected final void setNodeId (final String nodeId) |
| 324 | | | { |
| 325 | 100 | | setParameter(NODEID_INDEX, nodeId); |
| 326 | 100 | | } |
| 327 | | | |
| 328 | | | |
| 329 | | | |
| 330 | | | |
| 331 | | | @return |
| 332 | | | |
| 333 | | | protected final String getNodeId () |
| 334 | | | { |
| 335 | 100 | | return (String) getParameter(NODEID_INDEX); |
| 336 | | | } |
| 337 | | | |
| 338 | | | |
| 339 | | | |
| 340 | | | |
| 341 | | | @param |
| 342 | | | |
| 343 | | | protected final void setInstanceId (final String instanceId) |
| 344 | | | { |
| 345 | 100 | | setParameter(INSTANCEID_INDEX, instanceId); |
| 346 | 100 | | } |
| 347 | | | |
| 348 | | | |
| 349 | | | |
| 350 | | | |
| 351 | | | @return |
| 352 | | | |
| 353 | | | protected final String getInstanceId () |
| 354 | | | { |
| 355 | 100 | | return (String) getParameter(INSTANCEID_INDEX); |
| 356 | | | } |
| 357 | | | |
| 358 | | | |
| 359 | | | |
| 360 | | | |
| 361 | | | @param |
| 362 | | | |
| 363 | | | protected final void setThreadId (final long threadId) |
| 364 | | | { |
| 365 | 100 | | setParameter(THREADID_INDEX, String.valueOf(threadId)); |
| 366 | 100 | | } |
| 367 | | | |
| 368 | | | |
| 369 | | | |
| 370 | | | |
| 371 | | | @return |
| 372 | | | |
| 373 | | | protected final long getThreadId () |
| 374 | | | { |
| 375 | 100 | | return Long.parseLong((String) getParameter(THREADID_INDEX)); |
| 376 | | | } |
| 377 | | | |
| 378 | | | |
| 379 | | | |
| 380 | | | |
| 381 | | | @param |
| 382 | | | |
| 383 | | | protected final void setThreadName (String threadName) |
| 384 | | | { |
| 385 | 100 | | setParameter(THREAD_NAME_INDEX, threadName); |
| 386 | 100 | | } |
| 387 | | | |
| 388 | | | |
| 389 | | | |
| 390 | | | |
| 391 | | | @return |
| 392 | | | |
| 393 | | | protected final String getThreadName () |
| 394 | | | { |
| 395 | 100 | | return (String) getParameter(THREAD_NAME_INDEX); |
| 396 | | | } |
| 397 | | | |
| 398 | | | |
| 399 | | | |
| 400 | | | |
| 401 | | | @param |
| 402 | | | |
| 403 | | | protected final void setTrackingIds (final List trackingIds) |
| 404 | | | { |
| 405 | 100 | | setParameter(TRACKINGID_INDEX, trackingIds); |
| 406 | 100 | | } |
| 407 | | | |
| 408 | | | |
| 409 | | | |
| 410 | | | |
| 411 | | | @return |
| 412 | | | |
| 413 | | | protected final List getTrackingIds () |
| 414 | | | { |
| 415 | 100 | | return (List) getParameter(TRACKINGID_INDEX); |
| 416 | | | } |
| 417 | | | |
| 418 | | | |
| 419 | | | |
| 420 | | | |
| 421 | | | @param |
| 422 | | | |
| 423 | | | protected final void setLevel (final Level level) |
| 424 | | | { |
| 425 | 100 | | setParameter(LEVEL_INDEX, level.getName()); |
| 426 | 100 | | } |
| 427 | | | |
| 428 | | | |
| 429 | | | |
| 430 | | | |
| 431 | | | @return |
| 432 | | | |
| 433 | | | protected final Level getLevel () |
| 434 | | | { |
| 435 | 100 | | return Level.parse((String) getParameter(LEVEL_INDEX)); |
| 436 | | | } |
| 437 | | | |
| 438 | | | |
| 439 | | | |
| 440 | | | |
| 441 | | | @param |
| 442 | | | |
| 443 | | | protected final void setMessageId (final String messageId) |
| 444 | | | { |
| 445 | 100 | | setParameter(MESSAGEID_INDEX, messageId); |
| 446 | 100 | | } |
| 447 | | | |
| 448 | | | |
| 449 | | | |
| 450 | | | |
| 451 | | | @return |
| 452 | | | |
| 453 | | | protected final String getMessageId () |
| 454 | | | { |
| 455 | 100 | | return (String) getParameter(MESSAGEID_INDEX); |
| 456 | | | } |
| 457 | | | |
| 458 | | | |
| 459 | | | |
| 460 | | | |
| 461 | | | @param |
| 462 | | | |
| 463 | | | protected final void setBusinessImpact (final BusinessImpact impact) |
| 464 | | | { |
| 465 | 100 | | setParameter(BUSINESSIMPACT_INDEX, impact.toString()); |
| 466 | 100 | | } |
| 467 | | | |
| 468 | | | |
| 469 | | | |
| 470 | | | |
| 471 | | | @return |
| 472 | | | |
| 473 | | | protected final BusinessImpact getBusinessImpact () |
| 474 | | | { |
| 475 | 100 | | return BusinessImpact.fromString( |
| 476 | | | (String) getParameter(BUSINESSIMPACT_INDEX)); |
| 477 | | | } |
| 478 | | | |
| 479 | | | |
| 480 | | | |
| 481 | | | |
| 482 | | | @param |
| 483 | | | |
| 484 | | | protected final void setCategory (final Category category) |
| 485 | | | { |
| 486 | 100 | | setParameter(CATEGORY_INDEX, category.toString()); |
| 487 | 100 | | } |
| 488 | | | |
| 489 | | | |
| 490 | | | |
| 491 | | | |
| 492 | | | @return |
| 493 | | | |
| 494 | | | protected final Category getCategory () |
| 495 | | | { |
| 496 | 0 | | return Category.fromString((String) getParameter(CATEGORY_INDEX)); |
| 497 | | | } |
| 498 | | | |
| 499 | | | } |