Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Write trace logs to /tmp/nix-installer.log #1169

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ sysctl = "0.5.4"
walkdir = "2.3.3"
indexmap = { version = "2.0.2", features = ["serde"] }
once_cell = "1.19.0"
tracing-appender = "0.2.3"

[dev-dependencies]
eyre = { version = "0.6.8", default-features = false, features = [ "track-caller" ] }
Expand Down
2 changes: 1 addition & 1 deletion src/bin/nix-installer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ async fn main() -> eyre::Result<ExitCode> {

let cli = nix_installer::cli::NixInstallerCli::parse();

cli.instrumentation.setup()?;
let _guard = cli.instrumentation.setup()?;

tracing::info!("nix-installer v{}", env!("CARGO_PKG_VERSION"));

Expand Down
61 changes: 40 additions & 21 deletions src/cli/arg/instrumentation.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use eyre::WrapErr;
use std::error::Error;
use std::io::IsTerminal;

use eyre::WrapErr;
use tracing_error::ErrorLayer;
use tracing_subscriber::{
filter::Directive, layer::SubscriberExt, util::SubscriberInitExt, EnvFilter,
};
use tracing_subscriber::filter::Directive;
use tracing_subscriber::layer::SubscriberExt;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::{EnvFilter, Layer as _};

#[derive(Clone, Default, Debug, clap::ValueEnum)]
pub enum Logger {
Expand Down Expand Up @@ -52,65 +54,81 @@ impl Instrumentation {
.to_string()
}

pub fn setup(&self) -> eyre::Result<()> {
let filter_layer = self.filter_layer()?;
pub fn setup(&self) -> eyre::Result<tracing_appender::non_blocking::WorkerGuard> {
let log_path = format!("/tmp/nix-installer.log");
let trace_log_file = std::fs::File::create(&log_path)?;
let (nonblocking, guard) = tracing_appender::non_blocking(trace_log_file);
let file_layer = tracing_subscriber::fmt::layer()
.with_writer(nonblocking)
.with_filter(EnvFilter::new(format!(
"{}=trace",
env!("CARGO_PKG_NAME").replace('-', "_"),
)));

let registry = tracing_subscriber::registry()
.with(filter_layer)
.with(ErrorLayer::default());
.with(ErrorLayer::default())
.with(file_layer);

let filter_layer = self.filter_layer()?;

match self.logger {
Logger::Compact => {
let fmt_layer = self.fmt_layer_compact();
registry.with(fmt_layer).try_init()?
let fmt_layer = self.fmt_layer_compact(filter_layer);
registry.with(fmt_layer).try_init()?;
},
Logger::Full => {
let fmt_layer = self.fmt_layer_full();
registry.with(fmt_layer).try_init()?
let fmt_layer = self.fmt_layer_full(filter_layer);
registry.with(fmt_layer).try_init()?;
},
Logger::Pretty => {
let fmt_layer = self.fmt_layer_pretty();
registry.with(fmt_layer).try_init()?
let fmt_layer = self.fmt_layer_pretty(filter_layer);
registry.with(fmt_layer).try_init()?;
},
Logger::Json => {
let fmt_layer = self.fmt_layer_json();
registry.with(fmt_layer).try_init()?
let fmt_layer = self.fmt_layer_json(filter_layer);
registry.with(fmt_layer).try_init()?;
},
}

Ok(())
Ok(guard)
}

pub fn fmt_layer_full<S>(&self) -> impl tracing_subscriber::layer::Layer<S>
pub fn fmt_layer_full<S>(&self, filter: EnvFilter) -> impl tracing_subscriber::layer::Layer<S>
where
S: tracing::Subscriber + for<'span> tracing_subscriber::registry::LookupSpan<'span>,
{
tracing_subscriber::fmt::Layer::new()
.with_ansi(std::io::stderr().is_terminal())
.with_writer(std::io::stderr)
.with_filter(filter)
}

pub fn fmt_layer_pretty<S>(&self) -> impl tracing_subscriber::layer::Layer<S>
pub fn fmt_layer_pretty<S>(&self, filter: EnvFilter) -> impl tracing_subscriber::layer::Layer<S>
where
S: tracing::Subscriber + for<'span> tracing_subscriber::registry::LookupSpan<'span>,
{
tracing_subscriber::fmt::Layer::new()
.with_ansi(std::io::stderr().is_terminal())
.with_writer(std::io::stderr)
.pretty()
.with_filter(filter)
}

pub fn fmt_layer_json<S>(&self) -> impl tracing_subscriber::layer::Layer<S>
pub fn fmt_layer_json<S>(&self, filter: EnvFilter) -> impl tracing_subscriber::layer::Layer<S>
where
S: tracing::Subscriber + for<'span> tracing_subscriber::registry::LookupSpan<'span>,
{
tracing_subscriber::fmt::Layer::new()
.with_ansi(std::io::stderr().is_terminal())
.with_writer(std::io::stderr)
.json()
.with_filter(filter)
}

pub fn fmt_layer_compact<S>(&self) -> impl tracing_subscriber::layer::Layer<S>
pub fn fmt_layer_compact<S>(
&self,
filter: EnvFilter,
) -> impl tracing_subscriber::layer::Layer<S>
where
S: tracing::Subscriber + for<'span> tracing_subscriber::registry::LookupSpan<'span>,
{
Expand All @@ -124,6 +142,7 @@ impl Instrumentation {
.with_thread_names(false)
.with_file(false)
.with_line_number(false)
.with_filter(filter)
}

pub fn filter_layer(&self) -> eyre::Result<EnvFilter> {
Expand Down
Loading