-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Alexander Simmerl <[email protected]>
- Loading branch information
Showing
4 changed files
with
56 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
|
||
pub mod args; | ||
pub mod cfg; | ||
pub mod logging; | ||
pub mod protocol; | ||
pub mod signals; | ||
pub mod socket_activation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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") | ||
} | ||
} |