Logging in Fortress

Here’s the translation of the Go logging example to Java, formatted in Markdown suitable for Hugo:

Java provides various tools for logging in applications. The most commonly used are the built-in java.util.logging package and third-party libraries like Log4j or SLF4J. In this example, we’ll use java.util.logging for simplicity.

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

public class LoggingExample {
    public static void main(String[] args) throws IOException {
        // The Logger class is the main entry point for logging operations
        Logger logger = Logger.getLogger(LoggingExample.class.getName());

        // By default, loggers will log to console
        logger.info("standard logger");

        // You can set the logging level
        logger.setLevel(Level.FINE);
        logger.fine("This is a finer-grained informational message");

        // You can add handlers to loggers to define where log messages go
        FileHandler fileHandler = new FileHandler("myapp.log");
        logger.addHandler(fileHandler);

        // You can set a formatter to customize log message format
        SimpleFormatter formatter = new SimpleFormatter();
        fileHandler.setFormatter(formatter);

        logger.info("This will be logged to both console and file");

        // You can create a custom logger
        Logger customLogger = Logger.getLogger("MyCustomLogger");
        customLogger.setLevel(Level.ALL);

        ConsoleHandler consoleHandler = new ConsoleHandler();
        consoleHandler.setLevel(Level.ALL);
        customLogger.addHandler(consoleHandler);

        customLogger.info("Message from custom logger");

        // Log messages with additional parameters
        customLogger.log(Level.INFO, "User logged in", new Object[]{"username", "John", "id", 12345});
    }
}

To run this program, save it as LoggingExample.java and use javac to compile it, then java to run it:

$ javac LoggingExample.java
$ java LoggingExample

Sample output (the date and time will depend on when you run the example):

Jun 22, 2023 10:45:16 AM LoggingExample main
INFO: standard logger
Jun 22, 2023 10:45:16 AM LoggingExample main
FINE: This is a finer-grained informational message
Jun 22, 2023 10:45:16 AM LoggingExample main
INFO: This will be logged to both console and file
Jun 22, 2023 10:45:16 AM LoggingExample main
INFO: Message from custom logger
Jun 22, 2023 10:45:16 AM LoggingExample main
INFO: User logged in

In this Java example:

  1. We use the java.util.logging package, which provides functionality similar to Go’s log package.

  2. The Logger class is the main entry point for logging operations, similar to the log package in Go.

  3. We can set logging levels (like FINE, INFO, etc.) which is analogous to setting flags in Go’s logger.

  4. Handlers in Java logging are similar to the concept of setting output writers in Go. We can add multiple handlers to log to different destinations (console, file, etc.).

  5. Formatters in Java allow customizing the log message format, which is similar to setting prefixes and flags in Go.

  6. While Java doesn’t have a direct equivalent to Go’s structured logging (slog), we can achieve similar functionality by passing arrays of objects to the log method.

Remember that in real-world applications, it’s common to use more robust logging frameworks like Log4j or SLF4J, which offer more features and flexibility than the built-in logging package.