Skip to content

Commit 2a1f8fa

Browse files
committed
add option to allow logging to a file at the specified path
1 parent 8ad7ee2 commit 2a1f8fa

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
@@ -3,12 +3,15 @@ use std::net::IpAddr;
33
use anyhow::Result;
44
use bore_cli::{client::Client, server::Server};
55
use clap::{error::ErrorKind, CommandFactory, Parser, Subcommand};
6+
use tracing;
67

78
#[derive(Parser, Debug)]
89
#[clap(author, version, about)]
910
struct Args {
1011
#[clap(subcommand)]
1112
command: Command,
13+
#[arg(long)]
14+
log_path: Option<String>,
1215
}
1316

1417
#[derive(Subcommand, Debug)]
@@ -96,7 +99,33 @@ async fn run(command: Command) -> Result<()> {
9699
Ok(())
97100
}
98101

102+
fn setup_logging(log_path: Option<&String>) -> Result<tracing_appender::non_blocking::WorkerGuard> {
103+
match log_path {
104+
Some(x) => {
105+
let file_appender = tracing_appender::rolling::daily(x, "bore_server.log");
106+
let (non_blocking, guard) = tracing_appender::non_blocking(file_appender);
107+
let subscriber = tracing_subscriber::fmt()
108+
.with_writer(non_blocking)
109+
.with_ansi(false)
110+
.finish();
111+
tracing::subscriber::set_global_default(subscriber)
112+
.expect("setting up trace logging failed");
113+
Ok(guard)
114+
}
115+
None => {
116+
let (non_blocking, guard) = tracing_appender::non_blocking(std::io::stdout());
117+
let subscriber = tracing_subscriber::fmt()
118+
.with_writer(non_blocking)
119+
.finish();
120+
tracing::subscriber::set_global_default(subscriber)
121+
.expect("setting up trace logging failed");
122+
Ok(guard)
123+
}
124+
}
125+
}
126+
99127
fn main() -> Result<()> {
100-
tracing_subscriber::fmt::init();
128+
// _ to persist the global logger guard
129+
let _guard = setup_logging(Args::parse().log_path.as_ref());
101130
run(Args::parse().command)
102131
}

0 commit comments

Comments
 (0)