Skip to content

Add per-module log level filtering #8

@oubiwann

Description

@oubiwann

Problem

When the global log level is set to trace or debug, noisy third-party crates (e.g., tokenizers::tokenizer::normalizer) produce millions of log lines that drown out the application's own trace/debug output. Currently, twyg only supports a single global log level with no way to suppress specific modules.

Desired Behavior

Add a module_filters option that maps module path prefixes to log levels, similar to RUST_LOG=info,tokenizers=warn. This allows users to set a verbose global level while suppressing noisy crates:

let opts = OptsBuilder::new()
    .level(LogLevel::Trace)
    .module_filter("tokenizers", LogLevel::Warn)
    .module_filter("hyper", LogLevel::Info)
    .build()
    .unwrap();

Proposed API

  • Opts field: module_filters: Vec<(String, LogLevel)> — ordered list of (prefix, level) pairs
  • OptsBuilder methods:
    • module_filter(prefix, level) — add a single filter
    • module_filters(filters) — set all filters at once
  • Opts accessor: module_filters() -> &[(String, LogLevel)]
  • Serde support: serializable/deserializable with #[serde(default)]

Behavior

  • On each log event, enabled() checks the record's target against prefixes in order
  • First matching prefix determines the effective level for that module
  • Unmatched modules use the global level
  • log::set_max_level() is set to the most permissive of global + all filter levels so messages reach the custom enabled() check

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions