| 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; |
| 34 | | | |
| 35 | | | |
| 36 | | | import java.util.ArrayList; |
| 37 | | | import java.util.HashMap; |
| 38 | | | import java.util.List; |
| 39 | | | import java.util.Map; |
| 40 | | | import java.util.logging.Formatter; |
| 41 | | | import java.util.logging.LogRecord; |
| 42 | | | import java.util.logging.Logger; |
| 43 | | | |
| 44 | | | import org.jcoderz.commons.logging.LogLineFormat; |
| 45 | | | import org.jcoderz.commons.logging.LogLineFormatFactory; |
| 46 | | | |
| 47 | | | |
| 48 | | | |
| 49 | | | |
| 50 | | | {@link } |
| 51 | | | {@link } |
| 52 | | | |
| 53 | | | |
| 54 | 100 | (1) | public class LogFormatter |
| 55 | | | extends Formatter |
| 56 | | | { |
| 57 | | | |
| 58 | | | |
| 59 | | | |
| 60 | | | |
| 61 | | | public static final String MSG_LOGGER_STACK_TRACE = "msgLoggerStackTrace"; |
| 62 | 100 | | private static final Logger FWK_TRACE_LOGGER_LOGGER |
| 63 | | | = Logger.getLogger(MSG_LOGGER_STACK_TRACE); |
| 64 | 100 | | private final ThreadLocal mMessageFormatters = new ThreadLocal(); |
| 65 | | | |
| 66 | | | {@inheritDoc} |
| 67 | | | public String format (LogRecord record) |
| 68 | | | { |
| 69 | 100 | | final StringBuffer sb = new StringBuffer(); |
| 70 | 100 | | Loggable loggable = null; |
| 71 | 100 | | if (record.getParameters() != null && record.getParameters().length > 0) |
| 72 | | | { |
| 73 | 100 | | if (record.getParameters()[0] instanceof Loggable) |
| 74 | | | { |
| 75 | 100 | | loggable = (Loggable) record.getParameters()[0]; |
| 76 | | | } |
| 77 | | | } |
| 78 | 100 | | format(sb, record, loggable); |
| 79 | 100 | | return sb.toString(); |
| 80 | | | } |
| 81 | | | |
| 82 | | | |
| 83 | | | <code></code> |
| 84 | | | |
| 85 | | | |
| 86 | | | @param |
| 87 | | | |
| 88 | | | @return<code></code> |
| 89 | | | |
| 90 | | | private LogLineFormat getMessageFormat ( |
| 91 | | | final LogLineFormat.LogLineType type) |
| 92 | | | { |
| 93 | 100 | | Map formatters = (Map) mMessageFormatters.get(); |
| 94 | 100 | | if (formatters == null) |
| 95 | | | { |
| 96 | 100 | | formatters = createMessageFormats(); |
| 97 | 100 | | mMessageFormatters.set(formatters); |
| 98 | | | } |
| 99 | 100 | | return (LogLineFormat) formatters.get(type); |
| 100 | | | } |
| 101 | | | |
| 102 | | | private Map createMessageFormats () |
| 103 | | | { |
| 104 | 100 | | final Map rc = new HashMap(); |
| 105 | | | |
| 106 | 100 | | addMessageFormat(rc, LogLineFormat.TRACE_MESSAGE); |
| 107 | 100 | | addMessageFormat(rc, LogLineFormat.EXCEPTION_MESSAGE); |
| 108 | 100 | | addMessageFormat(rc, LogLineFormat.LOG_MESSAGE); |
| 109 | 100 | | addMessageFormat(rc, LogLineFormat.ERROR_MESSAGE); |
| 110 | 100 | | addMessageFormat(rc, LogLineFormat.STACKTRACE_MESSAGE); |
| 111 | 100 | | addMessageFormat(rc, LogLineFormat.PARAMETER_LINE); |
| 112 | 100 | | addMessageFormat(rc, LogLineFormat.NESTED_MESSAGE); |
| 113 | | | |
| 114 | 100 | | return rc; |
| 115 | | | } |
| 116 | | | |
| 117 | | | |
| 118 | | | |
| 119 | | | |
| 120 | | | |
| 121 | | | @param |
| 122 | | | @param |
| 123 | | | @param |
| 124 | | | |
| 125 | | | |
| 126 | | | private void formatLogRecord ( |
| 127 | | | final StringBuffer sb, |
| 128 | | | final LogRecord record, |
| 129 | | | final List trackingIdSequence) |
| 130 | | | { |
| 131 | | | LogLineFormat.LogLineType type; |
| 132 | 100 | | if (record.getThrown() != null) |
| 133 | | | { |
| 134 | 100 | | type = LogLineFormat.EXCEPTION_MESSAGE; |
| 135 | | | } |
| 136 | | | else |
| 137 | | | { |
| 138 | 100 | | type = LogLineFormat.TRACE_MESSAGE; |
| 139 | | | } |
| 140 | 100 | | final LogLineFormat format = getMessageFormat(type); |
| 141 | 100 | | format.format(sb, record, null, trackingIdSequence, null, null); |
| 142 | 100 | | } |
| 143 | | | |
| 144 | | | |
| 145 | | | |
| 146 | | | |
| 147 | | | |
| 148 | | | |
| 149 | | | |
| 150 | | | @param |
| 151 | | | @param |
| 152 | | | @param |
| 153 | | | @param |
| 154 | | | |
| 155 | | | |
| 156 | | | private void appendStackTrace ( |
| 157 | | | final StringBuffer sb, |
| 158 | | | final LogRecord record, |
| 159 | | | final Loggable loggable, |
| 160 | | | final List trackingIdSequence) |
| 161 | | | { |
| 162 | 100 | | Throwable thrown = getTopLevelThrown(record, loggable); |
| 163 | 100 | | Throwable outerTrace = null; |
| 164 | 100 | | final LogLineFormat.LogLineType type = LogLineFormat.STACKTRACE_MESSAGE; |
| 165 | 100 | | final LogLineFormat format = getMessageFormat(type); |
| 166 | 100 | | while (thrown != null) |
| 167 | | | { |
| 168 | 100 | | if (thrown instanceof Loggable) |
| 169 | | | { |
| 170 | 100 | | addTrackingNumber(trackingIdSequence, (Loggable) thrown); |
| 171 | | | } |
| 172 | 100 | | format.format(sb, record, loggable, trackingIdSequence, |
| 173 | | | thrown, outerTrace); |
| 174 | 100 | | outerTrace = thrown; |
| 175 | 100 | | thrown = outerTrace.getCause(); |
| 176 | | | } |
| 177 | 100 | | } |
| 178 | | | |
| 179 | | | |
| 180 | | | |
| 181 | | | |
| 182 | | | |
| 183 | | | @param |
| 184 | | | @param |
| 185 | | | @param |
| 186 | | | |
| 187 | | | |
| 188 | | | private void appendParameters ( |
| 189 | | | final StringBuffer sb, |
| 190 | | | final LogRecord record, |
| 191 | | | final Loggable loggable, |
| 192 | | | final List trackingIdSequence) |
| 193 | | | { |
| 194 | 100 | | final LogLineFormat.LogLineType type = LogLineFormat.PARAMETER_LINE; |
| 195 | 100 | | final LogLineFormat format = getMessageFormat(type); |
| 196 | 100 | | format.format(sb, record, loggable, trackingIdSequence, null, null); |
| 197 | 100 | | } |
| 198 | | | |
| 199 | | | |
| 200 | | | |
| 201 | | | |
| 202 | | | |
| 203 | | | @param |
| 204 | | | @param |
| 205 | | | @param |
| 206 | | | <code></code> |
| 207 | | | |
| 208 | | | private void format ( |
| 209 | | | final StringBuffer sb, |
| 210 | | | final LogRecord record, |
| 211 | | | final Loggable loggable) |
| 212 | | | { |
| 213 | 100 | | List trackingIds = initialiseTrackingIds(record, loggable); |
| 214 | 100 | | Loggable currentLoggable = loggable; |
| 215 | 100 | | boolean isFirst = true; |
| 216 | | | |
| 217 | 100 | | Throwable cause = null; |
| 218 | 100 | | while (isFirst || (! ((currentLoggable == null) && (cause == null)))) |
| 219 | | | { |
| 220 | 100 | | Throwable nestedCause = null; |
| 221 | 100 | | if (currentLoggable != null) |
| 222 | | | { |
| 223 | 100 | | formatLoggable(sb, record, currentLoggable, trackingIds); |
| 224 | 100 | | nestedCause = currentLoggable.getCause(); |
| 225 | | | } |
| 226 | 100 | | else if (isFirst) |
| 227 | | | { |
| 228 | 100 | | formatLogRecord(sb, record, trackingIds); |
| 229 | 100 | | nestedCause = record.getThrown(); |
| 230 | | | } |
| 231 | 100 | | isFirst = false; |
| 232 | 100 | | cause = (cause != null) ? cause.getCause() : nestedCause; |
| 233 | 100 | | currentLoggable = null; |
| 234 | | | |
| 235 | 100 | | if (cause != null) |
| 236 | | | { |
| 237 | 100 | | appendNestingLevel(sb, record, cause, trackingIds); |
| 238 | 100 | | if (cause instanceof Loggable) |
| 239 | | | { |
| 240 | 100 | | currentLoggable = (Loggable) cause; |
| 241 | | | } |
| 242 | | | } |
| 243 | 100 | | } |
| 244 | | | |
| 245 | | | |
| 246 | 100 | | if (!(loggable instanceof LogEvent) |
| 247 | | | || FWK_TRACE_LOGGER_LOGGER.isLoggable(record.getLevel())) |
| 248 | | | { |
| 249 | 100 | | trackingIds = initialiseTrackingIds(record, loggable); |
| 250 | 100 | | appendStackTrace(sb, record, loggable, trackingIds); |
| 251 | | | } |
| 252 | 100 | | } |
| 253 | | | |
| 254 | | | private void formatLoggable ( |
| 255 | | | final StringBuffer sb, |
| 256 | | | final LogRecord record, |
| 257 | | | final Loggable loggable, |
| 258 | | | final List trackingIds) |
| 259 | | | { |
| 260 | 100 | | final LogLineFormat.LogLineType type = determineType(loggable); |
| 261 | 100 | | final LogLineFormat format = getMessageFormat(type); |
| 262 | 100 | | format.format(sb, record, loggable, trackingIds, null, null); |
| 263 | 100 | | appendParameters(sb, record, loggable, trackingIds); |
| 264 | 100 | | } |
| 265 | | | |
| 266 | | | |
| 267 | | | |
| 268 | | | |
| 269 | | | |
| 270 | | | @param |
| 271 | | | <code></code> |
| 272 | | | @param |
| 273 | | | |
| 274 | | | private void addMessageFormat ( |
| 275 | | | final Map msgFormats, |
| 276 | | | final LogLineFormat.LogLineType type) |
| 277 | | | { |
| 278 | 100 | | final LogLineFormat format = LogLineFormatFactory.create(type); |
| 279 | 100 | | msgFormats.put(type, format); |
| 280 | 100 | | } |
| 281 | | | |
| 282 | | | |
| 283 | | | |
| 284 | | | |
| 285 | | | @param |
| 286 | | | |
| 287 | | | @return<code></code> |
| 288 | | | |
| 289 | | | @see |
| 290 | | | |
| 291 | | | private LogLineFormat.LogLineType determineType (final Loggable loggable) |
| 292 | | | { |
| 293 | | | final Throwable cause; |
| 294 | | | final LogLineFormat.LogLineType rc; |
| 295 | | | |
| 296 | 100 | | if (loggable instanceof Throwable) |
| 297 | | | { |
| 298 | 100 | | cause = (Throwable) loggable; |
| 299 | | | } |
| 300 | | | else |
| 301 | | | { |
| 302 | 0 | | cause = loggable.getCause(); |
| 303 | | | } |
| 304 | 100 | | if ((cause != null) && ! (cause instanceof LogEvent)) |
| 305 | | | { |
| 306 | 100 | | rc = LogLineFormat.ERROR_MESSAGE; |
| 307 | | | } |
| 308 | | | else |
| 309 | | | { |
| 310 | 100 | | rc = LogLineFormat.LOG_MESSAGE; |
| 311 | | | } |
| 312 | 100 | | return rc; |
| 313 | | | } |
| 314 | | | |
| 315 | | | |
| 316 | | | |
| 317 | | | |
| 318 | | | |
| 319 | | | |
| 320 | | | |
| 321 | | | |
| 322 | | | @param |
| 323 | | | @param |
| 324 | | | @param |
| 325 | | | @param |
| 326 | | | |
| 327 | | | private void appendNestingLevel ( |
| 328 | | | final StringBuffer sb, |
| 329 | | | final LogRecord record, |
| 330 | | | final Throwable cause, |
| 331 | | | final List trackingIdSequence) |
| 332 | | | { |
| 333 | | | final Loggable loggable; |
| 334 | 100 | | if (cause instanceof Loggable) |
| 335 | | | { |
| 336 | 100 | | loggable = (Loggable) cause; |
| 337 | | | } |
| 338 | | | else |
| 339 | | | { |
| 340 | 100 | | loggable = null; |
| 341 | | | } |
| 342 | 100 | | final LogLineFormat.LogLineType type = LogLineFormat.NESTED_MESSAGE; |
| 343 | 100 | | final LogLineFormat format = getMessageFormat(type); |
| 344 | 100 | | if (loggable == null) |
| 345 | | | { |
| 346 | 100 | | format.format(sb, record, null, trackingIdSequence, null, cause); |
| 347 | | | } |
| 348 | | | else |
| 349 | | | { |
| 350 | 100 | | addTrackingNumber(trackingIdSequence, loggable); |
| 351 | 100 | | format.format(sb, record, loggable, trackingIdSequence, null, |
| 352 | | | loggable.getLogMessageInfo().getSymbol()); |
| 353 | | | } |
| 354 | 100 | | } |
| 355 | | | |
| 356 | | | |
| 357 | | | |
| 358 | | | |
| 359 | | | |
| 360 | | | |
| 361 | | | |
| 362 | | | @param |
| 363 | | | @param <code></code> |
| 364 | | | |
| 365 | | | |
| 366 | | | @return |
| 367 | | | |
| 368 | | | private List initialiseTrackingIds ( |
| 369 | | | final LogRecord record, |
| 370 | | | final Loggable loggable) |
| 371 | | | { |
| 372 | 100 | | final List rc = new ArrayList(); |
| 373 | | | |
| 374 | 100 | | if (loggable != null) |
| 375 | | | { |
| 376 | 100 | | addTrackingNumber(rc, loggable); |
| 377 | | | } |
| 378 | | | else |
| 379 | | | { |
| 380 | 100 | | addTrackingNumber(rc, record); |
| 381 | | | } |
| 382 | 100 | | return rc; |
| 383 | | | } |
| 384 | | | |
| 385 | | | |
| 386 | | | |
| 387 | | | |
| 388 | | | |
| 389 | | | @param |
| 390 | | | @param |
| 391 | | | |
| 392 | | | private void addTrackingNumber ( |
| 393 | | | final List trackingIds, |
| 394 | | | final LogRecord record) |
| 395 | | | { |
| 396 | 100 | | addTrackingNumber(trackingIds, |
| 397 | | | Integer.toHexString((int) record.getSequenceNumber())); |
| 398 | 100 | | } |
| 399 | | | |
| 400 | | | |
| 401 | | | |
| 402 | | | |
| 403 | | | |
| 404 | | | @param |
| 405 | | | @param |
| 406 | | | |
| 407 | | | private void addTrackingNumber ( |
| 408 | | | final List trackingIds, |
| 409 | | | final Loggable loggable) |
| 410 | | | { |
| 411 | 100 | | addTrackingNumber(trackingIds, loggable.getTrackingNumber()); |
| 412 | 100 | | } |
| 413 | | | |
| 414 | | | |
| 415 | | | |
| 416 | | | |
| 417 | | | |
| 418 | | | @param |
| 419 | | | @param |
| 420 | | | |
| 421 | | | private void addTrackingNumber ( |
| 422 | | | final List trackingIds, |
| 423 | | | final String newId) |
| 424 | | | { |
| 425 | 100 | | if (! trackingIds.isEmpty()) |
| 426 | | | { |
| 427 | 100 | | if (! trackingIds.get(trackingIds.size() - 1).equals(newId)) |
| 428 | | | { |
| 429 | 100 | | trackingIds.add(newId); |
| 430 | | | } |
| 431 | | | } |
| 432 | | | else |
| 433 | | | { |
| 434 | 100 | | trackingIds.add(newId); |
| 435 | | | } |
| 436 | 100 | | } |
| 437 | | | |
| 438 | | | |
| 439 | | | |
| 440 | | | <code></code> |
| 441 | | | <code></code> |
| 442 | | | |
| 443 | | | @param |
| 444 | | | @param <code></code> |
| 445 | | | |
| 446 | | | @return |
| 447 | | | |
| 448 | | | private Throwable getTopLevelThrown ( |
| 449 | | | final LogRecord record, |
| 450 | | | final Loggable loggable) |
| 451 | | | { |
| 452 | | | final Throwable thrown; |
| 453 | | | |
| 454 | 100 | | if (loggable == null) |
| 455 | | | { |
| 456 | 100 | | thrown = record.getThrown(); |
| 457 | | | } |
| 458 | | | else |
| 459 | | | { |
| 460 | 100 | | if (loggable instanceof Throwable) |
| 461 | | | { |
| 462 | 100 | | thrown = (Throwable) loggable; |
| 463 | | | } |
| 464 | | | else |
| 465 | | | { |
| 466 | 0 | | thrown = loggable.getCause(); |
| 467 | | | } |
| 468 | | | } |
| 469 | 100 | | return thrown; |
| 470 | | | } |
| 471 | | | } |