| 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.MessageFormat; |
| 38 | | | import java.text.ParseException; |
| 39 | | | import java.util.List; |
| 40 | | | import java.util.logging.LogRecord; |
| 41 | | | |
| 42 | | | import org.jcoderz.commons.Loggable; |
| 43 | | | import org.jcoderz.commons.util.ArraysUtil; |
| 44 | | | import org.jcoderz.commons.util.ObjectUtil; |
| 45 | | | import org.jcoderz.commons.util.StringUtil; |
| 46 | | | |
| 47 | | | |
| 48 | | | |
| 49 | | | |
| 50 | | | |
| 51 | | | |
| 52 | | (1) | public class TraceLineFormat |
| 53 | | | extends BasicLogLineFormat |
| 54 | | | { |
| 55 | | | |
| 56 | | | |
| 57 | | | private static final int NUMBER_OF_ADDITIONAL_PARAMETERS = 2; |
| 58 | | | |
| 59 | | | private static final int LOGSOURCE_INDEX = NUMBER_OF_PARAMETERS; |
| 60 | | | |
| 61 | | | private static final int TEXT_INDEX = LOGSOURCE_INDEX + 1; |
| 62 | | | |
| 63 | | | |
| 64 | | | private static final String ADDITIONAL_LOGLINE_FORMAT_PATTERN |
| 65 | | | = " {" + LOGSOURCE_INDEX + "} {" + TEXT_INDEX + "}"; |
| 66 | | | |
| 67 | | | private static final String EMPTY_MSG = " "; |
| 68 | | | |
| 69 | | | |
| 70 | | | |
| 71 | | | |
| 72 | | | TraceLineFormat () |
| 73 | | | { |
| 74 | 100 | | this(LogLineFormat.TRACE_MESSAGE); |
| 75 | 100 | | } |
| 76 | | | |
| 77 | | | |
| 78 | | | |
| 79 | | | |
| 80 | | | |
| 81 | | | @param |
| 82 | | | |
| 83 | | | protected TraceLineFormat (final LogLineType type) |
| 84 | | | { |
| 85 | 100 | | super(type, ADDITIONAL_LOGLINE_FORMAT_PATTERN, |
| 86 | | | NUMBER_OF_ADDITIONAL_PARAMETERS); |
| 87 | 100 | | setFormats(getFormatList(null, true)); |
| 88 | 100 | | } |
| 89 | | | |
| 90 | | | |
| 91 | | | |
| 92 | | | |
| 93 | | | |
| 94 | | | @param |
| 95 | | | @param |
| 96 | | | @param |
| 97 | | | @param |
| 98 | | | |
| 99 | | | @param |
| 100 | | | @param |
| 101 | | | |
| 102 | | | @see |
| 103 | | | |
| 104 | | | public void format ( |
| 105 | | | final StringBuffer sb, |
| 106 | | | final LogRecord record, |
| 107 | | | final Loggable loggable, |
| 108 | | | final List trackingIdSequence, |
| 109 | | | final Throwable thrown, |
| 110 | | | final Object parameter) |
| 111 | | | { |
| 112 | 100 | | if (record.getParameters() == null) |
| 113 | | | { |
| 114 | 100 | | setMessageText(record.getMessage()); |
| 115 | | | } |
| 116 | | | else |
| 117 | | | { |
| 118 | 100 | | setMessageText( |
| 119 | | | formatMessage(record.getMessage(), record.getParameters())); |
| 120 | | | } |
| 121 | 100 | | setLogSource(record.getSourceClassName(), record.getSourceMethodName()); |
| 122 | 100 | | basicFormat(sb, record, loggable, trackingIdSequence); |
| 123 | 100 | | } |
| 124 | | | |
| 125 | | | |
| 126 | | | |
| 127 | | | |
| 128 | | | |
| 129 | | | @param |
| 130 | | | @param |
| 131 | | | |
| 132 | | | @throws |
| 133 | | | |
| 134 | | | @see |
| 135 | | | |
| 136 | | | public void parse (StringBuffer sb, LogFileEntry entry) |
| 137 | | | throws ParseException |
| 138 | | | { |
| 139 | | | try |
| 140 | | | { |
| 141 | 100 | | basicParse(sb, entry); |
| 142 | 100 | | entry.setMessage(getMessageText()); |
| 143 | 100 | | final String[] logSource = getLogSource(); |
| 144 | | | |
| 145 | 100 | | entry.setSourceClass(logSource[SOURCECLASS_INDEX]); |
| 146 | 100 | (2) | entry.setSourceMethod(logSource[SOURCEMETHOD_INDEX]); |
| 147 | | | } |
| 148 | 0 | | catch (ParseException pex) |
| 149 | | | { |
| 150 | | | |
| 151 | 0 | | throw pex; |
| 152 | | | } |
| 153 | 0 | | catch (Exception ex) |
| 154 | | | { |
| 155 | 0 | | final ParseException pex = new ParseException( |
| 156 | | | "Got an error parsing " + sb, 0); |
| 157 | 0 | | pex.initCause(ex); |
| 158 | 0 | | throw pex; |
| 159 | 100 | | } |
| 160 | 100 | | } |
| 161 | | | |
| 162 | | | |
| 163 | | | |
| 164 | | | |
| 165 | | | @param |
| 166 | | | <code></code> |
| 167 | | | @param |
| 168 | | | |
| 169 | | | |
| 170 | | | @return |
| 171 | | | |
| 172 | | | |
| 173 | | | static final Format[] getFormatList ( |
| 174 | | | final DisplayOptions options, |
| 175 | | | final boolean ignoreOptions) |
| 176 | | | { |
| 177 | 100 | | final List formatList = getBasicFormatList(options, ignoreOptions); |
| 178 | 33 | | if (ignoreOptions || options.displaySourceClass() |
| 179 | | | || options.displaySourceMethod()) |
| 180 | | | { |
| 181 | 100 | | formatList.add(new AsItIsFormat(" \t")); |
| 182 | | | } |
| 183 | 100 | | formatList.add(new WhitespaceFormat(new AsItIsFormat("\r\n"))); |
| 184 | | | |
| 185 | 100 | | return (Format[]) formatList.toArray(EMPTY_FORMATTERS); |
| 186 | | | } |
| 187 | | | |
| 188 | | | |
| 189 | | | |
| 190 | | | |
| 191 | | | |
| 192 | | | @param |
| 193 | | | |
| 194 | | | protected final void setMessageText (final String text) |
| 195 | | | { |
| 196 | 100 | | if (text == null || text.length() <= 0) |
| 197 | | | { |
| 198 | 0 | | setParameter(TEXT_INDEX, EMPTY_MSG); |
| 199 | | | } |
| 200 | | | else |
| 201 | | | { |
| 202 | 100 | | setParameter(TEXT_INDEX, text); |
| 203 | | | } |
| 204 | 100 | | } |
| 205 | | | |
| 206 | | | |
| 207 | | | |
| 208 | | | |
| 209 | | | @return |
| 210 | | | |
| 211 | | | protected final String getMessageText () |
| 212 | | | { |
| 213 | 100 | | return (String) getParameter(TEXT_INDEX); |
| 214 | | | } |
| 215 | | | |
| 216 | | | |
| 217 | | | |
| 218 | | | |
| 219 | | | @param |
| 220 | | | @param |
| 221 | | | |
| 222 | | | protected final void setLogSource ( |
| 223 | | | final String clazz, final String method) |
| 224 | | | { |
| 225 | 100 | | final boolean needParentheses |
| 226 | | | = !StringUtil.isNullOrEmpty(method) && method.indexOf('(') < 0; |
| 227 | 100 | | setParameter(LOGSOURCE_INDEX, |
| 228 | | | ObjectUtil.toStringOrEmpty(clazz) + "." |
| 229 | | | + ObjectUtil.toStringOrEmpty(method) |
| 230 | | | + (needParentheses ? "()" : "")); |
| 231 | 100 | | } |
| 232 | | | |
| 233 | | | |
| 234 | | | |
| 235 | | | |
| 236 | | | |
| 237 | | | @return |
| 238 | | | |
| 239 | | | |
| 240 | | | protected final String[] getLogSource () |
| 241 | | | { |
| 242 | 100 | | return getLogSource((String) getParameter(LOGSOURCE_INDEX)); |
| 243 | | | } |
| 244 | | | |
| 245 | | | private static final String formatMessage (String pattern, Object[] params) |
| 246 | | | { |
| 247 | | | String result; |
| 248 | 100 | | if (params != null && params.length != 0) |
| 249 | | | { |
| 250 | | | try |
| 251 | | | { |
| 252 | 100 | | final MessageFormat formatter = new MessageFormat(pattern); |
| 253 | 100 | | result |
| 254 | | | = formatter.format( |
| 255 | | | params, new StringBuffer(), null).toString(); |
| 256 | | | } |
| 257 | 0 | | catch (IllegalArgumentException ex) |
| 258 | | | { |
| 259 | 0 | | result |
| 260 | | | = ArraysUtil.toString(params) + " " + pattern; |
| 261 | 50 | | } |
| 262 | | | } |
| 263 | | | else |
| 264 | | | { |
| 265 | 0 | | result = pattern; |
| 266 | | | } |
| 267 | 100 | | return result; |
| 268 | | | } |
| 269 | | | |
| 270 | | | } |