Logging in Cilk

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

Our first program demonstrates various logging techniques in Java. We’ll use the built-in java.util.logging package for basic logging and the popular org.slf4j with logback for more advanced, structured logging.

import java.io.ByteArrayOutputStream;
import java.util.logging.*;
import org.slf4j.LoggerFactory;

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

    public static void main(String[] args) {
        // Using the default logger
        logger.info("standard logger");

        // Configuring the logger to include microseconds
        System.setProperty("java.util.logging.SimpleFormatter.format",
                "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS.%1$tL %4$s %2$s %5$s%6$s%n");
        logger.info("with micro");

        // Configuring the logger to include the source file and line number
        System.setProperty("java.util.logging.SimpleFormatter.format",
                "%1$tY-%1$tm-%1$td %1$tH:%1$tM:%1$tS %4$s %2$s %5$s%6$s%n");
        logger.logp(Level.INFO, LoggingExample.class.getSimpleName(), "main", "with file/line");

        // Creating a custom logger with a specific name
        Logger myLogger = Logger.getLogger("MyLogger");
        myLogger.info("from myLogger");

        // Changing the name of an existing logger
        myLogger.setUseParentHandlers(false);
        ConsoleHandler handler = new ConsoleHandler();
        handler.setFormatter(new SimpleFormatter() {
            private static final String format = "MyLogger: [%1$tF %1$tT] %2$s %n";

            @Override
            public synchronized String format(LogRecord lr) {
                return String.format(format,
                        new java.util.Date(lr.getMillis()),
                        lr.getMessage()
                );
            }
        });
        myLogger.addHandler(handler);
        myLogger.info("from renamed myLogger");

        // Logging to a custom output (ByteArrayOutputStream in this case)
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        Logger bufLogger = Logger.getLogger("BufferLogger");
        bufLogger.setUseParentHandlers(false);
        StreamHandler streamHandler = new StreamHandler(baos, new SimpleFormatter());
        bufLogger.addHandler(streamHandler);
        bufLogger.info("hello");
        streamHandler.flush();
        System.out.print("from bufLogger: " + baos.toString());

        // Using SLF4J for structured logging (requires additional dependencies)
        org.slf4j.Logger slfLogger = LoggerFactory.getLogger(LoggingExample.class);
        slfLogger.info("hi there");
        slfLogger.info("hello again", "key", "val", "age", 25);
    }
}

This Java example demonstrates various logging techniques:

  1. We start by using the default Java logger.

  2. We then configure the logger to include microseconds in its output.

  3. Next, we set up the logger to include the source file and line number.

  4. We create a custom logger with a specific name.

  5. We demonstrate how to change the format of an existing logger.

  6. We show how to log to a custom output (in this case, a ByteArrayOutputStream).

  7. Finally, we use SLF4J for structured logging, which is similar to the slog package in the original example.

To run this program, you’ll need to include the SLF4J and Logback libraries in your classpath. You might also need to configure Logback for JSON output to fully replicate the slog JSON output from the original example.

The output will vary depending on the exact time the program is run, but it will look something like this:

2023-08-22 10:45:16 INFO LoggingExample standard logger
2023-08-22 10:45:16.904 INFO LoggingExample with micro
2023-08-22 10:45:16 INFO LoggingExample with file/line
Aug 22, 2023 10:45:16 AM LoggingExample main
INFO: from myLogger
MyLogger: [2023-08-22 10:45:16] from renamed myLogger 
from bufLogger: Aug 22, 2023 10:45:16 AM BufferLogger info
INFO: hello

10:45:16.904 [main] INFO LoggingExample - hi there
10:45:16.904 [main] INFO LoggingExample - hello again key=val age=25

Note that the exact output format for the SLF4J logger will depend on your Logback configuration.