trust_dns_util/lib.rs
1// Copyright 2015-2022 Benjamin Fry <benjaminfry@me.com>
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8//! **NOTICE** This project has been rebranded to Hickory DNS and has been moved to the https://github.com/hickory-dns/hickory-dns organization and repo, this crate/binary has been moved to [hickory-util](https://docs.rs/hickory-util/latest/hickory_util/), from `0.24` and onward.
9
10use std::env;
11
12use tracing::metadata::LevelFilter;
13use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};
14
15fn get_env() -> String {
16 env::var("RUST_LOG").unwrap_or_default()
17}
18
19fn get_levels<T: ToString>(bin: &str, level: Option<T>) -> String {
20 let trust_dns_crates = level.map(|level| {
21 format!(
22 "{bin}={level},trust_dns={level}",
23 bin = bin,
24 level = level.to_string().to_lowercase(),
25 )
26 });
27
28 if let Some(trust_dns_crates) = trust_dns_crates {
29 format!(
30 "{trust_dns_crates},{env}",
31 trust_dns_crates = trust_dns_crates,
32 env = get_env()
33 )
34 } else {
35 get_env()
36 }
37}
38
39/// Setup the logging for the given Level of output and all trust-dns crates
40///
41/// # Panic
42///
43/// This will panic if the tracing subscriber can't be registered
44pub fn logger(bin: &str, level: Option<tracing::Level>) {
45 // Setup tracing for logging based on input
46 let subscriber = EnvFilter::builder()
47 .with_default_directive(LevelFilter::OFF.into())
48 .parse(get_levels(bin, level))
49 .expect("failed to configure tracing/logging");
50
51 let formatter = tracing_subscriber::fmt::layer().compact();
52
53 tracing_subscriber::registry()
54 .with(formatter)
55 .with(subscriber)
56 .init();
57}