RUST_LOG - controlling log levels from the environment
Learn what the RUST_LOG environment variable does, how tracing reads it at startup, which values to use in development and production, and what happens when it is missing or misspelled.
RUST_LOG is an environment variable that carries a filter string into your program from outside the code. You set it in the shell before you start the program, and the program reads it once at startup and uses it to decide which log events to print. It is the standard way to change logging without recompiling, and nearly every Rust server you ever run will be controlled through it.
Where This Fits
A filter written in code is fixed until the next build. A filter carried in RUST_LOG can change with every run. The variable is the delivery mechanism for the filter rules from the previous page: the rules themselves are the same strings, and this page is about how the string gets into the program, when it is read, and what goes wrong when it is missing or misspelled.
Think of it like a note taped to the outside of a parcel. The program is the parcel, and the note tells the person opening it how to handle what is inside. The note is read once, at the moment the parcel is opened. Changing the note after the parcel is open changes nothing until the next parcel arrives.
The Smallest Working Version
This program reads its filter from RUST_LOG and falls back to info when the variable is not set.
# Cargo.toml
[package]
name = "rust_log_demo"
version = "0.1.0"
edition = "2021"
[dependencies]
tracing = "=0.1.44"
tracing-subscriber = { version = "=0.3.23", features = ["env-filter"] }
// src/main.rs
use tracing_subscriber::EnvFilter;
fn main() {
let filter = EnvFilter::try_from_default_env()
.unwrap_or_else(|_| EnvFilter::new("info"));
tracing_subscriber::fmt()
.with_env_filter(filter)
.init();
tracing::trace!("trace event");
tracing::debug!("debug event");
tracing::info!("info event");
tracing::warn!("warn event");
tracing::error!("error event");
}
Run it three ways and watch the output change with nothing recompiled.
With no variable set, the fallback supplies info, and three lines appear.
$ cargo run
2026-11-14T11:47:31.386584Z INFO envfilter: info event
2026-11-14T11:47:31.386649Z WARN envfilter: warn event
2026-11-14T11:47:31.386660Z ERROR envfilter: error event
With RUST_LOG=debug, four lines appear.
$ RUST_LOG=debug cargo run
2026-11-14T11:47:31.392226Z DEBUG envfilter: debug event
2026-11-14T11:47:31.392255Z INFO envfilter: info event
2026-11-14T11:47:31.394411Z WARN envfilter: warn event
2026-11-14T11:47:31.394429Z ERROR envfilter: error event
With RUST_LOG=warn, two lines appear.
$ RUST_LOG=warn cargo run
2026-11-14T11:47:31.814777Z WARN envfilter: warn event
2026-11-14T11:47:31.814821Z ERROR envfilter: error event
All three runs were verified by running the same program. The code never changed. Only the variable changed, and the output followed it. The target in the lines is the binary name of the test program; your own program shows your own crate name there.
Changing output without recompiling means RUST_LOG works:
If the same binary prints different lines under different RUST_LOG values, the variable is being read and the filter is live. If the output never changes, the subscriber is probably built with a fixed level instead of a filter, which is the first mistake in the mistakes section below.
How the Variable Is Read
RUST_LOG is read exactly once, when the filter is constructed, which normally happens on the first line of main. The reading is done by the EnvFilter constructors, and the name of the variable is stored in a constant called DEFAULT_ENV inside EnvFilter. You never need to write the string "RUST_LOG" yourself in code, because the constructors already know it.
The reading happens at startup and never again. Setting the variable in the shell after the program has started does nothing to the running program. Exporting a new value in the same terminal affects only programs started after the export. This is a property of environment variables in general, not of tracing: a process receives a copy of its environment when it starts, and later changes in the shell do not reach it.
The variable is also read by the process that runs the program, which means it works the same way whether the program is started by hand, by a script, by a container, or by a service manager. The mechanism that sets the variable changes, but the program side stays the same: one read, at startup, from the process environment.
The Two Ways to Read It
The constructors come in a lossy pair and a strict pair, and the difference is what they do when the variable is missing or malformed. This is the most important distinction on the page, because the wrong choice produces silence that looks like a bug.
EnvFilter::from_default_env() is the lossy constructor. It reads RUST_LOG, drops any directive it cannot parse, and returns a filter. When the variable is not set at all, it returns the default filter, which enables only ERROR. That was verified by running a program built with it and no variable set: only the error line appeared.
// src/main.rs
use tracing_subscriber::EnvFilter;
fn main() {
tracing_subscriber::fmt()
.with_env_filter(EnvFilter::from_default_env())
.init();
tracing::info!("info is hidden with no RUST_LOG set");
tracing::error!("error still shows");
}
EnvFilter::try_from_default_env() is the strict constructor. It reads RUST_LOG and returns a result. The result is an error when the variable is not set, and an error when the value cannot be parsed as a filter. The usual pattern pairs it with a fallback, so the program always has a filter.
// src/main.rs
use tracing_subscriber::EnvFilter;
fn main() {
let filter = EnvFilter::try_from_default_env()
.unwrap_or_else(|_| EnvFilter::new("info"));
tracing_subscriber::fmt()
.with_env_filter(filter)
.init();
tracing::info!("info shows with no RUST_LOG set, because of the fallback");
}
The two constructors look similar, but they answer different questions. The lossy one says the variable is optional and a missing value means quiet logs. The strict one says the variable is optional but a missing value means your chosen default. For a server, the strict form with a fallback is almost always the right choice, because a missing variable should mean sensible logs, not near silence.
There is one case the fallback cannot rescue, and it is covered in its own section below: a value that parses successfully but means something different from what you typed.
fromdefaultenv with no variable means ERROR only:
A program built with from_default_env() and run with no RUST_LOG set prints only ERROR events. Beginners read that as a broken subscriber, but it is the documented default of an empty filter. If your program is silent except for errors, check whether the variable is set before you change any code.
The Values You Actually Type
The value of RUST_LOG is a filter string, exactly the strings from the filter page. This section is a recipe list, not a repeat of the grammar: each recipe is one string, what it does, and when to use it.
A bare level is the starting point. RUST_LOG=info shows INFO and above from every crate. RUST_LOG=debug adds DEBUG lines from every crate, including dependencies, which is often far more output than you expect. RUST_LOG=trace shows everything, and it can flood a terminal so fast that the useful lines scroll away before you can read them.
RUST_LOG=info cargo run
RUST_LOG=debug cargo run
RUST_LOG=error cargo run
A per-crate value is the everyday debugging recipe. It raises your own crate and leaves everything else at the default.
RUST_LOG=info,my_app=debug cargo run
Replace my_app with your crate name, with hyphens turned into underscores. An event from your crate prints at DEBUG, an event from any other crate prints at INFO, and nothing else changes. This was verified by running a program with the value info,envfilter=debug, which printed the debug line from the test binary and kept everything else at INFO.
A silencing value quiets one noisy dependency while the rest stays loud.
RUST_LOG=info,hyper=off cargo run
The hyper=off directive disables the HTTP crate entirely. The same shape works for any dependency that logs more than you want to read. The value debug,hyper=warn is the same idea in the other direction: everything at DEBUG, but the HTTP crate only at WARN.
A module value drills one level deeper than a crate value. The target in a directive is a prefix, so my_app::db=trace enables TRACE for the database module and its submodules, while the rest of my_app stays at its own level.
RUST_LOG=info,my_app::db=trace cargo run
A span value enables extra detail only inside one named unit of work.
RUST_LOG=info,my_app[checkout]=debug cargo run
Events inside a span named checkout print at DEBUG, and events anywhere else print at INFO. This is the quietest way to debug one operation in a busy server.
The table below collects these recipes in one place. The strings are the values, and the target names are examples you replace with your own crate and dependency names.
| RUST_LOG value | What it shows |
|---|---|
info | INFO and above from every crate |
debug | DEBUG and above from every crate |
error | Only errors, from every crate |
info,my_app=debug | Your crate at DEBUG, the rest at INFO |
info,hyper=off | Everything at INFO, the named crate silent |
info,my_app::db=trace | The database module at TRACE, the rest at INFO |
info,my_app[checkout]=debug | DEBUG inside checkout spans, INFO elsewhere |
Setting the Variable in the Shell
The variable is set in the shell that starts the program, and the syntax depends on the shell. On Linux and macOS, putting the assignment before the command sets it for that one command only.
RUST_LOG=debug cargo run
The variable lasts for that single run and is gone afterwards. Exporting it sets it for the whole terminal session, so every program started from that terminal sees it.
export RUST_LOG=debug
cargo run
On Windows PowerShell, the syntax is different.
$env:RUST_LOG="debug"; cargo run
The semicolon separates the assignment from the command, and the value needs quotes. Setting the variable in a system settings panel instead makes it permanent for every program, which is rarely what you want, because a permanent RUST_LOG=trace will flood every Rust program you run.
RUST_LOG is not specific to your program:
The variable is a convention shared by the whole Rust ecosystem, not an Axum setting. Other Rust programs and tools read the same variable, so an exported RUST_LOG=debug in your terminal also turns up the logging of every other Rust tool you run from that terminal, including the build tooling. That is one more reason to set it per command rather than exporting it.
In a container or a service file, the variable is set in the container or service configuration, not in a shell. A Docker run flag, a compose file entry, and a systemd unit all have their own syntax for it, but the program side never changes: the value arrives in the process environment and is read once at startup.
The Plain Builder Ignores RUST_LOG
A subscriber built with tracing_subscriber::fmt().init() never reads the variable. That was verified by running the same program with RUST_LOG=debug and without it: the output was identical, INFO and above, both times. The plain builder holds a fixed maximum level, and no environment variable can change it.
The shortcut tracing_subscriber::fmt::init() is different: it does read the variable, because it builds its subscriber with a filter from the environment. With no variable set it prints only ERROR, and with RUST_LOG=debug it prints DEBUG and above, both verified by running it. The shortcut is handy for a quick experiment, but its no-variable behaviour surprises people, so a real program should use the explicit filter form from the smallest example on this page instead.
The rule is simple to remember. If the subscriber setup mentions EnvFilter or from_default_env, the variable controls the output. If it mentions only fmt() and with_max_level, the variable is ignored. When output does not respond to RUST_LOG, look at the setup code and check which of those two shapes it has.
The Misspelling Trap
The sharpest edge of RUST_LOG is a value that parses without an error but means something other than what you typed. The filter parser treats a bare word as a target name, not as a level. So a misspelled level becomes a target that matches nothing.
$ RUST_LOG=debg cargo run
Running the example program with that value prints nothing at all, verified by running it. The error line does not even appear. The value debg parses successfully as a directive that enables the target debg at TRACE. No code in the program has the target debg, so no event matches, and every line is dropped. The fallback in try_from_default_env().unwrap_or_else(...) never runs, because there was no error to fall back from. The parse succeeded, and the successful parse is the trap.
The same trap catches a value that is a valid filter but names the wrong target. RUST_LOG=myapp=debug when the crate is named my_app enables a target that does not exist, and the program prints only the default level from the rest of the string, or nothing if there is no default. The fix in both cases is the same: check the spelling against the level list and against the crate name, and test the value once by running the program and counting the lines.
A second, smaller trap is a space in the value. The filter parser does not allow spaces around the equals sign, so RUST_LOG="info, hyper = debug" fails to parse. With the lossy constructor, the bad directives are dropped silently. With the strict constructor, the whole read is an error, and the fallback supplies your default. Either way, the value you typed is not the filter that runs. Write RUST_LOG values with no spaces at all.
A silent program after a RUST_LOG change means the value parsed as something else:
If the program printed logs before you set the variable and prints nothing after, do not rebuild or reinstall anything. Print the variable back with echo $RUST_LOG on Linux and macOS, or $env:RUST_LOG in PowerShell, and read it letter by letter. A misspelled level and a mistyped crate name are the two causes, and both are visible in that one line.
RUST_LOG in Development and in Production
In development, the useful values are the loud ones. RUST_LOG=debug while you are building a feature shows the detail you need, and RUST_LOG=info,my_app=debug is the version you reach for when a dependency gets noisy. The variable changes with every run, so you can turn the volume up for one run and back down for the next without touching the code.
In production, the useful values are the quiet ones. A service usually runs with RUST_LOG=info or with a per-crate value such as RUST_LOG=info,my_app=warn, so the log volume stays readable and the storage stays affordable. When something goes wrong, the operator raises the value for one service, reproduces the problem, and lowers it again. The program is never restarted with a new build for this, only with a new variable, which is the whole point of reading the filter from the environment.
Two habits make the production use safe. First, always ship a fallback in code, so a service started without the variable still logs at INFO instead of going nearly silent. Second, never set RUST_LOG=trace for a whole service in production. TRACE is for a single crate or a single span, such as RUST_LOG=info,my_app::db=trace, and a global TRACE can produce more lines in a minute than anyone can read.
Common Beginner Mistakes
Building the subscriber without a filter
// Wrong - RUST_LOG has no effect on this subscriber
fn main() {
tracing_subscriber::fmt().init();
tracing::debug!("hidden no matter what RUST_LOG says");
}
Running this with RUST_LOG=debug still hides the debug line. The fix is the filter form: build the filter from the environment and attach it with .with_env_filter(...).
Expecting a change to affect a running program
Setting RUST_LOG=debug in a second terminal while the server runs in the first terminal changes nothing. The variable is read once at startup. The fix is to stop the server and start it again with the variable set.
Setting the variable after cargo run starts
cargo run
RUST_LOG=debug
The second line runs after the program has already started, so it has no effect on that run. The assignment must come before the command, on the same line, or be exported before the command runs.
Using the wrong crate name
RUST_LOG=my-app=debug cargo run
Crate names with hyphens become underscores in targets, so the directive must read my_app=debug, not my-app=debug. The hyphen form names a target that does not exist, and the debug lines stay hidden.
Forgetting the env-filter feature
// Wrong without features = ["env-filter"] - will not compile
use tracing_subscriber::EnvFilter;
The EnvFilter type and the from_default_env constructors live behind the env-filter feature. Without it, the code does not compile. Add the feature to Cargo.toml as shown at the top of this page.
Testing RUST_LOG Behaviour
The variable itself is hard to test inside a test process, because reading it touches the process environment, and tests share one process. The testable part is the filter parsing, which is what the variable carries. This test file checks that the strings you would put in RUST_LOG parse to the levels you expect.
// tests/rust_log_tests.rs
use tracing_subscriber::filter::LevelFilter;
use tracing_subscriber::EnvFilter;
#[test]
fn info_value_sets_the_maximum_to_info() {
let filter = EnvFilter::new("info");
assert_eq!(filter.max_level_hint(), Some(LevelFilter::INFO));
}
#[test]
fn per_crate_value_raises_the_maximum() {
let filter = EnvFilter::new("info,my_app=debug");
assert_eq!(filter.max_level_hint(), Some(LevelFilter::DEBUG));
}
#[test]
fn misspelled_level_becomes_a_target_and_parses() {
assert!(EnvFilter::try_new("debg").is_ok());
let filter = EnvFilter::new("debg");
assert_eq!(filter.max_level_hint(), Some(LevelFilter::TRACE));
}
Run the tests with cargo test. All three pass. The third test is the trap written as a checkable fact: "debg" parses successfully, and its maximum level hint is TRACE, because it is a target enabled at TRACE, not a level at all. When a value behaves strangely in the shell, add a test like these for the exact string you typed, and the parse result tells you what the program actually read.
Final Complete Example
Here is a small server setup that puts the page together: a filter read from the environment with an INFO fallback, a DEBUG subscriber when the variable says so, and a handler that logs at INFO so its lines appear under every sensible value.
# Cargo.toml
[package]
name = "rust_log_demo"
version = "0.1.0"
edition = "2021"
[dependencies]
axum = "=0.8.9"
tokio = { version = "=1.54.1", features = ["full"] }
tracing = "=0.1.44"
tracing-subscriber = { version = "=0.3.23", features = ["env-filter"] }
// src/main.rs
use axum::{routing::get, Router};
use tracing_subscriber::EnvFilter;
#[tokio::main]
async fn main() {
let filter = EnvFilter::try_from_default_env()
.unwrap_or_else(|_| EnvFilter::new("info"));
tracing_subscriber::fmt()
.with_env_filter(filter)
.init();
let app = Router::new().route("/", get(hello));
let listener = tokio::net::TcpListener::bind("127.0.0.1:3000")
.await
.unwrap();
println!("listening on http://127.0.0.1:3000");
axum::serve(listener, app).await.unwrap();
}
async fn hello() -> &'static str {
tracing::info!("hello handler was reached");
"Hello, world!"
}
Run it with no variable, and a request prints the handler line. Run it with RUST_LOG=error, and the same request prints nothing, because the handler line is INFO and the filter now allows only errors. Run it with RUST_LOG=info,rust_log_demo=debug, and the handler line appears again, with DEBUG available for your own crate when you add a debug call. One binary, three behaviours, no rebuild in between.
Summary
RUST_LOG is a filter string delivered through the environment, read once at startup, and used to decide which events print. Its power is that it changes logging without a rebuild, and its danger is that it fails quietly: a missing variable means ERROR only under the lossy constructor, a misspelled level becomes a target that matches nothing, and a plain builder ignores the variable entirely. The working pattern that avoids all three is one shape: read with try_from_default_env, fall back to info, attach with with_env_filter, and keep the env-filter feature enabled. With that shape in place, the variable becomes a dial you can turn from the shell, and the dial is the difference between a server you can debug in the moment and a server whose logs you can only change by shipping new code.