Skip to content

Add optional file logging, and add logging for heartbeat disconnects #160

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

Open
wants to merge 2 commits 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
133 changes: 117 additions & 16 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ serde = { version = "1.0.136", features = ["derive"] }
serde_json = "1.0.79"
sha2 = "0.10.2"
tokio = { version = "1.17.0", features = ["rt-multi-thread", "io-util", "macros", "net", "time"] }
time = "0.3.35"
tokio-util = { version = "0.7.1", features = ["codec"] }
tracing = "0.1.32"
tracing-appender = "0.2.3"
tracing-subscriber = "0.3.18"
uuid = { version = "1.2.1", features = ["serde", "v4"] }

Expand Down
31 changes: 30 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,15 @@ use std::net::IpAddr;
use anyhow::Result;
use bore_cli::{client::Client, server::Server};
use clap::{error::ErrorKind, CommandFactory, Parser, Subcommand};
use tracing;

#[derive(Parser, Debug)]
#[clap(author, version, about)]
struct Args {
#[clap(subcommand)]
command: Command,
#[arg(long)]
log_path: Option<String>,
}

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

fn setup_logging(log_path: Option<&String>) -> Result<tracing_appender::non_blocking::WorkerGuard> {
match log_path {
Some(x) => {
let file_appender = tracing_appender::rolling::daily(x, "bore_server.log");
let (non_blocking, guard) = tracing_appender::non_blocking(file_appender);
let subscriber = tracing_subscriber::fmt()
.with_writer(non_blocking)
.with_ansi(false)
.finish();
tracing::subscriber::set_global_default(subscriber)
.expect("setting up trace logging failed");
Ok(guard)
}
None => {
let (non_blocking, guard) = tracing_appender::non_blocking(std::io::stdout());
let subscriber = tracing_subscriber::fmt()
.with_writer(non_blocking)
.finish();
tracing::subscriber::set_global_default(subscriber)
.expect("setting up trace logging failed");
Ok(guard)
}
}
}

fn main() -> Result<()> {
tracing_subscriber::fmt::init();
// _ to persist the global logger guard
let _guard = setup_logging(Args::parse().log_path.as_ref());
run(Args::parse().command)
}
2 changes: 2 additions & 0 deletions src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ impl Server {
}

async fn handle_connection(&self, stream: TcpStream) -> Result<()> {
let peer_addr = stream.peer_addr().unwrap().to_string();
let mut stream = Delimited::new(stream);
if let Some(auth) = &self.auth {
if let Err(err) = auth.server_handshake(&mut stream).await {
Expand Down Expand Up @@ -146,6 +147,7 @@ impl Server {
loop {
if stream.send(ServerMessage::Heartbeat).await.is_err() {
// Assume that the TCP connection has been dropped.
info!(peer_addr, "connection dropped after heartbeat timeout");
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Example log events:

2025-03-26T21:40:35.281814Z  INFO control{addr=172.31.11.42:49554}: bore_cli::server: connection dropped after heartbeat timeout peer_addr="172.31.11.42:49554"
2025-03-26T21:40:35.281852Z  INFO control{addr=172.31.11.42:49554}: bore_cli::server: connection exited

This just gives us a little more info about why the client disconnected.

return Ok(());
}
const TIMEOUT: Duration = Duration::from_millis(500);
Expand Down