Skip to content

Latest commit

 

History

History

java-core-logging

java-core-logging

Showcasing Java's core logging facilities: java.util.logging (JUL) and java.lang.System.Logger.

Why?

The surface area of logging solutions in the Java ecosystem is vast. Java's own standard library for logging is infrequently used compared to logging frameworks like Log4j, Logback and the logging facade SLF4J but it is still important to learn in my opinion.

Instructions

Follow these instructions to build and run the program.

  1. Pre-requisite: Java 21
  2. Build the program distribution
    • ./gradlew installDist
  3. Run the program
    • build/install/java-core-logging/bin/java-core-logging
    • Notice the logs printed to the console! It will look something like below.
      speciallog's parent logger: java.util.logging.LogManager$RootLogger@490ab905
      speciallog's parent logger: dgroomes.java_core_logging.LoggingConfig$1DeadEndLogger@27c20538
      [07:19:51 PM] FINE      This message should be logged at the log level DEBUG and above
      [07:19:51 PM] INFO      This message should be logged at the log level INFO and above
      [07:19:51 PM] WARNING   This message should be logged at the log level WARNING and above
      [07:19:51 PM] SEVERE    This message should be logged at the log level ERROR and above
      [speciallog] Hello! This message was logged on the 'speciallogger'. Notice how the format is different than the other logs.
      
  4. Study the configuration
    • Read the build.gradle.kts file and notice the system properties used when running the program
    • Read the custom-logging.properties file and notice the log level and file handler configurations

Tips

A great way to learn about and explore Java's core logging facilities is to make a copy of Java's default logging configuration file:

cp "$JAVA_HOME/conf/logging.properties" my-custom-logging.properties

Then, open the file and tweak some property values. Then, execute your Java program and pass a reference to your custom configuration file via a Java system property:

-Djava.util.logging.config.file=my-custom-logging.properties

Wish List

General clean-ups, TODOs and things I wish to implement for this project:

  • DONE Log messages with a parameter. Remember, MessageFormatter is used under-the-hood and supports format strings like {1}.
  • DONE Use a custom format string. I don't like the two-line default.
  • DONE Consider (update: yes, do it) coding to java.lang.System.Logger
  • DONE Defect. The log level isn't working. It should printing FINE logs but isn't.
  • DONE Is there a way to create different formatters? Answer: yes, but multiple handlers and formattes are needed.
  • DONE Defect. The speciallog statement is logged twice.

Reference