Java Logging: LogRecord

The LogRecord class wraps the messages logged to a Logger. You can either log a LogRecord directly to aLogger, or have the Logger wrap what is logged in a LogRecord. That is, all logging methods on the Loggerthat do not take a LogRecord will wrap the parameters passed to the log method in a LogRecord object.

Once a LogRecord is passed to, or produced internally by the Logger, it can be passed to a Filter (if a filter is set), to Handler‘s, and to Logger‘s further op the Logger hierarchy.

The LogRecord contains the following getter methods, which can be used in e.g. a Filter to determine if theLogRecord should be logged or not:

getLevel()
getLoggerName()
getMessage()
getMillis()
getParameters()
getResourceBundle()
getResourceBundleName()
getSequenceNumber()
getSourceClassName()
getSourceMethodName()
getThreadID()
getThrown()

Each of these getter methods have a corresponding setter method too, but unless you create LogRecordinstances yourself, you won‘t need the setter methods. Here is a brief explanation of the getter methods.

The getLevel() method returns the log level the message represented by this LogRecord was logged with.

The getLoggerName() method returns the name of the Logger that logged this LogRecord.

The getMessage() method returns the message logged.

The getMillis() method returns the time in milliseconds this LogRecord was recorded.

The getParameters() method returns the parameters to be inserted into the message of this LogRecord.

The getResourceBundle() method returns the ResourceBundle (if any) used to localize the message of thisLogRecord. If no ResourceBundle is used, this method returns null.

The getResourceBundleName() method returns the name of the ResourceBundle (if any) used to localize the message of this LogRecord. If no ResourceBundle is used, null is returned.

The getSequenceNumber() method returns a sequence number generated internally in the LogRecordconstructor when the LogRecord is created.

The getSourceClassName() returns the class name of the class logging the message represented by thisLogRecord. Keep in mind though, that this name may not be entirely correct for various reasons. See the JavaDoc for more details.

The getSourceMethodName() returns the method name of the method logging the message represented by this LogRecord. Keep in mind though, that this name may not be entirely correct for various reasons. See the JavaDoc for more details.

The getThreadID() method returns the ID of the thread logging the message represented by this LogRecord.

The getThrown() method returns the Throwable that was marked thrown when logging the message represented by this LogRecord. This Throwable is either passed as parameter to a Logger log method, or set directly on a LogRecord which is then passed to a Logger. This Throwable is typically what caused the log message to be logged.

时间: 2024-10-27 05:33:17

Java Logging: LogRecord的相关文章

Java Logging: Overview

Table of Contents Log Level Logger Hierarchy LogManager In this text I will try to give you an overview of the java.util.logging API. Hopefully it will be easier to understand the individual components once you understand the big picture. Many of the

Java Logging: Formatters

The Handler's in the Java Logging API use a java.util.logging.Formatter to format theLogRecord's before writing it to an external system. Java comes with two built-in Formatter's (subclasses of Formatter): SimpleFormatter XMLFormatter The various Han

Java Logging: Logger

Table of Contents Logging Messages The log() Methods The logp() Methods The logrb() Methods The Last Log Methods Adding and Removing Handlers Setting a Log Filter Setting the Log Level Parent Logger Additional Methods The java.util.Logger class is th

Java Logging: Configuration

Table of Contents Configuration Class Configuration File The Java Logging API can be configured in two ways: Via a configuration class. Via a configuration file. The initialization of the configuration is taken care of by the java.util.logging.LogMan

Java Logging API - Tutorial

Java Logging This article describes how to use the Logging API in Java programs. It includes an example for creating an HTML logger. Table of Contents 1. Overview 1.1. Logging 1.2. Logging in Java 1.3. Create a logger 1.4. Level 1.5. Handler 1.6. For

Java Logging: Handlers

Table of Contents Handlers and Formatters Built-in Handlers ConsoleHandler FileHandler File Name Pattern StreamHandler SocketHandler MemoryHandler A Handler is a component that takes care of the actual logging to the outside world. You can add one or

Java Logging: Logger Hierarchy

Table of Contents Filters and Handlers in the Logger Hierarchy Log Levels of Loggers in the Hierarchy The Logger's used in your application are typically organized into a hierarchy, as mentioned elsewhere in this tutorial. This text will take a close

Java Logging: Filters

You can set a Filter on a Logger. A Filter can filter out log messages, meaning decide if the message gets logged or not. Filters are represented by the Java interface java.util.logging.Filter Here is an example of setting a Filter on a Logger: Filte

Java Logging: Log Levels

Table of Contents Filtering Messages When a message is logged via a Logger it is logged with a certain log level. The built-in log levels are: SEVERE WARNING INFO CONFIG FINE FINER FINEST The log level is represented by the class java.util.logging.Le