| 1 | /* |
|---|
| 2 | * $Id$ |
|---|
| 3 | * |
|---|
| 4 | * Copyright 2006, The jCoderZ.org Project. All rights reserved. |
|---|
| 5 | * |
|---|
| 6 | * Redistribution and use in source and binary forms, with or without |
|---|
| 7 | * modification, are permitted provided that the following conditions are |
|---|
| 8 | * met: |
|---|
| 9 | * |
|---|
| 10 | * * Redistributions of source code must retain the above copyright |
|---|
| 11 | * notice, this list of conditions and the following disclaimer. |
|---|
| 12 | * * Redistributions in binary form must reproduce the above |
|---|
| 13 | * copyright notice, this list of conditions and the following |
|---|
| 14 | * disclaimer in the documentation and/or other materials |
|---|
| 15 | * provided with the distribution. |
|---|
| 16 | * * Neither the name of the jCoderZ.org Project nor the names of |
|---|
| 17 | * its contributors may be used to endorse or promote products |
|---|
| 18 | * derived from this software without specific prior written |
|---|
| 19 | * permission. |
|---|
| 20 | * |
|---|
| 21 | * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND |
|---|
| 22 | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|---|
| 23 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
|---|
| 24 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS |
|---|
| 25 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|---|
| 26 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|---|
| 27 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR |
|---|
| 28 | * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
|---|
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR |
|---|
| 30 | * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF |
|---|
| 31 | * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 32 | */ |
|---|
| 33 | package org.jcoderz.commons.logging; |
|---|
| 34 | |
|---|
| 35 | import java.util.Iterator; |
|---|
| 36 | import java.util.List; |
|---|
| 37 | import java.util.Set; |
|---|
| 38 | |
|---|
| 39 | import org.jcoderz.commons.BusinessImpact; |
|---|
| 40 | import org.jcoderz.commons.Category; |
|---|
| 41 | import org.jcoderz.commons.Loggable; |
|---|
| 42 | import org.jcoderz.commons.LoggableImpl; |
|---|
| 43 | import org.jcoderz.commons.types.Date; |
|---|
| 44 | |
|---|
| 45 | |
|---|
| 46 | |
|---|
| 47 | /** |
|---|
| 48 | * This is an implementation of LogItem based upon LogRecords. On instanciation |
|---|
| 49 | * it creates a nested structure of LogElements according to the structure |
|---|
| 50 | * of the LogRecord. |
|---|
| 51 | * |
|---|
| 52 | */ |
|---|
| 53 | public class LogElement |
|---|
| 54 | extends LogItem |
|---|
| 55 | { |
|---|
| 56 | private static final String TRACEMSG = "TRACEMSG"; |
|---|
| 57 | |
|---|
| 58 | private Loggable mLoggable; |
|---|
| 59 | private final java.util.logging.LogRecord mLogRecord; |
|---|
| 60 | private Throwable mThrown; |
|---|
| 61 | |
|---|
| 62 | /** |
|---|
| 63 | * Creates a root LogElement from a LogRecord |
|---|
| 64 | * |
|---|
| 65 | * @param record The log record from which to create this. |
|---|
| 66 | */ |
|---|
| 67 | LogElement (final java.util.logging.LogRecord record) |
|---|
| 68 | { |
|---|
| 69 | mThrown = null; |
|---|
| 70 | mLogRecord = record; |
|---|
| 71 | Loggable loggable = null; |
|---|
| 72 | |
|---|
| 73 | if (! ((record.getParameters() == null) |
|---|
| 74 | || record.getParameters().length == 0)) |
|---|
| 75 | { |
|---|
| 76 | if (record.getParameters()[0] instanceof Loggable) |
|---|
| 77 | { |
|---|
| 78 | loggable = (Loggable) record.getParameters()[0]; |
|---|
| 79 | } |
|---|
| 80 | } |
|---|
| 81 | if (loggable != null) |
|---|
| 82 | { |
|---|
| 83 | init(loggable); |
|---|
| 84 | } |
|---|
| 85 | else |
|---|
| 86 | { |
|---|
| 87 | init(); |
|---|
| 88 | } |
|---|
| 89 | } |
|---|
| 90 | |
|---|
| 91 | /** |
|---|
| 92 | * Creates a nested LogElement from a LogRecord and nested LogRecord |
|---|
| 93 | * elements. |
|---|
| 94 | * |
|---|
| 95 | * @param record The log record from which to create this. |
|---|
| 96 | * @param thrown The current nested element of LogRecord. |
|---|
| 97 | * @param parent The parent element of this. |
|---|
| 98 | */ |
|---|
| 99 | private LogElement ( |
|---|
| 100 | final java.util.logging.LogRecord record, |
|---|
| 101 | final Throwable thrown, |
|---|
| 102 | final LogElement parent) |
|---|
| 103 | { |
|---|
| 104 | mLogRecord = record; |
|---|
| 105 | setParentItem(parent); |
|---|
| 106 | init(thrown); |
|---|
| 107 | } |
|---|
| 108 | |
|---|
| 109 | private void init () |
|---|
| 110 | { |
|---|
| 111 | mLoggable = null; |
|---|
| 112 | initData(); |
|---|
| 113 | |
|---|
| 114 | final Throwable thrown = mLogRecord.getThrown(); |
|---|
| 115 | if (thrown != null) |
|---|
| 116 | { |
|---|
| 117 | setNestedItem(new LogElement(mLogRecord, thrown, this)); |
|---|
| 118 | } |
|---|
| 119 | } |
|---|
| 120 | |
|---|
| 121 | private void init (Throwable thrown) |
|---|
| 122 | { |
|---|
| 123 | if (thrown instanceof Loggable) |
|---|
| 124 | { |
|---|
| 125 | init((Loggable) thrown); |
|---|
| 126 | } |
|---|
| 127 | else |
|---|
| 128 | { |
|---|
| 129 | final Throwable nestedThrown = thrown.getCause(); |
|---|
| 130 | mLoggable = null; |
|---|
| 131 | mThrown = thrown; |
|---|
| 132 | initData(); |
|---|
| 133 | if (nestedThrown != null) |
|---|
| 134 | { |
|---|
| 135 | setNestedItem(new LogElement(mLogRecord, nestedThrown, this)); |
|---|
| 136 | } |
|---|
| 137 | } |
|---|
| 138 | } |
|---|
| 139 | |
|---|
| 140 | private void init (Loggable loggable) |
|---|
| 141 | { |
|---|
| 142 | mLoggable = loggable; |
|---|
| 143 | mThrown = null; |
|---|
| 144 | initData(); |
|---|
| 145 | if (loggable.getCause() != null) |
|---|
| 146 | { |
|---|
| 147 | setNestedItem(new LogElement(mLogRecord, loggable.getCause(), this)); |
|---|
| 148 | } |
|---|
| 149 | } |
|---|
| 150 | |
|---|
| 151 | private void initData () |
|---|
| 152 | { |
|---|
| 153 | initType(); |
|---|
| 154 | setLoggerLevel(mLogRecord.getLevel()); |
|---|
| 155 | setSourceClass(mLogRecord.getSourceClassName()); |
|---|
| 156 | setSourceMethod(mLogRecord.getSourceMethodName()); |
|---|
| 157 | if (mLoggable != null) |
|---|
| 158 | { |
|---|
| 159 | setBusinessImpact(mLoggable.getLogMessageInfo().getBusinessImpact()); |
|---|
| 160 | setCategory(mLoggable.getLogMessageInfo().getCategory()); |
|---|
| 161 | setInstanceId(mLoggable.getInstanceId()); |
|---|
| 162 | setMessage(mLoggable.getMessage()); |
|---|
| 163 | setNodeId(mLoggable.getNodeId()); |
|---|
| 164 | setSolution(mLoggable.getLogMessageInfo().getSolution()); |
|---|
| 165 | setSymbol(mLoggable.getLogMessageInfo().getSymbol()); |
|---|
| 166 | setSymbolId( |
|---|
| 167 | Integer.toHexString(mLoggable.getLogMessageInfo().toInt())); |
|---|
| 168 | setThreadId(mLoggable.getThreadId()); |
|---|
| 169 | try |
|---|
| 170 | { |
|---|
| 171 | setThreadName(mLoggable.getThreadName()); |
|---|
| 172 | } |
|---|
| 173 | catch (AbstractMethodError ex) |
|---|
| 174 | { |
|---|
| 175 | // We have a old loggable that does not support |
|---|
| 176 | // thread name jet. |
|---|
| 177 | setThreadName(Thread.currentThread().getName()); |
|---|
| 178 | } |
|---|
| 179 | |
|---|
| 180 | setTimestamp(Date.fromUtilDate( |
|---|
| 181 | new java.util.Date(mLoggable.getEventTime()))); |
|---|
| 182 | setTrackingNumber(mLoggable.getTrackingNumber()); |
|---|
| 183 | setParameters(); |
|---|
| 184 | if (mLoggable instanceof Throwable) |
|---|
| 185 | { |
|---|
| 186 | initStackTrace((Throwable) mLoggable); |
|---|
| 187 | } |
|---|
| 188 | } |
|---|
| 189 | else if (mThrown != null) |
|---|
| 190 | { |
|---|
| 191 | setMessage(mThrown.getMessage()); |
|---|
| 192 | initStackTrace(mThrown); |
|---|
| 193 | } |
|---|
| 194 | else |
|---|
| 195 | { |
|---|
| 196 | setBusinessImpact(BusinessImpact.UNDEFINED); |
|---|
| 197 | setCategory(Category.TECHNICAL); |
|---|
| 198 | setInstanceId(LoggableImpl.INSTANCE_ID); |
|---|
| 199 | setMessage(mLogRecord.getMessage()); |
|---|
| 200 | setNodeId(LoggableImpl.NODE_ID); |
|---|
| 201 | setSymbol(TRACEMSG); |
|---|
| 202 | setSymbolId(TRACEMSG); |
|---|
| 203 | setThreadId(mLogRecord.getThreadID()); |
|---|
| 204 | setThreadName(Thread.currentThread().getName()); |
|---|
| 205 | setTimestamp(Date.fromLong(mLogRecord.getMillis())); |
|---|
| 206 | setTrackingNumber(Integer.toHexString( |
|---|
| 207 | (int) mLogRecord.getSequenceNumber())); |
|---|
| 208 | } |
|---|
| 209 | } |
|---|
| 210 | |
|---|
| 211 | private void setParameters () |
|---|
| 212 | { |
|---|
| 213 | if (mLoggable != null) |
|---|
| 214 | { |
|---|
| 215 | final Set names = mLoggable.getParameterNames(); |
|---|
| 216 | if (names != null && ! names.isEmpty()) |
|---|
| 217 | { |
|---|
| 218 | for (final Iterator iter = names.iterator(); iter.hasNext(); ) |
|---|
| 219 | { |
|---|
| 220 | final String name = (String) iter.next(); |
|---|
| 221 | if (! name.startsWith(INTERNAL_PARAMETER_PREFIX)) |
|---|
| 222 | { |
|---|
| 223 | final List parameters = mLoggable.getParameter(name); |
|---|
| 224 | addToParameters(name, parameters); |
|---|
| 225 | } |
|---|
| 226 | } |
|---|
| 227 | } |
|---|
| 228 | } |
|---|
| 229 | } |
|---|
| 230 | |
|---|
| 231 | private void initStackTrace (final Throwable thrown) |
|---|
| 232 | { |
|---|
| 233 | // this is a nop, currently not interested in stack trace |
|---|
| 234 | } |
|---|
| 235 | |
|---|
| 236 | |
|---|
| 237 | private void initType () |
|---|
| 238 | { |
|---|
| 239 | if (mLoggable == null) |
|---|
| 240 | { |
|---|
| 241 | if (getParentItem() == null) |
|---|
| 242 | { |
|---|
| 243 | if (mLogRecord.getThrown() == null) |
|---|
| 244 | { |
|---|
| 245 | setType(String.valueOf( |
|---|
| 246 | LogLineFormat.TRACE_MESSAGE.getTypeSpecifier())); |
|---|
| 247 | } |
|---|
| 248 | else |
|---|
| 249 | { |
|---|
| 250 | setType(String.valueOf( |
|---|
| 251 | LogLineFormat.EXCEPTION_MESSAGE.getTypeSpecifier())); |
|---|
| 252 | } |
|---|
| 253 | } |
|---|
| 254 | } |
|---|
| 255 | else |
|---|
| 256 | { |
|---|
| 257 | if ((mLoggable.getCause() == null) || ( |
|---|
| 258 | mLoggable.getCause() instanceof Loggable)) |
|---|
| 259 | { |
|---|
| 260 | setType(String.valueOf( |
|---|
| 261 | LogLineFormat.LOG_MESSAGE.getTypeSpecifier())); |
|---|
| 262 | } |
|---|
| 263 | else |
|---|
| 264 | { |
|---|
| 265 | setType(String.valueOf( |
|---|
| 266 | LogLineFormat.ERROR_MESSAGE.getTypeSpecifier())); |
|---|
| 267 | } |
|---|
| 268 | } |
|---|
| 269 | } |
|---|
| 270 | } |
|---|