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):

  1. SimpleFormatter
  2. XMLFormatter

The various Handler‘s in the Java Logging API use either of these two Formatter‘s by default, but you can also set your own custom Formatter subclass on a Handler.

You can create your own Formatter by subclassing the java.util.logging.Formatter class. Here is a simple example:

public class MyFormatter extends Formatter {

    @Override
    public String format(LogRecord record) {
        return record.getLevel() + ":" + record.getMessage();
    }
}

The subclass must override the abstract format() method in the Formatter class. The String returned by theformat() is what is forwarded to the external system by the Handler. Exactly how the string should be formatted is up to you.

The Formatter class also contains the convenience method formatMessage() which can be used to format the message using the ResourceBundle of the LogRecord. Here is an example:

public class MyFormatter extends Formatter {

    @Override
    public String format(LogRecord record) {
        return formatMessage(record);
    }
}

You will not often need to implement your own Formatter, but once in a while, if a specific log format is required, a custom Formatter may be useful.

时间: 2024-08-08 11:01:24

Java Logging: Formatters的相关文章

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: 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: 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

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: LogManager

The java.util.logging.LogManager manages the internal Logger hierarchy, and initiates the configuration of the Logger's, either through the configuration class or configuration file. There is only a single LogManager instantiated in the whole JVM. It