Logging is presented as a practical way to reconstruct how an application behaved after the fact, much like a ship’s logbook or a chess score sheet. The chapter emphasizes that logs are valuable because they preserve a timeline of events, making them especially useful for investigating past incidents, elusive Heisenbugs, long-term behavior, and critical events that need immediate attention. A well-formed log message should include a timestamp, severity, and the source of the message so developers can more easily trace what happened and when.
The chapter then shows how logs can be used directly in troubleshooting. They can reveal exception stack traces, help identify the root cause behind a failure, expose who called a method by printing an execution stack trace, measure how long a particular operation takes, and provide clues in multithreaded code where timing and execution order matter. The main idea is that logs should usually be the first thing checked during an investigation because they often give a strong starting point, even if they do not answer every question by themselves.
After explaining why logs matter, the chapter turns to implementation and caution. It compares persistence options such as files, relational databases, and NoSQL stores, and recommends using logging frameworks instead of raw console printing for better flexibility. It also explains logging levels like error, warn, info, and debug, showing how they reduce noise and let developers control what gets recorded without recompiling the app. Finally, it warns that logs can create security, performance, and maintainability problems if they expose sensitive data, produce too many messages, or clutter the code, and it contrasts logs with remote debugging as complementary techniques, with logging being more production-friendly and less intrusive.
Sailors store events in logs that they can use to determine their route or analyze the crew’s response to a given event. In the same way, apps store log messages so that developers can later analyze a potential issue or discover breaches in the app.
IDE log console. All IDEs have a log console. While logging messages in the console is useful when running the app locally, real-world apps also store logs that are needed to understand how the app behaved at a given time.
The anatomy of a well-formatted log message. In addition to describing a situation or an event, a log message should also contain several other relevant details: the timestamp of when the app logged the message, the event's severity, and where the message was written. Using the details in these logs allows you to more easily investigate a problem.
Whenever you investigate an issue, the first thing you should always do is read the app’s logs. In many cases, the log messages give you a starting point or offer valuable hints on what you should do next to solve the problem.
When investigating an issue with the debugger, you focus on the present. When you use log messages, you focus on a given period in the past. This difference can help you to decide which to use.
A NullPointerException indicates the app execution encountered a behavior that was called without the behaving instance. But that doesn’t mean that the line that produced the exception is also the cause of the problem. The exception could be a consequence of the root cause. You should always look for the root cause instead of locally treating a problem.
Locally solving the problem is in many cases equivalent to sweeping it under the rug. If the root cause remains, more issues can appear later. Remember that an exception in the logs doesn’t necessarily indicate the root cause.
A multithreaded architecture. An app with the capability to use multiple threads running concurrently to process data is a multithreaded app. Unless explicitly synchronized, instructions running on independent threads (A, B, and C) can run in any order.
Using a tool such as a debugger or a profiler interferes with the execution, making some (or all) threads slower. Because of this, the execution often changes, and some instructions may execute in a different order than the scenario you wanted to investigate. In such a case, the tool is no longer useful since you can’t research the behavior you're interested in.
The log severity pyramid. On the top are the critical log messages that usually require immediate attention. The bottom represents the log messages that provide details you’ll rarely need. From the top to the bottom, the log messages become less essential but greater in number. Usually, the debug-level messages are disabled by default, and the developer can choose to enable them if their investigation requires fine-grained details about the app’s execution.
The relationship between the appender, logger, and formatter. A logger uses one or more appenders. The logger decides what to write (e.g., only log messages printed by objects in the package). The logger gives the messages to be written to one or more appenders. Each appender then implements a certain way to store the messages. The appender uses formatters to shape the messages before storing them.
The components in configuration. The logger Root takes all the log messages with severity level info that the app writes. The logger sends the messages to the appender named Console. The appender Console is configured to send the messages to the system terminal. It uses a formatter to attach the timestamp and the severity level to the message before writing it.
Small details can cause big problems. Developers sometimes consider an app’s logging capability harmless by default and disregard the problems logging can introduce. Logging, however, like all the other software capabilities, deals with the data and, wrongly implemented, can affect the app’s functionality and maintainability.
Log messages should not contain secret or private details. No one working on the app or the infrastructure where the app is deployed should access such data. Exposing sensitive details in logs can help a malicious person (hacker) to find easier ways to break the system or create security-related problems.
Summary
- Always check the app’s logs when you start investigating any issue. The logs may indicate what’s wrong or at least give you a starting point for your investigation.
- All log messages should include a timestamp. Remember that in most cases a system doesn’t guarantee the order in which the logs are stored. The timestamp will help you to order the log messages chronologically.
- Avoid saving too many log messages. Not every detail is relevant or helpful in investigating a potential issue, and storing too many log messages can affect the app’s performance and make the code more difficult to read.
- You should implement more logging only if needed. A running app should only log essential messages. If you require more details, you can always enable more logging for a short time.
- An exception in the logs is not necessarily the root of the problem. It could be a consequence of a problem. Research what caused the exception before treating it locally.
- You can use exception stack traces to figure out what called a given method. In large, messy, and difficult-to-understand codebases, this approach can be very helpful and save you time.
- Never write sensitive details (e.g., passwords, private keys, or personal details) in a log message. Logging passwords or private keys introduces security vulnerabilities since anyone with access to the logs can see and use them. Writing personal details such as names, addresses, or phone numbers also may not comply with various government regulations.