Use tracing-subscriber to log only Debug to file

I have a Debug log, but it seems to also get Info, Error, and Debug, when I want it to only have Debug. I'm having a difficult time understanding a lot of tracing-related terminology, so forgive me if this is a simple fix.

use ex_06_bookstore_app::app;
use std::fs::OpenOptions;
use tracing::{info, error, Level, debug};
use tracing_subscriber::{
    Registry, filter

async fn main() {
    let err_file = OpenOptions::new()
    let debug_file = OpenOptions::new()

    let subscriber = Registry::default()
            // stdout layer, to view everything in the console
            // log-error file, to log the errors that arise
            // log-debug file, to log the debug
    // Opens the server
    let listener = tokio::net::TcpListener::bind("").await.unwrap();
    println!("Server is running on localhost:3000");
// ------ The issue ------
    // All three go into log-debug.log
    error!("test"); // Only thing log-error.log gets
    // app is a standard Axum Router that I have inside ``
    axum::serve(listener, app().await).await.unwrap();

the provided LevelFilter will record all events and spans with verbosity greater or equal to the filter level. if you want a filter that matches exact level, you'll have to define your own Filter.

struct DebugOnlyFilter;
impl Filter for DebugOnlyFilter {
    fn enabled(&self, meta: &Metadata<'_>, _: &Context<'_, S>) -> bool {
        meta.level() == &Level::DEBUG
1 Like