Skip to content

Commit 339ce65

Browse files
committed
feat: switch to rolling logs
Remove cli args and output to stdout. Wasn't readable anyway
1 parent 4ef2112 commit 339ce65

File tree

10 files changed

+51
-84
lines changed

10 files changed

+51
-84
lines changed

Cargo.lock

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

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ tokio = { version = "1.42.0", default-features = false }
6161
tokio-util = "0.7.13"
6262
tracing = "0.1.41"
6363
tracing-actix-web = "0.7.11"
64+
tracing-appender = "0.2.3"
6465
tracing-bunyan-formatter = "0.3.10"
6566
tracing-log = "0.2.0"
6667
tracing-subscriber = { version = "0.3", features = ["fmt", "json"] }

crates/chat-app-client/Cargo.toml

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ description = "egui Client for the Demo Chat App"
77

88
[dependencies]
99
anyhow.workspace = true
10-
clap = { workspace = true, features = ["derive"] }
1110
eframe = { workspace = true, features = [ # put all features on their own line
1211
"accesskit", # Make egui compatible with screen readers. NOTE: adds a lot of dependencies.
1312
"default_fonts", # Embed the default egui fonts.
@@ -30,10 +29,9 @@ wykies-time.workspace = true
3029

3130
# native:
3231
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
33-
clap.workspace = true
3432
ewebsock = { workspace = true, features = ["tls", "tokio"] }
3533
tokio = { workspace = true, features = ["rt-multi-thread"] }
36-
tracing-subscriber.workspace = true
34+
tracing-appender.workspace = true
3735

3836
# web:
3937
[target.'cfg(target_arch = "wasm32")'.dependencies]

crates/chat-app-client/src/cli.rs

Lines changed: 0 additions & 13 deletions
This file was deleted.

crates/chat-app-client/src/lib.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ mod suppress_wasm_warnings {
99

1010
mod app;
1111
pub mod background_worker;
12-
pub mod cli;
1312
mod lockout;
1413
mod pages;
1514
mod shortcuts;

crates/chat-app-client/src/main.rs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44
// When compiling natively:
55
#[cfg(not(target_arch = "wasm32"))]
66
fn main() -> eframe::Result<()> {
7-
use clap::Parser;
8-
let args = chat_app_client::cli::Cli::parse();
9-
10-
if let Err(e) = chat_app_client::tracing::init(&args) {
11-
eprintln!("Failed to start tracing: {e}");
12-
}
7+
let _guard = match chat_app_client::tracing::init() {
8+
Ok(guard) => guard,
9+
Err(err_msg) => {
10+
eprintln!("Failed to start tracing: {err_msg:?}");
11+
std::process::exit(84);
12+
}
13+
};
1314

1415
let rt = chat_app_client::background_worker::create_runtime();
1516
let _enter = rt.enter(); // This Guard must be held to call `tokio::spawn` anywhere in the program

crates/chat-app-client/src/tracing.rs

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,22 @@
11
#[cfg(not(target_arch = "wasm32"))]
2-
pub fn init(cli: &super::cli::Cli) -> anyhow::Result<()> {
2+
pub fn init() -> anyhow::Result<tracing_appender::non_blocking::WorkerGuard> {
33
use anyhow::{bail, Context};
44
use wykies_shared::telemetry;
55

6-
fn init_to_file() -> anyhow::Result<()> {
7-
let (file, filename) = telemetry::create_trace_file("chat_app_client")?;
8-
let subscriber =
9-
telemetry::get_subscriber("chat_app_client".into(), "zbus=warn,info", file);
6+
let (writer, path, guard) = telemetry::setup_tracing_writer("chat_app_client")?;
7+
let subscriber = telemetry::get_subscriber("chat_app_client".into(), "zbus=warn,info", writer);
108

11-
// Start logging to file
12-
match telemetry::init_subscriber(subscriber) {
13-
Ok(_) => {
14-
println!(
15-
"Traces being written to: {:?}",
16-
filename
17-
.canonicalize()
18-
.context("trace file canonicalization failed")?
19-
);
20-
Ok(())
21-
}
22-
Err(e) => {
23-
bail!("Failed to start tracing to file. Error: {e}");
24-
}
9+
match telemetry::init_subscriber(subscriber) {
10+
Ok(()) => {
11+
println!(
12+
"Traces being written to: {:?}",
13+
path.canonicalize()
14+
.context("trace file canonicalization failed")?
15+
);
16+
Ok(guard)
2517
}
26-
}
27-
28-
if !cli.is_to_std_out {
29-
// Log to file
30-
match init_to_file() {
31-
Ok(_) => return Ok(()),
32-
Err(e) => {
33-
// Print error and fall though to logging to stdout
34-
eprintln!("Failed to start logging to file: {e}");
35-
}
36-
}
37-
}
38-
39-
// Log to stdout
40-
match tracing_subscriber::fmt().try_init() {
41-
Ok(_) => Ok(()),
4218
Err(e) => {
43-
bail!("Failed to start tracing. Error: {e}");
19+
bail!("Failed to start tracing to file. Error: {e}");
4420
}
4521
}
4622
}

crates/chat-app-server/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,9 @@ async fn main(
3838
async fn main() -> anyhow::Result<()> {
3939
use shuttle_runtime::Service as _;
4040

41-
let (file, path) = wykies_shared::telemetry::create_trace_file("chat-app-server")
42-
.context("failed to create file for traces")?;
43-
initialize_tracing("chat_app_server", "info", file);
41+
let (writer, path, _guard) = wykies_shared::telemetry::setup_tracing_writer("chat-app-server")
42+
.context("failed to setup traces")?;
43+
initialize_tracing("chat_app_server", "info", writer);
4444
println!(
4545
"Traces being written to: {:?}",
4646
path.canonicalize()

crates/wykies-shared/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,9 @@ wykies-time.workspace = true
2525
# For native compilation only
2626
[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
2727
actix-web.workspace = true
28-
tracing-bunyan-formatter.workspace = true
2928
ewebsock = { workspace = true, features = ["tls", "tokio"] }
29+
tracing-appender.workspace = true
30+
tracing-bunyan-formatter.workspace = true
3031
tracing-log.workspace = true
3132
tracing-subscriber = { workspace = true, features = ["registry", "env-filter"] }
3233

crates/wykies-shared/src/telemetry.rs

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
use actix_web::rt::task::JoinHandle;
22
use anyhow::Context;
3-
use std::{
4-
fs::{create_dir_all, File},
5-
path::PathBuf,
6-
};
3+
use std::{fs::create_dir_all, path::PathBuf};
74
use tracing::subscriber::set_global_default;
85
use tracing::Subscriber;
6+
use tracing_appender::non_blocking::{NonBlocking, WorkerGuard};
97
use tracing_bunyan_formatter::{BunyanFormattingLayer, JsonStorageLayer};
108
use tracing_log::LogTracer;
119
use tracing_subscriber::fmt::MakeWriter;
@@ -55,23 +53,16 @@ where
5553
actix_web::rt::task::spawn_blocking(move || current_span.in_scope(f))
5654
}
5755

58-
fn gen_log_filename(app_name: &str) -> String {
59-
format!(
60-
"{}_{app_name}.log",
61-
chrono::Local::now().format("%Y-%m-%dT%H-%M-%S")
62-
)
63-
}
64-
6556
/// Returns a handle to the file created and the file path
66-
pub fn create_trace_file(app_name: &str) -> anyhow::Result<(File, PathBuf)> {
57+
pub fn setup_tracing_writer(app_name: &str) -> anyhow::Result<(NonBlocking, PathBuf, WorkerGuard)> {
6758
// Create logging folder
68-
let log_folder = PathBuf::from("traces");
59+
let mut log_folder = PathBuf::from("traces");
60+
log_folder.push(app_name);
6961
create_dir_all(&log_folder).context("Failed to create logging folder")?;
7062

71-
// Create file to log to
72-
let filename = gen_log_filename(app_name);
73-
let file_path = log_folder.join(&filename);
74-
let file = File::create(&file_path)
75-
.with_context(|| format!("Failed to create log file: {filename:?}"))?;
76-
Ok((file, file_path))
63+
// Start non blocking logger wrapping a rolling logger
64+
let file_appender = tracing_appender::rolling::daily(&log_folder, app_name);
65+
let (non_blocking, guard) = tracing_appender::non_blocking(file_appender);
66+
67+
Ok((non_blocking, log_folder, guard))
7768
}

0 commit comments

Comments
 (0)