chore: log config

This commit is contained in:
Roman 2024-12-06 22:22:42 +08:00
parent d67b5c5a09
commit f7465746f0
No known key found for this signature in database
GPG Key ID: B8FE070B54E11B75
6 changed files with 159 additions and 0 deletions

View File

@ -39,6 +39,7 @@ members = [
"testnet/cfgsync", "testnet/cfgsync",
"tests", "tests",
"clients/executor-http-client", "clients/executor-http-client",
"nomos-cluster-tests",
] ]
exclude = ["proof_of_leadership/risc0/risc0_proofs", "nomos-core/risc0_proofs"] exclude = ["proof_of_leadership/risc0/risc0_proofs", "nomos-core/risc0_proofs"]
resolver = "2" resolver = "2"

View File

@ -0,0 +1,14 @@
[package]
name = "nomos-cluster-tests"
version = "0.1.0"
edition = "2021"
[dependencies]
clap = { version = "4", features = ["derive", "env"] }
serde_yaml = "0.9"
serde = { version = "1.0.215", features = ["derive"] }
nomos-tracing = { path = "../nomos-tracing" }
nomos-tracing-service = { path = "../nomos-services/tracing" }
overwatch-rs = { git = "https://github.com/logos-co/Overwatch", rev = "2f70806" }
color-eyre = "0.6.0"
tracing = "0.1"

View File

@ -0,0 +1,4 @@
# Nomos Cluster Tests
Suite of small scale distributed tests for Nomos

View File

@ -0,0 +1,5 @@
log:
backend: "Stdout"
format: "Json"
level: "debug"

View File

@ -0,0 +1,101 @@
use std::net::ToSocketAddrs;
use std::path::PathBuf;
use clap::{Parser, ValueEnum};
use serde::{Deserialize, Serialize};
use color_eyre::eyre::{eyre, Result};
use tracing::Level;
use nomos_tracing_service::{LoggerLayer, Tracing};
use overwatch_rs::services::ServiceData;
use nomos_tracing::logging::gelf::GelfConfig;
use nomos_tracing::logging::local::FileConfig;
#[derive(ValueEnum, Clone, Debug, Default)]
pub enum LoggerLayerType {
Gelf,
File,
#[default]
Stdout,
Stderr,
}
#[derive(Parser, Debug, Clone)]
pub struct LogArgs {
/// Address for the Gelf backend
#[clap(long = "log-addr", env = "LOG_ADDR", required_if_eq("backend", "Gelf"))]
log_addr: Option<String>,
/// Directory for the File backend
#[clap(long = "log-dir", env = "LOG_DIR", required_if_eq("backend", "File"))]
directory: Option<PathBuf>,
/// Prefix for the File backend
#[clap(long = "log-path", env = "LOG_PATH", required_if_eq("backend", "File"))]
prefix: Option<PathBuf>,
/// Backend type
#[clap(long = "log-backend", env = "LOG_BACKEND", value_enum)]
backend: Option<LoggerLayerType>,
#[clap(long = "log-level", env = "LOG_LEVEL")]
level: Option<String>,
}
#[derive(Deserialize, Debug, Clone, Serialize)]
pub struct Config {
pub tracing: <Tracing as ServiceData>::Settings,
}
impl Config {
pub fn update_from_args(
mut self,
log_args: LogArgs,
) -> Result<Self> {
update_tracing(&mut self.tracing, log_args)?;
Ok(self)
}
}
pub fn update_tracing(
tracing: &mut <Tracing as ServiceData>::Settings,
tracing_args: LogArgs,
) -> Result<()> {
let LogArgs {
backend,
log_addr: addr,
directory,
prefix,
level,
} = tracing_args;
// Override the file config with the one from env variables.
if let Some(backend) = backend {
tracing.logger = match backend {
LoggerLayerType::Gelf => LoggerLayer::Gelf(GelfConfig {
addr: addr
.ok_or_else(|| eyre!("Gelf backend requires an address."))?
.to_socket_addrs()?
.next()
.ok_or_else(|| eyre!("Invalid gelf address"))?,
}),
LoggerLayerType::File => LoggerLayer::File(FileConfig {
directory: directory.ok_or_else(|| eyre!("File backend requires a directory."))?,
prefix,
}),
LoggerLayerType::Stdout => LoggerLayer::Stdout,
LoggerLayerType::Stderr => LoggerLayer::Stderr,
}
};
if let Some(level_str) = level {
tracing.level = match level_str.as_str() {
"DEBUG" => Level::DEBUG,
"INFO" => Level::INFO,
"ERROR" => Level::ERROR,
"WARN" => Level::WARN,
_ => return Err(eyre!("Invalid log level provided.")),
};
}
Ok(())
}

View File

@ -0,0 +1,34 @@
mod config;
use clap::Parser;
use crate::config::{Config, LogArgs};
use color_eyre::eyre::{Result};
#[derive(Parser, Debug)]
#[command(author, version, about, long_about = None)]
struct Args {
/// Path for a yaml-encoded network config file
config: std::path::PathBuf,
/// Overrides log config.
#[clap(flatten)]
log_args: LogArgs,
}
fn main() -> Result<()> {
// Parse cluster options
let Args {
config,
log_args,
} = Args::parse();
let config = serde_yaml::from_reader::<_, Config>(std::fs::File::open(config)?)?
.update_from_args(
log_args,
)?;
// Run the test suite
Ok(())
}