Skip to content

Commit

Permalink
chore: rotate logs after exceeding 20mb
Browse files Browse the repository at this point in the history
The previous rotation strategy was using the number of lines, which was set to 5000.

Users recently noticed this limitation was producing lots of logs, so we've changed the strategy to
be based on the size of the log in bytes.
  • Loading branch information
jacderida committed Sep 6, 2023
1 parent a173fa8 commit d947e02
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 8 deletions.
4 changes: 2 additions & 2 deletions sn_logging/src/appender.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use tracing_appender::non_blocking::{NonBlocking, WorkerGuard};
/// - older files are deleted.
pub(super) fn file_rotater(
dir: &PathBuf,
max_lines: usize,
max_bytes: usize,
uncompressed_files: usize,
max_files: usize,
) -> (NonBlocking, WorkerGuard) {
Expand All @@ -50,7 +50,7 @@ pub(super) fn file_rotater(
dir,
format!("{binary_name}.log"),
AppendTimestamp::default(FileLimit::MaxFiles(max_files)),
ContentLimit::Lines(max_lines),
ContentLimit::BytesSurpassed(max_bytes),
Compression::OnRotate(uncompressed_files),
);

Expand Down
16 changes: 10 additions & 6 deletions sn_logging/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ use tracing_subscriber::{
Layer, Registry,
};

const MAX_LOG_SIZE: usize = 20 * 1024 * 1024;
const MAX_UNCOMPRESSED_LOG_FILES: usize = 100;
const MAX_LOG_FILES: usize = 1000;

#[derive(Debug, Clone)]
pub enum LogOutputDest {
Stdout,
Expand Down Expand Up @@ -123,12 +127,12 @@ impl TracingLayers {
std::fs::create_dir_all(path)?;
println!("Logging to directory: {path:?}");

let logs_max_lines = 5000;
let logs_uncompressed = 100;
let logs_max_files = 1000;

let (file_rotation, worker_guard) =
appender::file_rotater(path, logs_max_lines, logs_uncompressed, logs_max_files);
let (file_rotation, worker_guard) = appender::file_rotater(
path,
MAX_LOG_SIZE,
MAX_UNCOMPRESSED_LOG_FILES,
MAX_LOG_FILES,
);
self.guard = Some(worker_guard);

match format {
Expand Down

0 comments on commit d947e02

Please sign in to comment.