XMLFormatter

XMLFormatter is a class (commonly from java.util.logging) used to format logging records into XML format. It transforms log entries into a structured XML string. It can also refer to a custom utility class to format any raw XML string into a human-readable, pretty-printed XML format with indentation.

Commonly Used Methods

Simple Program

import java.io.IOException;
import java.util.logging.*;

public class SimpleXMLFormatterExample {
    public static void main(String[] args) throws IOException {
        Logger logger = Logger.getLogger("SimpleXMLLogger");
        
        // FileHandler writes log records to a file
        FileHandler fileHandler = new FileHandler("simpleLog.xml");
        
        // Set XMLFormatter on the FileHandler
        fileHandler.setFormatter(new XMLFormatter());
        
        // Add handler to logger
        logger.addHandler(fileHandler);
        
        // Disable console logging (optional)
        logger.setUseParentHandlers(false);
        
        // Log messages
        logger.info("Info message logged in XML format");
        logger.warning("Warning message logged in XML format");
    }
}

Problem Statement:

LotusJavaPrince is building a Student Attendance System. Mahesh needs to keep detailed attendance logs in XML format for auditing. The logs should include the student’s name, attendance status, and timestamp, stored in separate XML log files per student.

import java.io.IOException;
import java.util.logging.*;

class StudentAttendance {
    private String studentName;
    private Logger logger;

    public StudentAttendance(String studentName) {
        this.studentName = studentName;
        this.logger = Logger.getLogger(studentName);
        setupLogger();
    }

    private void setupLogger() {
        try {
            FileHandler fileHandler = new FileHandler(studentName + "_attendance.xml", true);
            fileHandler.setFormatter(new XMLFormatter());
            logger.addHandler(fileHandler);
            logger.setUseParentHandlers(false);
        } catch (IOException e) {
            System.out.println("Error setting up logger for " + studentName + ": " + e.getMessage());
        }
    }

    public void markAttendance(boolean present) {
        if (present) {
            logger.info(studentName + " marked present.");
        } else {
            logger.warning(studentName + " marked absent.");
        }
    }
}

public class StudentAttendanceSystem {
    public static void main(String[] args) {
        StudentAttendance student1 = new StudentAttendance("Mahesh");
        student1.markAttendance(true);

        StudentAttendance student2 = new StudentAttendance("Sita");
        student2.markAttendance(false);
    }
}

The XMLFormatter class in Java’s logging framework provides a straightforward way to format log records as well-structured XML documents. This makes it ideal for applications that require logs to be machine-readable, easily parsed, or integrated with other XML-based tools and systems.

By using XMLFormatter with handlers like FileHandler, developers can generate persistent XML log files that include detailed information about each log event — such as timestamps, levels, and messages — all wrapped in a clean XML structure.

This formatter is especially useful in enterprise applications, auditing systems, and situations where logs must be exchanged or analyzed automatically. Its automatic generation of XML headers and footers saves development time and reduces errors compared to manual XML logging.

Scroll to Top