# Use tracing-subscriber to log only Debug to file

**URL:** <https://users.rust-lang.org/t/use-tracing-subscriber-to-log-only-debug-to-file/104453>\
**Category:** help\
**Created:** [December 26, 2023, 5:28pm UTC](https://users.rust-lang.org/t/use-tracing-subscriber-to-log-only-debug-to-file/104453 "2023-12-26T17:28:55Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![naginipython](https://avatars.discourse-cdn.com/v4/letter/n/73ab20/32.png) [@naginipython](https://users.rust-lang.org/u/naginipython)\
**Post date:** [December 26, 2023, 5:28pm UTC](https://users.rust-lang.org/t/use-tracing-subscriber-to-log-only-debug-to-file/104453/1 "2023-12-26T17:28:55Z")

</div>

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.

```rust
use ex_06_bookstore_app::app;
use std::fs::OpenOptions;
use tracing::{info, error, Level, debug};
use tracing_subscriber::{
    prelude::*,
    fmt,
    layer::Layer,
    Registry, filter
};

#[tokio::main]
async fn main() {
    let err_file = OpenOptions::new()
        .append(true)
        .create(true)
        .open("log-error.log")
        .unwrap();
    let debug_file = OpenOptions::new()
        .append(true)
        .create(true)
        .open("log-debug.log")
        .unwrap();

    let subscriber = Registry::default()
        .with(
            // stdout layer, to view everything in the console
            fmt::layer()
                .compact()
                .with_ansi(true)
        )
        .with(
            // log-error file, to log the errors that arise
            fmt::layer()
                .json()
                .with_writer(err_file)
                .with_filter(filter::LevelFilter::from_level(Level::ERROR))
        )
        .with(
            // log-debug file, to log the debug
            fmt::layer()
                .json()
                .with_writer(debug_file)
                .with_filter(filter::LevelFilter::from_level(Level::DEBUG))
        );
    
    tracing::subscriber::set_global_default(subscriber).unwrap();
    
    // Opens the server
    let listener = tokio::net::TcpListener::bind("127.0.0.1:3000").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
    info!("test2");
    debug!("test3");
    // app is a standard Axum Router that I have inside `lib.rs`
    axum::serve(listener, app().await).await.unwrap();
}

```

---

<div class="post-metadata">

**Author:** ![nerditation](https://avatars.discourse-cdn.com/v4/letter/n/e79b87/32.png) [@nerditation](https://users.rust-lang.org/u/nerditation)\
**Post date:** [December 26, 2023, 5:50pm UTC](https://users.rust-lang.org/t/use-tracing-subscriber-to-log-only-debug-to-file/104453/2 "2023-12-26T17:50:06Z")

</div>

> [@naginipython](#):
>
> ```rust
> .with_filter(filter::LevelFilter::from_level(Level::DEBUG))
> 
> ```

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`.

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

```

---

<div class="post-metadata">

**Author:** ![system](https://sea1.discourse-cdn.com/flex019/user_avatar/users.rust-lang.org/system/32/51372_2.png) [@system](https://users.rust-lang.org/u/system)\
**Post date:** [March 25, 2024, 5:50pm UTC](https://users.rust-lang.org/t/use-tracing-subscriber-to-log-only-debug-to-file/104453/3 "2024-03-25T17:50:48Z")

</div>

Closed on March 25, 2024, 5:50pm UTC

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.
