Chapter 5: Making the most of logs: Auditing an app’s behavior
This introduction explains how logs help investigate application behavior, what a useful log message contains, and why logs are a good starting point for investigations.
After this chapter you will be able to
- Logs as execution records. Explain how application logs help reconstruct and investigate behavior, including behavior that occurred in the past or changes when the app is interfered with.
- Logs as investigation starting point. Explain why the author recommends checking logs first and how logs can be combined with other investigation techniques.
- Logs and debugger perspectives. Distinguish how logs and a debugger reveal an application’s execution over time.
- Logged exception investigation. Explain how exception stack traces in logs help identify a potential problem and provide a starting point for further investigation.
- NullPointerException and root cause. Explain what a NullPointerException indicates and why its occurrence should prompt an investigation of the missing reference rather than an immediate local fix.
- Exception stack trace logging. Describe how to print an exception stack trace at the start of a method to reveal its execution stack without throwing the exception.
- Execution Time Measurement. Explain how to use logs to measure how long a particular instruction or set of instructions takes to execute.
- Slow Query Parameter Diagnosis. Describe how logging a database query and its execution time can help identify parameter values associated with a performance issue.
- Interference in multithreaded investigations. Explain why logs can be useful when investigating a multithreaded app whose behavior changes under a debugger or profiler.
- Operation ordering and thread identification. Describe how timestamps and thread names in logs help reconstruct multithreaded execution, and why descriptive thread names are useful.
- Purpose of log persistence. Explain why applications store log messages and how the storage choice affects their use.
- NoSQL logging tradeoffs. Explain the performance and consistency tradeoff of storing logs in NoSQL databases, and describe the role of timestamps and log engines.
- Relational logging and compliance. Explain why relational databases are sometimes used for logs despite their performance cost, including the financial-app example.
- Common logging severity levels. Distinguish error, warn, info, and debug messages by the events or details they represent.
- Selective logging by severity. Explain why an app normally logs the most important messages and enables debug details only when needed.
- Logger appender formatter roles. Explain how the logger, appender, and formatter divide responsibility in the Log4j configuration.
- Log4j configuration and thresholds. Describe how the example configures the Root logger and how changing its level affects which messages are written.
- Sensitive Data in Logs. Explain why logging secrets and personal data creates security and privacy risks, and describe how to avoid exposing them.
- Logging Performance Costs. Explain how logging can slow an application, using the section's network and loop example.
- Logging Volume Controls. Describe ways to limit logging overhead while retaining useful diagnostic detail.
- Logging and Code Maintainability. Explain how excessive logging obscures application logic and describe ways to keep logging maintainable.
- Present versus past observation. Distinguish how remote debugging and logging reveal an application's behavior over time.
- Remote investigation prerequisites. Explain the practical requirements for using logs and remote debugging to investigate a remotely executing app.
What it builds on
- Investigating code with a debugger (chapter 2) · Investigating code with a debugger.
- What is the execution stack trace, and how do I use it? (chapter 2) · The execution stack trace as a way to identify an execution path.
- Navigating code with the debugger (chapter 2) · Examining execution while a debugger pauses on a line.
- Investigating in remote environments (chapter 4) · Investigating issues in remote environments.
- Finding issues in remote environments (chapter 4) · Using a debugger to investigate issues in remote environments.
- What is remote debugging? (chapter 4) · Connecting a debugger to an application running in an external environment.
Section by section
Introduction
This introduction explains how logs help investigate application behavior, what a useful log message contains, and why logs are a good starting point for investigations.
- Logs as execution records: Explain how application logs help reconstruct and investigate behavior, including behavior that occurred in the past or changes when the app is interfered with.
Why it matters: Recorded events let you investigate behavior even after it has happened. - Standard log message anatomy: Describe the information a standard-formatted log message contains in addition to its event description.
Why it matters: These fields make a log message easier to use during an investigation. - Logs as investigation starting point: Explain why the author recommends checking logs first and how logs can be combined with other investigation techniques.
Why it matters: A useful starting point can save investigation time without ruling out other tools.
Key terms: log messages, Heisenbugs, timestamp, severity, log message, logs, debugger
5.1 Investigating issues with logs
This section contrasts logs’ view of past execution with a debugger’s focus on the present and explains how timestamps help interpret log messages.
- Logs and debugger perspectives: Distinguish how logs and a debugger reveal an application’s execution over time.
Why it matters: Choosing the right perspective helps you decide how to investigate an issue. - Log timestamp interpretation: Explain why log messages should include timestamps and how time zones affect their interpretation.
Why it matters: Correctly reading timestamps helps you reconstruct when events were logged without being misled by time-zone differences.
Key terms: log messages, debugger, execution stack trace, timestamp, time zone, log message
5.1.1 Using logs to identify exceptions
This section explains how logged exceptions provide a starting point for investigation while cautioning that their root causes may lie elsewhere.
- Logged exception investigation: Explain how exception stack traces in logs help identify a potential problem and provide a starting point for further investigation.
Why it matters: A log can point you toward a problem even after it has occurred. - NullPointerException and root cause: Explain what a NullPointerException indicates and why its occurrence should prompt an investigation of the missing reference rather than an immediate local fix.
Why it matters: Understanding why the reference is missing helps you avoid treating a symptom while leaving the underlying problem in place.
Key terms: logs, exception stack trace, debugger, profiler, NullPointerException, root cause, object instance, reference
5.1.2 Using exception stack traces to identify what calls a method
This section shows how to log an exception stack trace to identify the runtime caller of a method without a debugger.
- Exception stack trace logging: Describe how to print an exception stack trace at the start of a method to reveal its execution stack without throwing the exception.
Why it matters: You can inspect a method’s call chain even when a debugger is unavailable. - Runtime caller identification: Explain how the logged stack trace identifies the execution flow that called a method, and why this helps when remote debugging is unavailable.
Why it matters: The trace narrows many possible callers down to the execution path that actually reached the method.
Key terms: exception stack trace, execution stack trace, execution flow
5.1.3 Measuring time spent to execute a given instruction
This section explains how temporary timing logs can help identify slow code and troublesome database query parameters.
- Execution Time Measurement: Explain how to use logs to measure how long a particular instruction or set of instructions takes to execute.
Why it matters: Timing a specific operation can narrow down where a performance problem occurs. - Slow Query Parameter Diagnosis: Describe how logging a database query and its execution time can help identify parameter values associated with a performance issue.
Why it matters: Connecting slow query executions with their parameter values helps focus the search for a fix. - Temporary Timing Logs: Justify removing timing logs once an investigation is complete.
Why it matters: Removing obsolete investigation logs keeps the code easier to read.
Key terms: log messages, timestamp, execution time, query, query execution time, parameter values, index, logs
5.1.4 Investigating issues in multithreaded architectures
This section explains how logs can help investigate multithreaded execution with less interference and identify the order and threads involved in operations.
- Interference in multithreaded investigations: Explain why logs can be useful when investigating a multithreaded app whose behavior changes under a debugger or profiler.
Why it matters: An investigation tool that changes the behavior you are investigating can make the problem harder to observe. - Operation ordering and thread identification: Describe how timestamps and thread names in logs help reconstruct multithreaded execution, and why descriptive thread names are useful.
Why it matters: Knowing the sequence of operations and which thread performed each one helps you reconstruct what happened.
Key terms: multithreaded architecture, debugger, profiler, logs, timestamp, log messages, thread name
5.2 Implementing logging
This section introduces upcoming discussions of logging implementation practices, including persistence, severity classification, and avoiding problems.
5.2.1 Persisting logs
This section explains why logs must be persisted and compares storage choices by performance, consistency, and usability.
- Purpose of log persistence: Explain why applications store log messages and how the storage choice affects their use.
Why it matters: Persistence makes logs useful for investigating events after they happen. - NoSQL logging tradeoffs: Explain the performance and consistency tradeoff of storing logs in NoSQL databases, and describe the role of timestamps and log engines.
Why it matters: This tradeoff helps you judge whether faster logging is acceptable for your application. - Relational logging and compliance: Explain why relational databases are sometimes used for logs despite their performance cost, including the financial-app example.
Why it matters: Some logs are important enough that their preservation outweighs the performance cost. - File based log storage: Describe why direct logging to files is less common and generally discouraged for current applications.
Why it matters: Recognizing the limitations of file logging helps you evaluate examples and older applications.
Key terms: persistence, log messages, nonrelational database, NoSQL, performance, consistency, timestamp, ELK stack, relational database, financial apps
5.2.2 Defining logging levels and using logging frameworks
This section explains logging severities and demonstrates how to write and configure selective logs with Log4j.
- Common logging severity levels: Distinguish error, warn, info, and debug messages by the events or details they represent.
Why it matters: Choosing an appropriate severity helps make important events visible without treating every detail as critical. - Selective logging by severity: Explain why an app normally logs the most important messages and enables debug details only when needed.
Why it matters: Limiting routine log volume makes it easier to find the information an investigation needs. - Log4j logging setup: Describe how the example adds Log4j and uses a logger to write messages at chosen severities.
Why it matters: A logging framework lets application code assign severities while keeping logging configurable. - Logger appender formatter roles: Explain how the logger, appender, and formatter divide responsibility in the Log4j configuration.
Why it matters: Separating selection, destination, and format makes logging behavior configurable. - Log4j configuration and thresholds: Describe how the example configures the Root logger and how changing its level affects which messages are written.
Why it matters: Understanding the threshold lets you adjust detail without changing every logging call. - Status and level attributes: Distinguish the logger's level attribute from the configuration's status attribute.
Why it matters: Distinguishing these settings prevents changing library diagnostics when you intend to change application logging.
Key terms: logging levels, severities, error, warn, info, debug, log severity pyramid, critical issues, Log4j, Logger
5.2.3 Problems caused by logging and how to avoid them
This section explains security, performance, and maintainability risks caused by logging and ways to reduce them.
- Sensitive Data in Logs: Explain why logging secrets and personal data creates security and privacy risks, and describe how to avoid exposing them.
Why it matters: Logs can expose secrets or make personal data harder to manage. - Logging Performance Costs: Explain how logging can slow an application, using the section's network and loop example.
Why it matters: Even diagnostic messages can make an application substantially slower. - Logging Volume Controls: Describe ways to limit logging overhead while retaining useful diagnostic detail.
Why it matters: Controlling logging volume helps preserve performance while making diagnostic detail available when needed. - Logging and Code Maintainability: Explain how excessive logging obscures application logic and describe ways to keep logging maintainable.
Why it matters: Readable code makes it easier to understand and maintain application behavior.
Key terms: security, privacy, private keys, personal data, secrets vault, I/O stream, log messages, performance, logging levels, logging configuration
5.3 Logs vs. remote debugging
This section compares logs and remote debugging for investigating remotely running applications.
- Present versus past observation: Distinguish how remote debugging and logging reveal an application's behavior over time.
Why it matters: The time perspective helps you choose what evidence to use in an investigation. - Remote investigation prerequisites: Explain the practical requirements for using logs and remote debugging to investigate a remotely executing app.
Why it matters: Each technique can be useful remotely, but each has prerequisites that affect whether you can use it. - Complementary investigation techniques: Explain how logs and remote debugging can be chosen or combined in an investigation.
Why it matters: You do not have to treat logging and debugging as competing choices.
Key terms: debugging, logging, current state, timeline, adding extra logs, remote debugging, network configurations, permissions, logs
5.4 Summary
Where this is used later
- Reading a heap dump (chapter 11) · Investigating issues with logs
- The relevance of integrated log monitoring (chapter 12) · Using logs to identify exceptions
- Identifying slowness in an app’s execution (chapter 6) · Provides a timing-based investigation approach for comparison with identifying slowness using a profiler.
- Reading plain-text thread dumps (chapter 10) · Recognizing thread names helps identify threads when investigating thread dumps.
Concepts explained in this chapter
- logA record of events or messages generated during software execution.
- loggingLogging is the recording of events or information produced during a program's execution.
- Log4jA Java logging library for recording application events with configurable levels, outputs, and formats.
- performance issueA problem in an application’s use of resources or delivery of service, including but not limited to slow responses.
- stack traceA representation of the sequence of method calls active at a particular point in a program's execution.
- NoSQL databaseA database that uses a nonrelational data model rather than the relational model.
- overall execution timeThe elapsed time for which a thread has been alive.
- remote debuggingDebugging a program by inspecting or controlling its execution from a different environment or machine.
- multithreaded programA multithreaded architecture is a program design in which multiple threads execute concurrently.
- interceptorAn aspect is a modular unit of cross-cutting logic that can augment the execution of selected methods under specified conditions.
These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.
