Overview

6 Logging, tracing, and debugging

The chapter introduces application instrumentation as a practical way to understand what a Rust web service is doing while it runs. It explains that the compiler catches many problems, but not business logic issues, runtime failures, or the need to inspect how requests move through the system. To support that, the text first focuses on logging: where logs should go, how standard output differs from standard error, why diagnostics are usually written to stderr, and why structured, level-based logging is more useful than simple print statements. It also shows how logging can be routed to the terminal, a file, or other destinations, and why JSON logs are especially helpful when another service must parse them.

The chapter then walks through building logging into a Warp-based web service in Rust, starting with the basic facade-based logging approach and then improving it with more flexible libraries. It demonstrates how the log crate works with an implementation such as env_logger, how environment variables control log levels, and how enabling more verbose levels reveals framework and HTTP internals. When env_logger proves too limited for file output and configuration, the text swaps to log4rs and shows how a YAML config can send logs to both console and file, adjust levels at runtime, and format output as JSON. It also adds request-level logging to the /questions route, first by logging method, path, status, elapsed time, remote address, and headers, and then by attaching a request ID so logs from one request can be correlated across multiple statements. This leads to the realization that manual logging can become noisy and awkward in asynchronous applications.

To address that complexity, the chapter shifts from ordinary logging to tracing, which is presented as a better fit for asynchronous Rust services. Tracing is introduced through three key ideas: spans, events, and subscribers. Spans represent a request’s lifetime or a nested operation, events are the log-like records that occur within spans, and subscribers collect and format them. The chapter shows how to replace the earlier logging setup with tracing and tracing-subscriber, configure an environment filter, and use Warp’s trace support so incoming requests are automatically wrapped in spans. The instrument macro is used to reduce boilerplate and automatically attach events to the right span, while the final section introduces debugging as a separate but complementary tool. It compares command-line debugging with GDB and LLDB, then shows how to debug a Rust web service using breakpoints, inspect variables, and step through execution in both the terminal and Visual Studio Code.

A running web service has many points of failure. Each step should produce enough logs for later investigation.
06-01
When using a crate for logging, chances are high that you will use the logging macros from a crate called log.
06-02
A basic tracing workflow consists of three elements: events, which write the logs; spans, which define a period in time and have a start and an end; and a subscriber, which collects all the logs.
06-03
Birds-eye view of the debugging setup. A debugger attaches itself to the process of our Rust application, and uses kernel interrupt events to step in, pause the process, and display the state.
06-04
A running debugging session in Visual Studio Code. We set a breakpoint on line 25 and started the debugging session by clicking the green arrow symbol at the top left.
06-05
The Step Over button highlights each following step in the code, and even brings up internal crates if the next function being called is inside a third-party library. This way, you get an inside tour of each request you want to follow.
06-06

Summary

  • Logging is crucial in production so you can introspect reported bugs or spot malicious behavior.
  • The log crate uses the facade pattern, which means you use the log crate as the API and a logging crate for the actual implementation. This gives you the advantage of be able to change the logging crate later while still keeping the same log functions in your code.
  • Rust has a variety of logging crates that offer logging to files, to the console, or other output streams.
  • A crate called Tracing is better suited for asynchronous applications. This gives you better abstraction layers and the ability to follow through function calls even if they are called in random order.
  • Debugging is a good way to find bugs or to be able to dig deeper into problems.
  • You have the option of using a command-line interface or an IDE like Visual Studio Code to debug your Rust code.

FAQ

What is the main purpose of logging, tracing, and debugging in a Rust web service?They help you inspect what your application is doing at runtime, diagnose errors, follow incoming HTTP requests, and understand why code behaves differently than expected.
Why is logging important even when Rust's compiler already catches many errors?The compiler catches many compile-time issues, but it cannot detect all runtime bugs, business-logic problems, or issues that depend on real requests, data, or external systems.
What is the difference between stdout and stderr in Rust applications?stdout is generally used for normal program output, while stderr is preferred for diagnostics and error messages. Logging libraries commonly write to stderr.
How is logging different from simply using println! in Rust?Logging usually provides structured output, log levels, and better control over where messages go. println! just writes plain text to stdout.
What log levels are commonly used in Rust logging?The chapter highlights debug, info, warn, error, trace, and critical, each representing a different severity or detail level.
How do env_logger and log4rs differ?env_logger is simple and controlled mainly through the RUST_LOG environment variable, while log4rs supports configuration files, multiple appenders, and easier logging to files as well as the console.
What is the facade pattern in Rust logging?The log crate acts as a facade, exposing logging macros while delegating the actual logging work to an implementation crate such as env_logger or log4rs.
Why does the chapter recommend using JSON logs in some cases?JSON logs are easier for machines and log collectors to parse, filter, and process, especially in larger systems or when logs are sent to another service.
What are spans, events, and subscribers in the tracing crate?A span marks a period of work, an event is a log entry inside that span, and a subscriber collects and formats tracing data.
How does the #[instrument] macro help when tracing async functions?It automatically opens and closes a span around the function, helping trace async execution without manually managing spans, which is safer and more accurate in asynchronous code.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Rust Web Development Rust Web Development ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Rust Web Development Rust Web Development ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Rust Web Development Rust Web Development ebook for free