3 Basic logging patterns with System.Logger

 

This chapter covers

  • Using the System.Logger for simple use cases
  • Working with log levels in System.Logger
  • Assessing the "pros" and "cons" of System.Logger

Java 9 introduced a new logging framework called System.Logger. Since we have covered all the basics in the previous chapter with "log4print", System.Logger will feel familiar to you.

System.Logger is a lightweight logging framework in the Java Developer Toolkit (JDK). It’s easy to start with, and you can extend it later by integrating it with larger frameworks like Log4j if needed. It is a good choice for small projects or libraries such as:

  • A utility that transforms JSON into Java objects
  • A tool that estimates the CO2 emissions based on flight distance
  • A service that verifies if a database is online
  • A file compressor that zips files into archives

We’ll also explore log levels in more depth. By the end, you’ll know when and how to use a logging framework—and how to apply your knowledge to any project that only needs basic logging needs.

3.1 Receiving a System.Logger instance

Similar to the "log4print" framework from the last chapter, System.Logger uses a LoggerManager concept. As a reminder, a LoggerManager is a factory that gives you access to Logger instances used to create log statements.

In "log4print", we wrote the following code to get a Logger instance:

Logger logger = LoggerManager.getLogger();

3.2 Log Levels for the System.Logger

3.3 Writing a simple log statement

3.4 The AirportApp Skeleton

3.5 Naming the logger in an easy way

3.6 Warning: this shouldn’t happen

3.7 Flow tracing

3.8 Dealing with unexpected problems

3.9 Avoiding unnecessary work

3.10 Supplier saves Strings

3.11 Reading the output

3.12 Pros and Cons of System.Logger

3.13 Summary