diff --git a/linkd/Cargo.toml b/linkd/Cargo.toml index 13bf00a99..00e09f25a 100644 --- a/linkd/Cargo.toml +++ b/linkd/Cargo.toml @@ -19,8 +19,10 @@ test = false [dependencies] anyhow = "1.0" base64 = "0.13" +env_logger = "0.9" futures = "0.3" lazy_static = "1.4" +log = "0.4" nix = "0.22" signal-hook = { version = "0.3", default-features = false } signal-hook-tokio = { version = "0.3", features = [ "futures-v0_3" ] } diff --git a/linkd/src/bin/linkd.rs b/linkd/src/bin/linkd.rs index 707f705d4..4aafe4dd9 100644 --- a/linkd/src/bin/linkd.rs +++ b/linkd/src/bin/linkd.rs @@ -11,18 +11,17 @@ use signal_hook_tokio::Signals; use structopt::StructOpt as _; use tokio::{net::UnixStream, spawn, sync::mpsc}; use tracing::info; -use tracing_subscriber::FmtSubscriber; use librad::{ crypto::BoxedSigner, net::{discovery, peer::Peer}, }; -use linkd::{args::Args, cfg::Cfg, protocol, signals, socket_activation}; +use linkd::{args::Args, cfg::Cfg, logging, protocol, signals, socket_activation}; #[tokio::main] async fn main() -> anyhow::Result<()> { - tracing::subscriber::set_global_default(FmtSubscriber::builder().finish())?; + logging::init(); let args = Args::from_args(); let cfg: Cfg = Cfg::from_args::(args).await?; diff --git a/linkd/src/lib.rs b/linkd/src/lib.rs index 5b390e596..7351b94eb 100644 --- a/linkd/src/lib.rs +++ b/linkd/src/lib.rs @@ -5,6 +5,7 @@ pub mod args; pub mod cfg; +pub mod logging; pub mod protocol; pub mod signals; pub mod socket_activation; diff --git a/linkd/src/logging.rs b/linkd/src/logging.rs new file mode 100644 index 000000000..fff31bd02 --- /dev/null +++ b/linkd/src/logging.rs @@ -0,0 +1,51 @@ +// Copyright © 2021 The Radicle Link Contributors +// +// This file is part of radicle-link, distributed under the GPLv3 with Radicle +// Linking Exception. For full terms see the included LICENSE file. + +use std::env; + +use log::{log_enabled, Level}; +use tracing::subscriber::set_global_default as set_subscriber; +use tracing_subscriber::{EnvFilter, FmtSubscriber}; + +/// Initialise logging / tracing +/// +/// The `TRACING_FMT` environment variable can be used to control the log +/// formatting. Supported values: +/// +/// * "pretty": [`tracing_subscriber::fmt::format::Pretty`] +/// * "compact": [`tracing_subscriber::fmt::format::Compact`] +/// * "json": [`tracing_subscriber::fmt::format::Json`] +/// +/// If the variable is not set, or set to any other value, the +/// [`tracing_subscriber::fmt::format::Full`] format is used. +pub fn init() { + if env_logger::builder().try_init().is_ok() { + if env::var("RUST_LOG").is_err() { + env::set_var("RUST_LOG", "debug"); + } + + let mut builder = FmtSubscriber::builder() + .with_env_filter(EnvFilter::from_default_env()) + .with_test_writer(); + if log_enabled!(target: "librad", Level::Trace) { + builder = builder.with_thread_ids(true); + } else if env::var("TRACING_FMT").is_err() { + let default_format = if env::var("CI").is_ok() { + "compact" + } else { + "pretty" + }; + env::set_var("TRACING_FMT", default_format); + } + + match env::var("TRACING_FMT").ok().as_deref() { + Some("pretty") => set_subscriber(builder.pretty().finish()), + Some("compact") => set_subscriber(builder.compact().finish()), + Some("json") => set_subscriber(builder.json().flatten_event(true).finish()), + _ => set_subscriber(builder.finish()), + } + .expect("setting tracing subscriber failed") + } +}