Skip to content

File Logging #209

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 38 commits into
base: unstable
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
38 commits
Select commit Hold shift + click to select a range
da1c7d8
file logging setup
ThreeHrSleep Mar 25, 2025
0cce662
Merge branch 'unstable' into logging
ThreeHrSleep Mar 25, 2025
af9cb20
Merge branch 'unstable' into logging
ThreeHrSleep Mar 26, 2025
32d29dc
cli stuff
ThreeHrSleep Mar 30, 2025
6106654
oops
ThreeHrSleep Mar 31, 2025
74d64e8
Merge branch 'unstable' into logging
ThreeHrSleep Apr 1, 2025
7bc7d71
supress dep logs
ThreeHrSleep Apr 1, 2025
17a06c2
remove libp2p & discv5 tracing layer + cleanup
ThreeHrSleep Apr 3, 2025
7335131
refactor
ThreeHrSleep Apr 3, 2025
b6060b2
Merge branch 'unstable' into logging
ThreeHrSleep Apr 3, 2025
ae8df1b
nit
ThreeHrSleep Apr 3, 2025
4242609
fmt+sort
ThreeHrSleep Apr 3, 2025
ca3da57
nits
ThreeHrSleep Apr 3, 2025
e049681
udeps
ThreeHrSleep Apr 3, 2025
85c6e1b
more nits
ThreeHrSleep Apr 3, 2025
117d59b
suggestions from review
ThreeHrSleep Apr 5, 2025
b43d5fd
nit
ThreeHrSleep Apr 5, 2025
8d912ac
add `--logfile-debug-level`
ThreeHrSleep Apr 5, 2025
42cd860
add default logs dir
ThreeHrSleep Apr 7, 2025
459de2b
nit
ThreeHrSleep Apr 7, 2025
2f4c34a
Merge branch 'unstable' into logging
ThreeHrSleep Apr 7, 2025
05ed3d9
lints
ThreeHrSleep Apr 7, 2025
539ec36
libp2p discv5 layer
ThreeHrSleep Apr 7, 2025
9530d77
fmt
ThreeHrSleep Apr 7, 2025
81f8792
review 2
ThreeHrSleep Apr 9, 2025
07976a8
Merge branch 'unstable' into logging
ThreeHrSleep Apr 9, 2025
c26a820
Merge branch 'unstable' into logging
ThreeHrSleep Apr 11, 2025
9a1fc2f
ci
ThreeHrSleep Apr 11, 2025
628ecea
Merge branch 'unstable' into logging
ThreeHrSleep Apr 12, 2025
510be2e
tidy up
ThreeHrSleep Apr 16, 2025
4ffb0b2
Merge branch 'unstable' into logging
ThreeHrSleep Apr 16, 2025
3ca34ad
Merge branch 'unstable' into logging
ThreeHrSleep Apr 24, 2025
4d949f2
prevent log mix-up logfiles
ThreeHrSleep Apr 25, 2025
226e518
do subscriber initialization for keygen & keysplitter in main.rs
ThreeHrSleep Apr 25, 2025
02cecfa
default to `Debug` for logfile
ThreeHrSleep Apr 25, 2025
eda3059
fix suppressed error
ThreeHrSleep Apr 25, 2025
195a647
fmt
ThreeHrSleep Apr 25, 2025
6078b0c
Merge branch 'unstable' into logging
ThreeHrSleep Apr 25, 2025
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
23 changes: 19 additions & 4 deletions Cargo.lock

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

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ members = [
"anchor/http_metrics",
"anchor/keygen",
"anchor/keysplit",
"anchor/logging",
"anchor/message_receiver",
"anchor/message_sender",
"anchor/message_validator",
Expand Down Expand Up @@ -42,6 +43,7 @@ http_api = { path = "anchor/http_api" }
http_metrics = { path = "anchor/http_metrics" }
keygen = { path = "anchor/keygen" }
keysplit = { path = "anchor/keysplit" }
logging = { path = "anchor/logging" }
message_receiver = { path = "anchor/message_receiver" }
message_sender = { path = "anchor/message_sender" }
message_validator = { path = "anchor/message_validator" }
Expand Down Expand Up @@ -130,6 +132,7 @@ tokio = { version = "1.39.2", features = [
] }
tower-http = { version = "0.6", features = ["cors"] }
tracing = "0.1.40"
tracing-appender = "0.2"
tracing-subscriber = { version = "0.3.18", features = ["fmt", "env-filter"] }
tree_hash = "0.9"
tree_hash_derive = "0.9"
Expand Down
2 changes: 2 additions & 0 deletions anchor/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ eth2_network_config = { workspace = true }
futures = { workspace = true }
keygen = { workspace = true }
keysplit = { workspace = true }
logging = { workspace = true }
sensitive_url = { workspace = true }
serde = { workspace = true }
strum = { workspace = true }
task_executor = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }
tracing-appender = { workspace = true }
tracing-subscriber = { workspace = true }
types = { workspace = true }

