fix(rust): don't start all log files with connlib. (#7853)

At present, the file logger for all Rust code starts each logfile with
`connlib.`. This is very confusing when exporting the logs from the GUI
client because even the logs from the client itself will start with
`connlib.`. To fix this, we make the base file name of the log file
configurable.
This commit is contained in:
Thomas Eizinger
2025-01-28 01:35:05 +00:00
committed by GitHub
parent 3887a7b690
commit c6492d4832
7 changed files with 19 additions and 16 deletions

View File

@@ -139,7 +139,7 @@ fn init_logging(log_dir: &Path, log_filter: String) -> Result<()> {
}
let (log_filter, reload_handle) = reload::Layer::new(log_filter);
let (file_layer, handle) = firezone_logging::file::layer(log_dir);
let (file_layer, handle) = firezone_logging::file::layer(log_dir, "connlib");
let subscriber = tracing_subscriber::registry()
.with(log_filter)

View File

@@ -193,7 +193,7 @@ fn init_logging(log_dir: PathBuf, log_filter: String) -> Result<()> {
let (env_filter, reload_handle) = tracing_subscriber::reload::Layer::new(env_filter);
let (file_layer, handle) = firezone_logging::file::layer(&log_dir);
let (file_layer, handle) = firezone_logging::file::layer(&log_dir, "connlib");
let subscriber = tracing_subscriber::registry()
.with(env_filter)

View File

@@ -52,7 +52,7 @@ pub fn setup(directives: &str) -> Result<Handles> {
let log_path = known_dirs::logs().context("Can't compute app log dir")?;
std::fs::create_dir_all(&log_path).map_err(Error::CreateDirAll)?;
let (layer, logger) = firezone_logging::file::layer(&log_path);
let (layer, logger) = firezone_logging::file::layer(&log_path, "gui-client");
let (filter, reloader) = reload::Layer::new(firezone_logging::try_filter(directives)?);
let subscriber = Registry::default()
.with(layer.with_filter(filter))

View File

@@ -66,10 +66,7 @@ fn choose_logs_to_delete(paths: &[PathBuf]) -> Vec<&Path> {
.filter_map(|path| {
// Don't delete files if we can't parse their stems as UTF-8.
let stem = path.file_stem()?.to_str()?;
if !stem.starts_with("connlib.") {
// Delete any non-log files like crash dumps.
return Some(path.as_path());
}
(stem < most_recent_stem).then_some(path.as_path())
})
.collect()
@@ -107,8 +104,6 @@ mod tests {
"/bogus/connlib.2024-08-06-14-21-13.log",
"/bogus/connlib.2024-08-06-14-51-19.jsonl",
"/bogus/connlib.2024-08-06-14-51-19.log",
"/bogus/crash.2024-07-22-21-16-20.dmp",
"/bogus/last_crash.dmp",
]
.into_iter()
.map(Path::new)

View File

@@ -666,7 +666,7 @@ fn setup_logging(
std::fs::create_dir_all(&log_dir)
.context("We should have permissions to create our log dir")?;
let (layer, handle) = firezone_logging::file::layer(&log_dir);
let (layer, handle) = firezone_logging::file::layer(&log_dir, "ipc-service");
let directives = get_log_filter().context("Couldn't read log filter")?;
let (filter, reloader) = reload::Layer::new(firezone_logging::try_filter(&directives)?);

View File

@@ -137,7 +137,7 @@ fn main() -> Result<()> {
.common
.log_dir
.as_deref()
.map(firezone_logging::file::layer)
.map(|dir| firezone_logging::file::layer(dir, "firezone-headless-client"))
.unzip();
firezone_logging::setup_global_subscriber(layer).context("Failed to set up logging")?;

View File

@@ -24,7 +24,6 @@ use tracing::Subscriber;
use tracing_appender::non_blocking::{NonBlocking, WorkerGuard};
use tracing_subscriber::Layer;
const LOG_FILE_BASE_NAME: &str = "connlib";
pub const TIME_FORMAT: &str = "[year]-[month]-[day]-[hour]-[minute]-[second]";
/// How many lines we will at most buffer in the channel with the background thread that writes to disk.
@@ -39,11 +38,14 @@ pub const TIME_FORMAT: &str = "[year]-[month]-[day]-[hour]-[minute]-[second]";
const MAX_BUFFERED_LINES: usize = 1024;
/// Create a new file logger layer.
pub fn layer<T>(log_dir: &Path) -> (Box<dyn Layer<T> + Send + Sync + 'static>, Handle)
pub fn layer<T>(
log_dir: &Path,
file_base_name: &'static str,
) -> (Box<dyn Layer<T> + Send + Sync + 'static>, Handle)
where
T: Subscriber + for<'a> tracing_subscriber::registry::LookupSpan<'a>,
{
let (appender_fmt, handle_fmt) = new_appender(log_dir.to_path_buf(), "log");
let (appender_fmt, handle_fmt) = new_appender(log_dir.to_path_buf(), file_base_name, "log");
let layer_fmt = tracing_subscriber::fmt::layer()
.with_ansi(false)
.with_writer(appender_fmt)
@@ -60,11 +62,16 @@ where
(layer_fmt, handle)
}
fn new_appender(directory: PathBuf, file_extension: &'static str) -> (NonBlocking, WorkerGuard) {
fn new_appender(
directory: PathBuf,
file_base_name: &'static str,
file_extension: &'static str,
) -> (NonBlocking, WorkerGuard) {
let appender = Appender {
directory,
current: None,
file_extension,
file_base_name,
};
let (non_blocking, guard) = tracing_appender::non_blocking::NonBlockingBuilder::default()
@@ -87,6 +94,7 @@ pub struct Handle {
#[derive(Debug)]
struct Appender {
directory: PathBuf,
file_base_name: &'static str,
file_extension: &'static str,
// Leaving this so that I/O errors come up through `write` instead of panicking
// in `layer`
@@ -119,7 +127,7 @@ impl Appender {
.format(&format)
.map_err(|_| io::Error::other("Failed to format timestamp"))?;
let filename = format!("{LOG_FILE_BASE_NAME}.{date}.{}", self.file_extension);
let filename = format!("{}.{date}.{}", self.file_base_name, self.file_extension);
let path = self.directory.join(&filename);
let mut open_options = fs::OpenOptions::new();