espresso_utils/
logging.rs1use clap::{Parser, ValueEnum};
2pub use hotshot::helpers::FmtSubscriber;
3use hotshot::helpers::{initialize_logging, initialize_logging_on_stderr, initialize_logging_with};
4use log_panics::BacktraceMode;
5use tracing_subscriber::Layer;
6
7#[derive(Clone, Copy, Debug, Default, ValueEnum)]
18enum BacktraceLoggingMode {
19 #[default]
20 Full,
21 Compact,
22 Json,
23}
24
25#[derive(Clone, Debug, Default, Parser)]
27pub struct Config {
28 #[clap(long, env = "RUST_LOG_FORMAT")]
29 backtrace_mode: Option<BacktraceLoggingMode>,
30}
31
32impl Config {
33 pub fn from_env() -> Self {
35 Self::parse_from(std::iter::empty::<String>())
36 }
37
38 pub fn init(&self) {
40 initialize_logging();
41 self.install_panic_hook();
42 }
43
44 pub fn init_on_stderr(&self) {
46 initialize_logging_on_stderr();
47 self.install_panic_hook();
48 }
49
50 pub fn init_with_otel<L>(&self, otel_layer: Option<L>)
52 where
53 L: Layer<FmtSubscriber> + Send + Sync + 'static,
54 {
55 initialize_logging_with(otel_layer);
56 self.install_panic_hook();
57 }
58
59 fn install_panic_hook(&self) {
60 if let BacktraceLoggingMode::Json = self.backtrace_mode.unwrap_or_default() {
61 log_panics::Config::new()
62 .backtrace_mode(BacktraceMode::Resolved)
63 .install_panic_hook();
64 }
65 }
66}