SimpleFormatter

SimpleFormatter is a built-in formatter class in Java used to format log records into a simple, readable, text-based format.

Simple Program

import java.util.logging.*;

public class SimpleFormatterExample {
    public static void main(String[] args) {
        Logger logger = Logger.getLogger("SimpleLogger");

        // Create a ConsoleHandler
        ConsoleHandler handler = new ConsoleHandler();

        // Set SimpleFormatter (default)
        handler.setFormatter(new SimpleFormatter());

        // Add handler and disable parent
        logger.addHandler(handler);
        logger.setUseParentHandlers(false);

        logger.info("This is an info message.");
        logger.warning("This is a warning message.");
    }
}

Sample Output:

May 24, 2025 10:00:00 AM SimpleLogger main
INFO: This is an info message.
May 24, 2025 10:00:00 AM SimpleLogger main
WARNING: This is a warning message.Code language: CSS (css)

Problem Statement

LotusJavaPrince is building a Banking Transaction Logger. Mahesh, the lead developer, wants to log all deposit and withdrawal activities with a simple timestamped format for debugging and audit tracking. The log should use SimpleFormatter and be output to a file.

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

class BankAccount {
    private String accountHolder;
    private double balance;
    private Logger logger;

    public BankAccount(String accountHolder) {
        this.accountHolder = accountHolder;
        this.balance = 0.0;

        logger = Logger.getLogger(accountHolder);
        setupLogger();
    }

    private void setupLogger() {
        try {
            FileHandler fileHandler = new FileHandler(accountHolder + "_log.txt", true);
            fileHandler.setFormatter(new SimpleFormatter());
            logger.addHandler(fileHandler);
            logger.setUseParentHandlers(false);  // Prevent console logging
        } catch (IOException e) {
            System.out.println("Logging setup failed: " + e.getMessage());
        }
    }

    public void deposit(double amount) {
        balance += amount;
        logger.info("Deposited: " + amount + " | Balance: " + balance);
    }

    public void withdraw(double amount) {
        if (amount <= balance) {
            balance -= amount;
            logger.info("Withdrawn: " + amount + " | Balance: " + balance);
        } else {
            logger.warning("Withdrawal failed: Insufficient funds. Attempted: " + amount + " | Balance: " + balance);
        }
    }

    public double getBalance() {
        return balance;
    }
}

public class BankTransactionLogger {
    public static void main(String[] args) {
        BankAccount account = new BankAccount("Mahesh");

        account.deposit(1000);
        account.withdraw(400);
        account.withdraw(700);  // Should log a warning
    }
}

Output in Mahesh_log.txt:

May 24, 2025 10:10:00 AM Mahesh deposit
INFO: Deposited: 1000.0 | Balance: 1000.0

May 24, 2025 10:10:02 AM Mahesh withdraw
INFO: Withdrawn: 400.0 | Balance: 600.0

May 24, 2025 10:10:04 AM Mahesh withdraw
WARNING: Withdrawal failed: Insufficient funds. Attempted: 700.0 | Balance: 600.0

In the world of Java logging using java.util.logging, Formatters play a crucial role in converting raw log records into human-readable or machine-readable output. They control how your logs appear—whether it’s a short readable text, a structured XML document, or a fully customized format.

Scroll to Top