Logging configuration through properties files

Java provides the ability to configure the logging behavior externally through .properties files, removing the need to hardcode logger behavior into the Java source code. This enables dynamic control over:

  • Logger levels
  • Handler types and destinations
  • Formatter styles
  • Output files, consoles, or network sockets

Format of Logging Properties File

Java logging reads configuration from a .properties file typically using LogManager. A typical logging properties file includes:

# Global logging level and handlers
.level = WARNING
handlers = java.util.logging.ConsoleHandler

# Configure the ConsoleHandler
java.util.logging.ConsoleHandler.level = WARNING
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

# Logger specific configuration
BankAuditLogger.level = WARNINGCode language: PHP (php)

Simple Program with Properties File

logging.properties(Place logging.properties in your resources directory or classpath.)

# Root logger configuration
handlers = java.util.logging.ConsoleHandler
.level = INFO

# ConsoleHandler configuration
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatterCode language: PHP (php)

SimpleLoggingWithProperties.java

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

public class SimpleLoggingWithProperties {
    private static final Logger logger = Logger.getLogger(SimpleLoggingWithProperties.class.getName());

    public static void main(String[] args) throws IOException {
        LogManager.getLogManager().readConfiguration(SimpleLoggingWithProperties.class.getResourceAsStream("/logging.properties"));

        logger.finest("This is FINEST"); // Not shown
        logger.info("This is INFO");     // Shown
        logger.warning("This is WARNING"); // Shown
    }
}

Problem Statement

LotusJavaPrince creates a secure transaction logging module. Mahesh, the auditor, wants:

  • INFO logs on the console.
  • WARNING and above stored in a rotating file system.
  • All configuration handled via a properties file (no hardcoding).

logging.properties(The file logs are stored in a rotating manner under logs/, and console displays INFO+ messages.)

# Handlers
handlers = java.util.logging.ConsoleHandler, java.util.logging.FileHandler
.level = INFO

# ConsoleHandler setup
java.util.logging.ConsoleHandler.level = INFO
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter

# FileHandler setup
java.util.logging.FileHandler.pattern = logs/bank-log.%u.%g.txt
java.util.logging.FileHandler.limit = 50000
java.util.logging.FileHandler.count = 3
java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter
java.util.logging.FileHandler.level = WARNINGCode language: PHP (php)
BankLoggerWithProperties.java
import java.io.IOException;
import java.util.logging.*;

public class BankLoggerWithProperties {
    private static final Logger logger = Logger.getLogger(BankLoggerWithProperties.class.getName());

    public static void main(String[] args) throws IOException {
        LogManager.getLogManager().readConfiguration(
            BankLoggerWithProperties.class.getResourceAsStream("/logging.properties")
        );

        logger.info("Transaction successful for Mahesh - Rs. 5000");
        logger.warning("Multiple failed login attempts for Mahesh");
        logger.severe("Fraudulent transaction detected for Mahesh");
    }
}

Using a .properties file for logging configuration in Java provides a centralized, flexible mechanism to manage log levels, handlers, and formats without modifying source code. It supports separation of configuration and code, easier maintenance, and is ideal for production environments where log control must be adjusted without recompilation.

Scroll to Top