Expand Down
85 changes: 83 additions & 2 deletions anchor/client/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ use std::{

use clap::{
builder::{styling::*, ArgAction, ArgPredicate},
Parser,
Parser, ValueEnum,
};
// use clap_utils::{get_color_style, FLAG_HEADER};
use ethereum_hashing::have_sha_extensions;
use serde::{Deserialize, Serialize};
use strum::Display;
use tracing::Level;
use version::VERSION;

pub static SHORT_VERSION: LazyLock<String> = LazyLock::new(|| VERSION.replace("Anchor/", ""));
Expand All @@ -30,6 +31,83 @@ pub static LONG_VERSION: LazyLock<String> = LazyLock::new(|| {

pub const FLAG_HEADER: &str = "Flags";

#[derive(Parser, Debug, Clone, Deserialize, Serialize)]
pub struct LoggingFlags {
#[arg(
long,
global = true,
default_value_t = DebugLevel::Info,
help = "Specifies the verbosity level used when emitting logs to the terminal")]
pub debug_level: DebugLevel,

#[arg(
long,
global = true,
default_value_t = DebugLevel::Info,
help = "Specifies the verbosity level used when emitting logs to the log file")]
pub logfile_debug_level: DebugLevel,

#[arg(
long,
global = true,
value_name = "SIZE",
help = "Maximum size of each log file in MB",
default_value_t = 20
)]
pub logfile_max_size: u64,

#[arg(
long,
global = true,
value_name = "NUMBER",
help = "Maximum number of log files to keep",
default_value_t = 5
)]
pub logfile_max_number: usize,

#[arg(
long,
global = true,
value_name = "DIR",
help = "Directory path where the log file will be stored"
)]
pub logfile_dir: Option<PathBuf>,

#[arg(
long,
global = true,
help = "If present, compress old log files. This can help reduce the space needed \
to store old logs."
)]
pub logfile_compression: bool,
}

#[derive(Clone, Copy, Debug, PartialEq, Deserialize, Serialize, Display, ValueEnum)]
pub enum DebugLevel {
#[strum(serialize = "info")]
Info,
#[strum(serialize = "debug")]
Debug,
#[strum(serialize = "trace")]
Trace,
#[strum(serialize = "warn")]
Warn,
#[strum(serialize = "error")]
Error,
}

impl From<DebugLevel> for Level {
fn from(debug_level: DebugLevel) -> Self {
match debug_level {
DebugLevel::Info => Level::INFO,
DebugLevel::Debug => Level::DEBUG,
DebugLevel::Trace => Level::TRACE,
DebugLevel::Warn => Level::WARN,
DebugLevel::Error => Level::ERROR,
}
}
}

fn allocator_name() -> &'static str {
if cfg!(target_os = "windows") {
"system"
Expand Down Expand Up @@ -444,6 +522,9 @@ pub struct Node {
display_order = 0
)]
pub disable_slashing_protection: bool,

