2022-05-05 15:50:44 +02:00
|
|
|
use serde::{Deserialize, Serialize};
|
2022-07-24 16:39:33 +02:00
|
|
|
use std::path::Path;
|
2022-07-24 16:07:49 +02:00
|
|
|
use tracing_subscriber::fmt::format::Format;
|
2022-06-21 22:23:55 +02:00
|
|
|
use tracing_subscriber::prelude::*;
|
2022-07-24 16:07:49 +02:00
|
|
|
use tracing_subscriber::reload::Handle;
|
2022-07-24 16:39:33 +02:00
|
|
|
use tracing_subscriber::{reload, EnvFilter};
|
2022-03-20 21:22:15 +01:00
|
|
|
|
|
|
|
#[derive(Debug, Clone, Serialize, Deserialize)]
|
|
|
|
pub struct LogConfig {
|
|
|
|
#[serde(default, skip_serializing_if = "Option::is_none")]
|
|
|
|
/// Log filter string in the tracing format `target[span{field=value}]=level`.
|
|
|
|
/// lvalue is optional and multiple filters can be combined with comma.
|
|
|
|
/// e.g. `warn,diflouroborane::actors=debug` will only print `WARN` and `ERROR` unless the
|
|
|
|
/// message is logged in a span below `diflouroborane::actors` (i.e. by an actor task) in
|
|
|
|
/// which case `DEBUG` and `INFO` will also be printed.
|
|
|
|
pub filter: Option<String>,
|
|
|
|
|
|
|
|
pub format: String,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for LogConfig {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
filter: None,
|
|
|
|
format: "full".to_string(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-24 16:07:49 +02:00
|
|
|
pub enum LogOutput<'a> {
|
|
|
|
Journald,
|
|
|
|
Stdout,
|
|
|
|
File(&'a Path),
|
|
|
|
}
|
|
|
|
pub struct LogConfig2<'a, F> {
|
|
|
|
output: LogOutput<'a>,
|
|
|
|
filter_str: Option<&'a str>,
|
2022-07-24 16:39:33 +02:00
|
|
|
format: Format<F>,
|
2022-07-24 16:07:49 +02:00
|
|
|
}
|
|
|
|
|
2022-06-21 22:23:55 +02:00
|
|
|
pub fn init(config: &LogConfig) -> console::Server {
|
2022-07-24 16:07:49 +02:00
|
|
|
let subscriber = tracing_subscriber::registry();
|
|
|
|
|
|
|
|
let (console_layer, server) = console::ConsoleLayer::new();
|
|
|
|
let subscriber = subscriber.with(console_layer);
|
2022-06-21 22:23:55 +02:00
|
|
|
|
2023-06-01 23:18:40 +02:00
|
|
|
let tracer = opentelemetry_jaeger::new_agent_pipeline()
|
|
|
|
.with_service_name("bffhd")
|
|
|
|
.install_simple()
|
|
|
|
.expect("failed to setup jaeger tracer");
|
|
|
|
let opentelemetry = tracing_opentelemetry::layer().with_tracer(tracer);
|
|
|
|
let subscriber = subscriber.with(opentelemetry);
|
|
|
|
|
2022-03-20 21:22:15 +01:00
|
|
|
let filter = if let Some(ref filter) = config.filter {
|
|
|
|
EnvFilter::new(filter.as_str())
|
|
|
|
} else {
|
|
|
|
EnvFilter::from_env("BFFH_LOG")
|
|
|
|
};
|
|
|
|
|
2022-07-24 16:07:49 +02:00
|
|
|
let format = config.format.to_lowercase();
|
2022-03-20 21:22:15 +01:00
|
|
|
|
2022-07-24 16:07:49 +02:00
|
|
|
let fmt_layer = tracing_subscriber::fmt::layer();
|
|
|
|
|
|
|
|
match format.as_ref() {
|
|
|
|
"pretty" => {
|
2022-07-24 16:39:33 +02:00
|
|
|
let fmt_layer = fmt_layer.pretty().with_filter(filter);
|
2022-07-24 16:07:49 +02:00
|
|
|
subscriber.with(fmt_layer).init();
|
|
|
|
}
|
|
|
|
"compact" => {
|
2022-07-24 16:39:33 +02:00
|
|
|
let fmt_layer = fmt_layer.compact().with_filter(filter);
|
2022-07-24 16:07:49 +02:00
|
|
|
subscriber.with(fmt_layer).init();
|
|
|
|
}
|
|
|
|
_ => {
|
2022-07-24 16:39:33 +02:00
|
|
|
let fmt_layer = fmt_layer.with_filter(filter);
|
2022-07-24 16:07:49 +02:00
|
|
|
subscriber.with(fmt_layer).init();
|
|
|
|
}
|
|
|
|
}
|
2022-06-21 22:23:55 +02:00
|
|
|
|
|
|
|
tracing::info!(format = format.as_str(), "Logging initialized");
|
|
|
|
|
|
|
|
server
|
2022-05-05 15:50:44 +02:00
|
|
|
}
|