Skip to content

Commit 1ba91df

Browse files
committed
add option to allow logging to a file at the specified path
1 parent b23beb9 commit 1ba91df

File tree

3 files changed

+149
-17
lines changed

3 files changed

+149
-17
lines changed

Cargo.lock

Lines changed: 117 additions & 16 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ serde = { version = "1.0.136", features = ["derive"] }
2727
serde_json = "1.0.79"
2828
sha2 = "0.10.2"
2929
tokio = { version = "1.17.0", features = ["rt-multi-thread", "io-util", "macros", "net", "time"] }
30+
time = "0.3.35"
3031
tokio-util = { version = "0.7.1", features = ["codec"] }
3132
tracing = "0.1.32"
33+
tracing-appender = "0.2.3"
3234
tracing-subscriber = "0.3.18"
3335
uuid = { version = "1.2.1", features = ["serde", "v4"] }
3436

src/main.rs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
use anyhow::Result;
22
use bore_cli::{client::Client, server::Server};
33
use clap::{error::ErrorKind, CommandFactory, Parser, Subcommand};
4+
use tracing;
45

56
#[derive(Parser, Debug)]
67
#[clap(author, version, about)]
78
struct Args {
89
#[clap(subcommand)]
910
command: Command,
11+
#[arg(long)]
12+
log_path: Option<String>,
1013
}
1114

1215
#[derive(Subcommand, Debug)]
@@ -81,7 +84,33 @@ async fn run(command: Command) -> Result<()> {
8184
Ok(())
8285
}
8386

87+
fn setup_logging(log_path: Option<&String>) -> Result<tracing_appender::non_blocking::WorkerGuard> {
88+
match log_path {
89+
Some(x) => {
90+
let file_appender = tracing_appender::rolling::daily(x, "bore_server.log");
91+
let (non_blocking, guard) = tracing_appender::non_blocking(file_appender);
92+
let subscriber = tracing_subscriber::fmt()
93+
.with_writer(non_blocking)
94+
.with_ansi(false)
95+
.finish();
96+
tracing::subscriber::set_global_default(subscriber)
97+
.expect("setting up trace logging failed");
98+
Ok(guard)
99+
}
100+
None => {
101+
let (non_blocking, guard) = tracing_appender::non_blocking(std::io::stdout());
102+
let subscriber = tracing_subscriber::fmt()
103+
.with_writer(non_blocking)
104+
.finish();
105+
tracing::subscriber::set_global_default(subscriber)
106+
.expect("setting up trace logging failed");
107+
Ok(guard)
108+
}
109+
}
110+
}
111+
84112
fn main() -> Result<()> {
85-
tracing_subscriber::fmt::init();
113+
// _ to persist the global logger guard
114+
let _guard = setup_logging(Args::parse().log_path.as_ref());
86115
run(Args::parse().command)
87116
}

0 commit comments

Comments
 (0)