Skip to content

Commit

Permalink
Improve logging setup
Browse files Browse the repository at this point in the history
Signed-off-by: Alexander V. Nikolaev <[email protected]>
  • Loading branch information
avnik committed Aug 29, 2024
1 parent 24cd19d commit 506436a
Show file tree
Hide file tree
Showing 7 changed files with 101 additions and 15 deletions.
58 changes: 55 additions & 3 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ tonic-types = {version="0.11.0"}
tonic-reflection = {version="0.11.0"}
tower = {version = "0.4"}
tracing = "0.1"
tracing-subscriber = {version = "0.3"}
tracing-subscriber = {version = "0.3", features = ["env-filter", "tracing-log", "time", "local-time"]}
tracing-journald = {version =" 0.2.0"}
serde = { version = "1.0.202", features = ["derive"]}
serde_json = "1.0.120"
x509-parser = { version = "0.16" }
Expand Down
7 changes: 3 additions & 4 deletions src/admin/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::pin::Pin;
use std::sync::Arc;
use std::time::Duration;
use tonic::{Code, Response, Status};
use tracing::{error, info};
use tracing::{debug, error, info};

pub use pb::admin_service_server::AdminServiceServer;

Expand Down Expand Up @@ -177,7 +177,7 @@ impl AdminServiceImpl {
async fn monitor_routine(&self) -> anyhow::Result<()> {
let watch_list = self.registry.watch_list();
for entry in watch_list {
info!("Monitoring {}...", &entry.name);
debug!("Monitoring {}...", &entry.name);
match self.get_remote_status(&entry).await {
Err(err) => {
error!(
Expand All @@ -199,7 +199,7 @@ impl AdminServiceImpl {
)
};

info!("Status of {} is {:#?} (updated)", &entry.name, status);
debug!("Status of {} is {:#?} (updated)", &entry.name, status);
// We have immutable copy of entry here, but need update _in registry_ copy
self.registry.update_state(&entry.name, status)?;

Expand All @@ -219,7 +219,6 @@ impl AdminServiceImpl {
watch.tick().await; // First tick fires instantly
loop {
watch.tick().await;
info!("Monitoring...");
if let Err(err) = self.monitor_routine().await {
error!("Error during watch: {}", err);
}
Expand Down
6 changes: 3 additions & 3 deletions src/bin/givc-admin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use givc_common::pb::reflection::ADMIN_DESCRIPTOR;
use std::net::SocketAddr;
use std::path::PathBuf;
use tonic::transport::Server;
use tracing::info;
use tracing::debug;

#[derive(Debug, Parser)] // requires `derive` feature
#[command(name = "givc-admin")]
Expand Down Expand Up @@ -39,10 +39,10 @@ struct Cli {

#[tokio::main]
async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
givc::trace_init();
givc::trace_init()?;

let cli = Cli::parse();
info!("CLI is {:#?}", cli);
debug!("CLI is {:#?}", cli);

let addr = SocketAddr::new(cli.addr.parse().unwrap(), cli.port);

Expand Down
2 changes: 1 addition & 1 deletion src/bin/givc-agent.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ struct Cli {

#[tokio::main]
async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
givc::trace_init();
givc::trace_init()?;

let cli = Cli::parse();
info!("CLI is {:#?}", cli);
Expand Down
2 changes: 1 addition & 1 deletion src/bin/givc-cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ async fn test_subcommands(test: Test, admin: AdminClient) -> anyhow::Result<()>

#[tokio::main]
async fn main() -> std::result::Result<(), Box<dyn std::error::Error>> {
givc::trace_init();
givc::trace_init()?;

let cli = Cli::parse();
info!("CLI is {:#?}", cli);
Expand Down
38 changes: 36 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,40 @@ pub mod pb {
pub use givc_client::endpoint;
pub use givc_common::types;

pub fn trace_init() {
tracing_subscriber::fmt::init();
pub fn trace_init() -> anyhow::Result<()> {
use std::env;
use tracing::Level;
use tracing_subscriber::{filter::LevelFilter, layer::SubscriberExt, EnvFilter, Layer};

let env_filter =
EnvFilter::try_from_env("GIVC_LOG").unwrap_or_else(|_| EnvFilter::from("info"));
let is_debug_log_level = env_filter
.max_level_hint()
.map_or_else(|| false, |level| level >= Level::DEBUG);

let stdout = tracing_subscriber::fmt::layer()
.with_target(is_debug_log_level)
.with_file(is_debug_log_level)
.with_line_number(is_debug_log_level)
.with_thread_ids(is_debug_log_level);

let stdout = if is_debug_log_level {
stdout.pretty().boxed()
} else {
stdout.boxed()
};

// enable journald logging only on release to avoid log spam on dev machines
let journald = match env::var("INVOCATION_ID") {
Err(_) => None,
Ok(_) => tracing_journald::layer().ok(),
};

let subscriber = tracing_subscriber::registry()
.with(journald.with_filter(LevelFilter::INFO))
.with(stdout.with_filter(env_filter));

tracing::subscriber::set_global_default(subscriber)
.expect("tracing shouldn't already have been set up");
Ok(())
}

0 comments on commit 506436a

Please sign in to comment.