-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(logging): centralize logging setup in teepot crate
- Added a new logging module in `teepot` crate. - Removed redundant logging setup code from individual projects. - Updated dependencies and references for logging setup. Signed-off-by: Harald Hoyer <harald@matterlabs.dev>
- Loading branch information
Showing
7 changed files
with
84 additions
and
67 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,4 +11,5 @@ pub mod json; | |
pub mod server; | ||
pub mod sgx; | ||
|
||
pub mod log; | ||
pub mod quote; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright (c) 2024 Matter Labs | ||
|
||
//! Logging related stuff | ||
|
||
use anyhow::Context; | ||
use tracing::level_filters::LevelFilter; | ||
use tracing_log::LogTracer; | ||
use tracing_subscriber::Registry; | ||
use tracing_subscriber::{fmt, prelude::*, EnvFilter}; | ||
|
||
/// A log level parser for clap, with "off", "error", "warn", "info", "debug", "trace" as valid values | ||
#[derive(Clone)] | ||
pub struct LogLevelParser; | ||
|
||
impl clap::builder::TypedValueParser for LogLevelParser { | ||
type Value = LevelFilter; | ||
|
||
fn parse_ref( | ||
&self, | ||
cmd: &clap::Command, | ||
arg: Option<&clap::Arg>, | ||
value: &std::ffi::OsStr, | ||
) -> anyhow::Result<Self::Value, clap::Error> { | ||
clap::builder::TypedValueParser::parse(self, cmd, arg, value.to_owned()) | ||
} | ||
|
||
fn parse( | ||
&self, | ||
cmd: &clap::Command, | ||
arg: Option<&clap::Arg>, | ||
value: std::ffi::OsString, | ||
) -> std::result::Result<Self::Value, clap::Error> { | ||
use std::str::FromStr; | ||
let p = clap::builder::PossibleValuesParser::new([ | ||
"off", "error", "warn", "info", "debug", "trace", | ||
]); | ||
let v = p.parse(cmd, arg, value)?; | ||
|
||
Ok(LevelFilter::from_str(&v).unwrap()) | ||
} | ||
} | ||
|
||
/// Setup standard logging and loglevel for the current crate and the `teepot` crate. | ||
pub fn setup_logging(log_level: &LevelFilter) -> anyhow::Result<()> { | ||
LogTracer::init().context("Failed to set logger")?; | ||
let filter = EnvFilter::builder() | ||
.try_from_env() | ||
.unwrap_or(match *log_level { | ||
LevelFilter::OFF => EnvFilter::new("off"), | ||
_ => EnvFilter::new(format!( | ||
"warn,{crate_name}={log_level},teepot={log_level}", | ||
crate_name = env!("CARGO_CRATE_NAME"), | ||
log_level = log_level | ||
)), | ||
}); | ||
let subscriber = Registry::default() | ||
.with(filter) | ||
.with(fmt::layer().with_writer(std::io::stderr)); | ||
tracing::subscriber::set_global_default(subscriber)?; | ||
|
||
Ok(()) | ||
} |