Skip to content
This repository has been archived by the owner on Mar 24, 2024. It is now read-only.

Commit

Permalink
add log format control
Browse files Browse the repository at this point in the history
  • Loading branch information
ThatsNoMoon committed Aug 7, 2022
1 parent c1b5f3b commit e6b44c9
Show file tree
Hide file tree
Showing 5 changed files with 118 additions and 32 deletions.
15 changes: 14 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "highlights"
version = "2.1.1"
version = "2.1.2"
authors = ["ThatsNoMoon <git@thatsnomoon.dev>"]
repository = "https://github.com/ThatsNoMoon/highlights"
license = "OSL-3.0"
Expand Down Expand Up @@ -43,7 +43,7 @@ serde_json = { version = "1.0", optional = true }
tinyvec = { version = "1.5", features = ["alloc"] }
tracing = "0.1"
tracing-opentelemetry = { version = "0.17", optional = true }
tracing-subscriber = "0.3"
tracing-subscriber = { version = "0.3", features = ["json"] }

[dependencies.config]
version = "0.13"
Expand Down
2 changes: 2 additions & 0 deletions example_config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ sample_ratio = 1.0
level = "WARN"
# Whether or not to use ANSI color codes (may not work on Windows)
color = true
# Format of standard output logging (can be "compact", "pretty", or "json")
format = "compact"
[logging.filters]
# The `highlights` crate will log at INFO instead
highlights = "INFO"
Expand Down
82 changes: 53 additions & 29 deletions src/logging/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,15 @@
use anyhow::Result;
#[cfg(any(feature = "monitoring", feature = "reporting"))]
use tracing::warn;
use tracing::Metadata;
use tracing::{Metadata, Subscriber};
use tracing_subscriber::{
filter::FilterFn,
layer::{Layer, SubscriberExt},
layer::{Layer, Layered, SubscriberExt},
registry::LookupSpan,
util::SubscriberInitExt,
};

use crate::settings::{settings, Settings};
use crate::settings::{settings, LogFormat, Settings};

#[cfg(feature = "monitoring")]
mod monitoring;
Expand Down Expand Up @@ -49,38 +50,61 @@ fn use_filters(settings: &Settings, metadata: &Metadata) -> bool {
/// This initializes [`reporting`] and [`monitoring`], if
/// enabled, as well as basic stdout logging.
pub(crate) fn init() -> Result<()> {
let subscriber = tracing_subscriber::registry().with(
tracing_subscriber::fmt::layer()
.with_ansi(settings().logging.color)
.with_filter({
let settings = settings();
FilterFn::new(|metadata| use_filters(settings, metadata))
}),
);
let fmt =
tracing_subscriber::fmt::layer().with_ansi(settings().logging.color);

#[cfg(feature = "monitoring")]
let (is_monitoring, subscriber) = {
let layer = monitoring::init()?;
(layer.is_some(), subscriber.with(layer))
let filter = {
let settings = settings();
FilterFn::new(|metadata| use_filters(settings, metadata))
};

#[cfg(feature = "reporting")]
let (is_reporting, subscriber) = {
let layer = reporting::init();
(layer.is_some(), subscriber.with(layer))
};
fn init_rest<L, S>(subscriber: Layered<L, S>) -> Result<()>
where
L: Layer<S> + Send + Sync + 'static,
S: Subscriber + for<'span> LookupSpan<'span> + Send + Sync + 'static,
{
#[cfg(feature = "monitoring")]
let (is_monitoring, subscriber) = {
let layer = monitoring::init()?;
(layer.is_some(), subscriber.with(layer))
};

subscriber.try_init()?;
#[cfg(feature = "reporting")]
let (is_reporting, subscriber) = {
let layer = reporting::init();
(layer.is_some(), subscriber.with(layer))
};

#[cfg(feature = "monitoring")]
if !is_monitoring {
warn!("Jaeger agent address not provided; not reporting traces");
}
subscriber.try_init()?;

#[cfg(feature = "monitoring")]
if !is_monitoring {
warn!("Jaeger agent address not provided; not reporting traces");
}

#[cfg(feature = "reporting")]
if !is_reporting {
warn!("Webhook URL is not present, not reporting panics");
#[cfg(feature = "reporting")]
if !is_reporting {
warn!("Webhook URL is not present, not reporting panics");
}

Ok(())
}

Ok(())
match &settings().logging.format {
LogFormat::Compact => {
let subscriber = tracing_subscriber::registry()
.with(fmt.compact().with_filter(filter));
init_rest(subscriber)
}
LogFormat::Pretty => {
let subscriber = tracing_subscriber::registry()
.with(fmt.pretty().with_filter(filter));
init_rest(subscriber)
}
LogFormat::Json => {
let subscriber = tracing_subscriber::registry()
.with(fmt.json().with_filter(filter));
init_rest(subscriber)
}
}
}
47 changes: 47 additions & 0 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,51 @@ mod level {
}

use level::{deserialize_level_filter, deserialize_level_filters};

mod log_format {
use std::fmt;

use serde::{de, Deserialize, Deserializer};

#[derive(Debug)]
pub(crate) enum LogFormat {
Compact,
Pretty,
Json,
}

/// Visitor to deserialize a `LevelFilter` from a string.
struct LogFormatVisitor;
impl<'de> de::Visitor<'de> for LogFormatVisitor {
type Value = LogFormat;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
write!(formatter, "a log format (compact, pretty, json)")
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: de::Error,
{
match v {
"compact" | "COMPACT" => Ok(LogFormat::Compact),
"pretty" | "PRETTY" => Ok(LogFormat::Pretty),
"json" | "JSON" => Ok(LogFormat::Json),
_ => Err(E::invalid_value(de::Unexpected::Str(v), &self)),
}
}
}

impl<'de> Deserialize<'de> for LogFormat {
fn deserialize<D>(d: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
d.deserialize_str(LogFormatVisitor)
}
}
}

pub(crate) use log_format::LogFormat;
#[cfg(feature = "monitoring")]
pub(crate) use user_address::UserAddress;

Expand Down Expand Up @@ -259,6 +304,8 @@ pub(crate) struct LoggingSettings {

/// Whether or not to use ANSI color codes.
pub(crate) color: bool,
/// Standard output logging format.
pub(crate) format: LogFormat,
}

/// Settings for the database.
Expand Down

0 comments on commit e6b44c9

Please sign in to comment.