FileDocCategorySizeDatePackage
LogRecord.javaAPI DocJava SE 5 API14588Fri Aug 26 14:57:28 BST 2005java.util.logging

LogRecord

public class LogRecord extends Object implements Serializable
LogRecord objects are used to pass logging requests between the logging framework and individual log Handlers.

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.

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.

Serialization notes:

  • The LogRecord class is serializable.
  • 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).
  • 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.
version
1.23, 01/12/04
since
1.4

Fields Summary
private static long
globalSequenceNumber
private static int
nextThreadId
private static ThreadLocal
threadIds
private Level
level
private long
sequenceNumber
private String
sourceClassName
private String
sourceMethodName
private String
message
private int
threadID
private long
millis
private Throwable
thrown
private String
loggerName
private String
resourceBundleName
private transient boolean
needToInferCaller
private transient Object[]
parameters
private transient ResourceBundle
resourceBundle
private static final long
serialVersionUID
Constructors Summary
public LogRecord(Level level, String msg)
Construct a LogRecord with the given level and message values.

The sequence property will be initialized with a new unique value. These sequence values are allocated in increasing order within a VM.

The millis property will be initialized to the current time.

The thread ID property will be initialized with a unique ID for the current thread.

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)


                                                                                                  
         
	// 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;
   
Methods Summary
public java.util.logging.LevelgetLevel()
Get the logging message level, for example Level.SEVERE.

return
the logging message level

	return level;
    
public java.lang.StringgetLoggerName()
Get the source Logger name's

return
source logger name (may be null)

	return loggerName;
    
public java.lang.StringgetMessage()
Get the "raw" log message, before localization or formatting.

May be null, which is equivalent to the empty string "".

This message may be either the final text or a localization key.

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

	return message;
    
public longgetMillis()
Get event time in milliseconds since 1970.

return
event time in millis since 1970

	return millis;
    
public java.lang.Object[]getParameters()
Get the parameters to the log message.

return
the log message parameters. May be null if there are no parameters.

	return parameters;
    
public java.util.ResourceBundlegetResourceBundle()
Get the localization resource bundle

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.

	return resourceBundle;
    
public java.lang.StringgetResourceBundleName()
Get the localization resource bundle name

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.

	return resourceBundleName;
    
public longgetSequenceNumber()
Get the sequence number.

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

	return sequenceNumber;
    
public java.lang.StringgetSourceClassName()
Get the name of the class that (allegedly) issued the logging request.

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.

May be null if no information could be obtained.

return
the source class name

	if (needToInferCaller) {
	    inferCaller();
	}
	return sourceClassName;
    
public java.lang.StringgetSourceMethodName()
Get the name of the method that (allegedly) issued the logging request.

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.

May be null if no information could be obtained.

return
the source method name

	if (needToInferCaller) {
	    inferCaller();
	}
	return sourceMethodName;
    
public intgetThreadID()
Get an identifier for the thread where the message originated.

This is a thread identifier within the Java VM and may or may not map to any operating system ID.

return
thread ID

	return threadID;
    
public java.lang.ThrowablegetThrown()
Get any throwable associated with the log record.

If the event involved an exception, this will be the exception object. Otherwise null.

return
a throwable

	return thrown;
    
private voidinferCaller()

	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.
    
private voidreadObject(java.io.ObjectInputStream in)

	// 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;
    
public voidsetLevel(java.util.logging.Level level)
Set the logging message level, for example Level.SEVERE.

param
level the logging message level

	if (level == null) {
	    throw new NullPointerException();
	}
	this.level = level;
    
public voidsetLoggerName(java.lang.String name)
Set the source Logger name.

param
name the source logger name (may be null)

	loggerName = name;
    
public voidsetMessage(java.lang.String message)
Set the "raw" log message, before localization or formatting.

param
message the raw message string (may be null)

	this.message = message;
    
public voidsetMillis(long millis)
Set event time.

param
millis event time in millis since 1970

	this.millis = millis;
    
public voidsetParameters(java.lang.Object[] parameters)
Set the parameters to the log message.

param
parameters the log message parameters. (may be null)

	this.parameters = parameters;
    
public voidsetResourceBundle(java.util.ResourceBundle bundle)
Set the localization resource bundle.

param
bundle localization bundle (may be null)

	resourceBundle = bundle;
    
public voidsetResourceBundleName(java.lang.String name)
Set the localization resource bundle name.

param
name localization bundle name (may be null)

	resourceBundleName = name;
    
public voidsetSequenceNumber(long seq)
Set the sequence number.

Sequence numbers are normally assigned in the LogRecord constructor, so it should not normally be necessary to use this method.

	sequenceNumber = seq;
    
public voidsetSourceClassName(java.lang.String sourceClassName)
Set the name of the class that (allegedly) issued the logging request.

param
sourceClassName the source class name (may be null)

	this.sourceClassName = sourceClassName;
	needToInferCaller = false;
    
public voidsetSourceMethodName(java.lang.String sourceMethodName)
Set the name of the method that (allegedly) issued the logging request.

param
sourceMethodName the source method name (may be null)

	this.sourceMethodName = sourceMethodName;
	needToInferCaller = false;
    
public voidsetThreadID(int threadID)
Set an identifier for the thread where the message originated.

param
threadID the thread ID

	this.threadID = threadID;
    
public voidsetThrown(java.lang.Throwable thrown)
Set a throwable associated with the log event.

param
thrown a throwable (may be null)

	this.thrown = thrown;
    
private voidwriteObject(java.io.ObjectOutputStream out)

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.


                                                                                             
          
	// 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());
	    }
	}