Get startedGet started for free

Logging

1. Logging

Let's look at Java logging capability.

2. What is a log

Applications can be complex. They are made up of thousands of lines of code with lots of control flows, classes, and methods. As developers, we build the applications, but we also have to fix them when they are broken. Finding and fixing issues in an application can be difficult. Where do we even start looking for an issue? That's where a log can help. Think of a log as a diary or journal about what has happened in an application. It often documents what code was called on and what exceptions or unexpected results have been encountered.

3. Logging

Java comes with built-in logging capability. As developers, we determine what messages are written to the log in a fashion very similar to how we write messages to System.out. We often want to record tasks the application has accomplished, what resources (like databases or files) it is using, and what problems the application has encountered. Again, like a diary, the entries we write to the log help create a story about what the application has done and this can give us a trail of breadcrumbs to follow when diagnosing issues. The log messages can be saved to a file or a database. This allows developers to compare log entries of today with those of a different time to see if a problem was always there or was it introduced recently.

4. Using Java logging

To use logging, we import `java.util.logging` on classes where we want to write messages to the log. This gives us access to the Logger class. We need to create a Logger object from the Logger class. We call Logger's `getLogger()` method with a name String to get a Logger object as shown here. The name can be any String, but standard practice is to name the Logger object based on the class it is in. Here, since we are in the HelloWorld class, we name our Logger "HelloWorld". The Logger object is our means to write messages to the log which we'll see next.

5. Writting log messages

Once we have the Logger object, we use its .log() method to send text to the log. This is similar to how we use System.out.println to print text to the console. Note each call to .log() includes the text message we want to enter into the log along with a level associated to the message. A log level says how important the log entry is. As we look through a log, the level helps draw our attention to those log messages that might be more informative of an issue we are trying to find. We'll look at the level options in a moment. By default, Java's logging sends our log entries to the console just like System.out.println. While beyond the scope of this course, there are ways to have log entries sent to a file or database for later search and use.

6. Log levels

This table lists the log levels. Most messages are written with INFO level, but if a problem occurs, we may want to log it as SEVERE or WARNING depending on how critical the problem. Log levels allow us to find critical messages in the log more quickly.

7. Let's practice!

Ready to try our hand at logging?