|
| 1 | +use std::str::{FromStr, ParseBoolError}; |
| 2 | + |
| 3 | +use thiserror::Error; |
| 4 | +use tracing::Subscriber; |
| 5 | +use tracing_subscriber::{EnvFilter, Layer, fmt, registry::LookupSpan}; |
| 6 | + |
| 7 | +/// Logging format to use |
| 8 | +#[derive(Debug, Default)] |
| 9 | +pub enum LoggingFormat { |
| 10 | + Text, |
| 11 | + #[default] |
| 12 | + Json, |
| 13 | +} |
| 14 | + |
| 15 | +#[derive(Debug, Error)] |
| 16 | +#[error("unknown logging format: {0}")] |
| 17 | +pub struct UnknownLoggingFormat(String); |
| 18 | + |
| 19 | +impl FromStr for LoggingFormat { |
| 20 | + type Err = UnknownLoggingFormat; |
| 21 | + |
| 22 | + fn from_str(value: &str) -> Result<Self, Self::Err> { |
| 23 | + match value { |
| 24 | + "text" => Ok(LoggingFormat::Text), |
| 25 | + "json" => Ok(LoggingFormat::Json), |
| 26 | + value => Err(UnknownLoggingFormat(value.to_string())), |
| 27 | + } |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +impl LoggingFormat { |
| 32 | + pub fn from_env() -> Result<LoggingFormat, UnknownLoggingFormat> { |
| 33 | + let format = match std::env::var("DOCBOX_LOGGING_FORMAT") { |
| 34 | + Ok(value) => value, |
| 35 | + Err(_) => return Ok(LoggingFormat::default()), |
| 36 | + }; |
| 37 | + |
| 38 | + format.parse() |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +#[derive(Debug, Default)] |
| 43 | +pub struct LoggingFormatConfig { |
| 44 | + pub format: LoggingFormat, |
| 45 | + pub allow_noisy: bool, |
| 46 | +} |
| 47 | + |
| 48 | +#[derive(Debug, Error)] |
| 49 | +pub enum LoggingFormatConfigError { |
| 50 | + #[error(transparent)] |
| 51 | + LoggingFormat(#[from] UnknownLoggingFormat), |
| 52 | + #[error("failed to parse DOCBOX_LOGGING_ALLOW_NOISY")] |
| 53 | + InvalidAllowNoisy(ParseBoolError), |
| 54 | +} |
| 55 | + |
| 56 | +impl LoggingFormatConfig { |
| 57 | + pub fn from_env() -> Result<Self, LoggingFormatConfigError> { |
| 58 | + let format = LoggingFormat::from_env()?; |
| 59 | + let allow_noisy = std::env::var("DOCBOX_LOGGING_ALLOW_NOISY") |
| 60 | + .ok() |
| 61 | + .map(|value| value.parse::<bool>()) |
| 62 | + .transpose() |
| 63 | + .map_err(LoggingFormatConfigError::InvalidAllowNoisy)? |
| 64 | + .unwrap_or_default(); |
| 65 | + |
| 66 | + Ok(Self { |
| 67 | + format, |
| 68 | + allow_noisy, |
| 69 | + }) |
| 70 | + } |
| 71 | +} |
| 72 | + |
| 73 | +/// Create a formatting layer from the provided format config |
| 74 | +pub fn fmt_layer<S>(config: LoggingFormatConfig) -> Box<dyn Layer<S> + Send + Sync + 'static> |
| 75 | +where |
| 76 | + S: Subscriber + for<'a> LookupSpan<'a>, |
| 77 | +{ |
| 78 | + match config.format { |
| 79 | + LoggingFormat::Text => text_fmt_layer().boxed(), |
| 80 | + LoggingFormat::Json => json_fmt_layer().boxed(), |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +/// Layer the outputs content using a text based formatting |
| 85 | +pub fn text_fmt_layer<S>() -> fmt::Layer<S> { |
| 86 | + fmt::layer() |
| 87 | + // Display source code file paths |
| 88 | + .with_file(true) |
| 89 | + // Display source code line numbers |
| 90 | + .with_line_number(true) |
| 91 | + // Don't display the event's target (module path) |
| 92 | + .with_target(false) |
| 93 | +} |
| 94 | + |
| 95 | +/// Layer that outputs content using a JSON formatting |
| 96 | +pub fn json_fmt_layer<S>() |
| 97 | +-> fmt::Layer<S, fmt::format::JsonFields, fmt::format::Format<fmt::format::Json>> { |
| 98 | + fmt::layer::<S>() |
| 99 | + .json() |
| 100 | + .with_span_list(true) |
| 101 | + // Display source code file paths |
| 102 | + .with_file(true) |
| 103 | + // Display source code line numbers |
| 104 | + .with_line_number(true) |
| 105 | + // Don't display the event's target (module path) |
| 106 | + .with_target(false) |
| 107 | +} |
| 108 | + |
| 109 | +pub fn filter_layer(allow_noisy: bool) -> EnvFilter { |
| 110 | + if allow_noisy { |
| 111 | + return EnvFilter::from_default_env(); |
| 112 | + } |
| 113 | + |
| 114 | + EnvFilter::from_default_env() |
| 115 | + // Increase logging requirements for noisy dependencies |
| 116 | + .add_directive( |
| 117 | + "aws_sdk_secretsmanager=info" |
| 118 | + .parse() |
| 119 | + .expect("directive was invalid"), |
| 120 | + ) |
| 121 | + .add_directive("aws_runtime=info".parse().expect("directive was invalid")) |
| 122 | + .add_directive( |
| 123 | + "aws_smithy_runtime=info" |
| 124 | + .parse() |
| 125 | + .expect("directive was invalid"), |
| 126 | + ) |
| 127 | + .add_directive("hyper_util=info".parse().expect("directive was invalid")) |
| 128 | + .add_directive("aws_sdk_sqs=info".parse().expect("directive was invalid")) |
| 129 | + .add_directive("h2=info".parse().expect("directive was invalid")) |
| 130 | +} |
0 commit comments