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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508
/* * @(#)LogRecord.java 1.24 05/11/17 * * Copyright 2006 Sun Microsystems, Inc. All rights reserved. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms. */ package java.util.logging; import java.util.*; import java.io.*; /** * LogRecord objects are used to pass logging requests between * the logging framework and individual log Handlers. * <p> * When a LogRecord is passed into the logging framework it * logically belongs to the framework and should no longer be * used or updated by the client application. * <p> * Note that if the client application has not specified an * explicit source method name and source class name, then the * LogRecord class will infer them automatically when they are * first accessed (due to a call on getSourceMethodName or * getSourceClassName) by analyzing the call stack. Therefore, * if a logging Handler wants to pass off a LogRecord to another * thread, or to transmit it over RMI, and if it wishes to subsequently * obtain method name or class name information it should call * one of getSourceClassName or getSourceMethodName to force * the values to be filled in. * <p> * <b> Serialization notes:</b> * <ul> * <li>The LogRecord class is serializable. * * <li> Because objects in the parameters array may not be serializable, * during serialization all objects in the parameters array are * written as the corresponding Strings (using Object.toString). * * <li> The ResourceBundle is not transmitted as part of the serialized * form, but the resource bundle name is, and the recipient object's * readObject method will attempt to locate a suitable resource bundle. * * </ul> * * @version 1.24, 11/17/05 * @since 1.4 */ public class LogRecord implements java.io.Serializable { private static long globalSequenceNumber; private static int nextThreadId=10; private static ThreadLocal threadIds = new ThreadLocal(); /** * @serial Logging message level */ private Level level; /** * @serial Sequence number */ private long sequenceNumber; /** * @serial Class that issued logging call */ private String sourceClassName; /** * @serial Method that issued logging call */ private String sourceMethodName; /** * @serial Non-localized raw message text */ private String message; /** * @serial Thread ID for thread that issued logging call. */ private int threadID; /** * @serial Event time in milliseconds since 1970 */ private long millis; /** * @serial The Throwable (if any) associated with log message */ private Throwable thrown; /** * @serial Name of the source Logger. */ private String loggerName; /** * @serial Resource bundle name to localized log message. */ private String resourceBundleName; private transient boolean needToInferCaller; private transient Object parameters[]; private transient ResourceBundle resourceBundle; /** * Construct a LogRecord with the given level and message values. * <p> * The sequence property will be initialized with a new unique value. * These sequence values are allocated in increasing order within a VM. * <p> * The millis property will be initialized to the current time. * <p> * The thread ID property will be initialized with a unique ID for * the current thread. * <p> * All other properties will be initialized to "null". * * @param level a logging level value * @param msg the raw non-localized logging message (may be null) */ public LogRecord(Level level, String msg) { // Make sure level isn't null, by calling random method. level.getClass(); this.level = level; message = msg; // Assign a thread ID and a unique sequence number. synchronized (LogRecord.class) { sequenceNumber = globalSequenceNumber++; Integer id = (Integer)threadIds.get(); if (id == null) { id = new Integer(nextThreadId++); threadIds.set(id); } threadID = id.intValue(); } millis = System.currentTimeMillis(); needToInferCaller = true; } /** * Get the source Logger name's * * @return source logger name (may be null) */ public String getLoggerName() { return loggerName; } /** * Set the source Logger name. * * @param name the source logger name (may be null) */ public void setLoggerName(String name) { loggerName = name; } /** * Get the localization resource bundle * <p> * This is the ResourceBundle that should be used to localize * the message string before formatting it. The result may * be null if the message is not localizable, or if no suitable * ResourceBundle is available. */ public ResourceBundle getResourceBundle() { return resourceBundle; } /** * Set the localization resource bundle. * * @param bundle localization bundle (may be null) */ public void setResourceBundle(ResourceBundle bundle) { resourceBundle = bundle; } /** * Get the localization resource bundle name * <p> * This is the name for the ResourceBundle that should be * used to localize the message string before formatting it. * The result may be null if the message is not localizable. */ public String getResourceBundleName() { return resourceBundleName; } /** * Set the localization resource bundle name. * * @param name localization bundle name (may be null) */ public void setResourceBundleName(String name) { resourceBundleName = name; } /** * Get the logging message level, for example Level.SEVERE. * @return the logging message level */ public Level getLevel() { return level; } /** * Set the logging message level, for example Level.SEVERE. * @param level the logging message level */ public void setLevel(Level level) { if (level == null) { throw new NullPointerException(); } this.level = level; } /** * Get the sequence number. * <p> * Sequence numbers are normally assigned in the LogRecord * constructor, which assigns unique sequence numbers to * each new LogRecord in increasing order. * @return the sequence number */ public long getSequenceNumber() { return sequenceNumber; } /** * Set the sequence number. * <p> * Sequence numbers are normally assigned in the LogRecord constructor, * so it should not normally be necessary to use this method. */ public void setSequenceNumber(long seq) { sequenceNumber = seq; } /** * Get the name of the class that (allegedly) issued the logging request. * <p> * Note that this sourceClassName is not verified and may be spoofed. * This information may either have been provided as part of the * logging call, or it may have been inferred automatically by the * logging framework. In the latter case, the information may only * be approximate and may in fact describe an earlier call on the * stack frame. * <p> * May be null if no information could be obtained. * * @return the source class name */ public String getSourceClassName() { if (needToInferCaller) { inferCaller(); } return sourceClassName; } /** * Set the name of the class that (allegedly) issued the logging request. * * @param sourceClassName the source class name (may be null) */ public void setSourceClassName(String sourceClassName) { this.sourceClassName = sourceClassName; needToInferCaller = false; } /** * Get the name of the method that (allegedly) issued the logging request. * <p> * Note that this sourceMethodName is not verified and may be spoofed. * This information may either have been provided as part of the * logging call, or it may have been inferred automatically by the * logging framework. In the latter case, the information may only * be approximate and may in fact describe an earlier call on the * stack frame. * <p> * May be null if no information could be obtained. * * @return the source method name */ public String getSourceMethodName() { if (needToInferCaller) { inferCaller(); } return sourceMethodName; } /** * Set the name of the method that (allegedly) issued the logging request. * * @param sourceMethodName the source method name (may be null) */ public void setSourceMethodName(String sourceMethodName) { this.sourceMethodName = sourceMethodName; needToInferCaller = false; } /** * Get the "raw" log message, before localization or formatting. * <p> * May be null, which is equivalent to the empty string "". * <p> * This message may be either the final text or a localization key. * <p> * During formatting, if the source logger has a localization * ResourceBundle and if that ResourceBundle has an entry for * this message string, then the message string is replaced * with the localized value. * * @return the raw message string */ public String getMessage() { return message; } /** * Set the "raw" log message, before localization or formatting. * * @param message the raw message string (may be null) */ public void setMessage(String message) { this.message = message; } /** * Get the parameters to the log message. * * @return the log message parameters. May be null if * there are no parameters. */ public Object[] getParameters() { return parameters; } /** * Set the parameters to the log message. * * @param parameters the log message parameters. (may be null) */ public void setParameters(Object parameters[]) { this.parameters = parameters; } /** * Get an identifier for the thread where the message originated. * <p> * This is a thread identifier within the Java VM and may or * may not map to any operating system ID. * * @return thread ID */ public int getThreadID() { return threadID; } /** * Set an identifier for the thread where the message originated. * @param threadID the thread ID */ public void setThreadID(int threadID) { this.threadID = threadID; } /** * Get event time in milliseconds since 1970. * * @return event time in millis since 1970 */ public long getMillis() { return millis; } /** * Set event time. * * @param millis event time in millis since 1970 */ public void setMillis(long millis) { this.millis = millis; } /** * Get any throwable associated with the log record. * <p> * If the event involved an exception, this will be the * exception object. Otherwise null. * * @return a throwable */ public Throwable getThrown() { return thrown; } /** * Set a throwable associated with the log event. * * @param thrown a throwable (may be null) */ public void setThrown(Throwable thrown) { this.thrown = thrown; } private static final long serialVersionUID = 5372048053134512534L; /** * @serialData Default fields, followed by a two byte version number * (major byte, followed by minor byte), followed by information on * the log record parameter array. If there is no parameter array, * then -1 is written. If there is a parameter array (possible of zero * length) then the array length is written as an integer, followed * by String values for each parameter. If a parameter is null, then * a null String is written. Otherwise the output of Object.toString() * is written. */ private void writeObject(ObjectOutputStream out) throws IOException { // We have to call defaultWriteObject first. out.defaultWriteObject(); // Write our version number. out.writeByte(1); out.writeByte(0); if (parameters == null) { out.writeInt(-1); return; } out.writeInt(parameters.length); // Write string values for the parameters. for (int i = 0; i < parameters.length; i++) { if (parameters[i] == null) { out.writeObject(null); } else { out.writeObject(parameters[i].toString()); } } } private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException { // We have to call defaultReadObject first. in.defaultReadObject(); // Read version number. byte major = in.readByte(); byte minor = in.readByte(); if (major != 1) { throw new IOException("LogRecord: bad version: " + major + "." + minor); } int len = in.readInt(); if (len == -1) { parameters = null; } else { parameters = new Object[len]; for (int i = 0; i < parameters.length; i++) { parameters[i] = in.readObject(); } } // If necessary, try to regenerate the resource bundle. if (resourceBundleName != null) { try { resourceBundle = ResourceBundle.getBundle(resourceBundleName); } catch (MissingResourceException ex) { // This is not a good place to throw an exception, // so we simply leave the resourceBundle null. resourceBundle = null; } } needToInferCaller = false; } // Private method to infer the caller's class and method names private void inferCaller() { needToInferCaller = false; // Get the stack trace. StackTraceElement stack[] = (new Throwable()).getStackTrace(); // First, search back to a method in the Logger class. int ix = 0; while (ix < stack.length) { StackTraceElement frame = stack[ix]; String cname = frame.getClassName(); if (cname.equals("java.util.logging.Logger")) { break; } ix++; } // Now search for the first frame before the "Logger" class. while (ix < stack.length) { StackTraceElement frame = stack[ix]; String cname = frame.getClassName(); if (!cname.equals("java.util.logging.Logger")) { // We've found the relevant frame. setSourceClassName(cname); setSourceMethodName(frame.getMethodName()); return; } ix++; } // We haven't found a suitable frame, so just punt. This is // OK as we are only committed to making a "best effort" here. } }