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.
When using a crate for logging, chances are high that you will use the logging macros from a crate called log.
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.
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.
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.
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.
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.
Rust Web Development ebook for free