#[clap(flatten)]
pub logging_flags: LoggingFlags,
}

pub fn get_color_style() -> Styles {
Expand Down
2 changes: 1 addition & 1 deletion anchor/client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// use tracing::{debug, info};

mod cli;
pub mod cli;
pub mod config;

use std::{
Expand Down
17 changes: 17 additions & 0 deletions anchor/logging/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "logging"
version = "0.1.0"
edition = { workspace = true }
authors = ["Sigma Prime <[email protected]>"]

[dependencies]
chrono = { version = "0.4", default-features = false, features = [
"clock",
"std",
] }
logroller = "0.1.8"
serde = { workspace = true }
tracing = { workspace = true }
tracing-appender = "0.2"
tracing-core = "0.1"
tracing-subscriber = { workspace = true }
2 changes: 2 additions & 0 deletions anchor/logging/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mod logging;
pub use logging::*;
88 changes: 88 additions & 0 deletions anchor/logging/src/logging.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
//! Collection of logging logic for initialising Anchor.
use std::path::PathBuf;

use logroller::{Compression, LogRollerBuilder, Rotation, RotationSize};
use serde::{Deserialize, Serialize};
use tracing::Level;
use tracing_appender::non_blocking::{NonBlocking, WorkerGuard};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LoggerConfig {
pub path: Option<PathBuf>,
#[serde(skip_serializing, skip_deserializing, default = "default_debug_level")]
pub debug_level: Level,
pub max_log_size: u64,
pub max_log_number: usize,
pub compression: bool,
}
impl Default for LoggerConfig {
fn default() -> Self {
LoggerConfig {
path: None,
debug_level: Level::INFO,
max_log_size: 20,
max_log_number: 5,
compression: false,
}
}
}

fn default_debug_level() -> Level {
Level::INFO
}

pub struct LoggingLayer {
pub non_blocking_writer: NonBlocking,
pub guard: WorkerGuard,
}
impl LoggingLayer {
pub fn new(non_blocking_writer: NonBlocking, guard: WorkerGuard) -> Self {
Self {
non_blocking_writer,
guard,
}
}
}

pub fn init_file_logging(default_logs_dir: PathBuf, config: LoggerConfig) -> Option<LoggingLayer> {
let filename = PathBuf::from("anchor.log");

let path = if config.max_log_number == 0 || config.max_log_size == 0 {
// User has explicitly disabled logging to file
return None;
} else {
config.path.unwrap_or(default_logs_dir)
};

let mut appender = LogRollerBuilder::new(path, filename)
.rotation(Rotation::SizeBased(RotationSize::MB(config.max_log_size)))
.max_keep_files(config.max_log_number.try_into().unwrap_or_else(|e| {
eprintln!("Failed to convert max_log_number to u64: {}", e);
10
}));

if config.compression {
appender = appender.compression(Compression::Gzip);
}

match appender.build() {
Ok(file_appender) => {
let (writer, guard) = tracing_appender::non_blocking(file_appender);
Some(LoggingLayer::new(writer, guard))
}
Err(e) => {
eprintln!("Failed to create rolling file appender: {e}");
None
}
}
}

pub fn filter_dependency_log(meta: &tracing::Metadata<'_>) -> bool {
if let Some(file) = meta.file() {
let target = meta.target();
if file.contains("/.cargo/") {
return target.contains("discv5") || target.contains("libp2p");
}
}
true
}
2 changes: 1 addition & 1 deletion anchor/message_validator/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ processor = { workspace = true }
sha2 = { workspace = true }
slot_clock = { workspace = true }
ssv_types = { workspace = true }
thiserror = { workspace = true }
thiserror = { workspace = true }
tokio = { workspace = true }
tracing = { workspace = true }

Expand Down
42 changes: 0 additions & 42 deletions anchor/src/logging.rs

This file was deleted.

Loading
Loading