Skip to content

Commit

Permalink
Make logging configurable
Browse files Browse the repository at this point in the history
  • Loading branch information
0xmichalis committed Jan 29, 2025
1 parent e8b5888 commit d581ac6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
24 changes: 23 additions & 1 deletion src/logging.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use std::fmt;
use tracing::Level;
use tracing_subscriber::fmt::format::FmtSpan;
use tracing_subscriber::fmt::time::FormatTime;

Expand All @@ -9,12 +10,33 @@ impl FormatTime for NoTime {
Ok(())
}
}
pub fn init() {

#[derive(clap::ValueEnum, Clone, Debug)]
pub enum LogLevel {
Debug,
Info,
Warn,
Error,
}

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

pub fn init(log_level: LogLevel) {
tracing_subscriber::fmt()
.with_timer(NoTime)
.with_target(false)
.with_span_events(FmtSpan::NONE)
.with_level(true)
.with_ansi(true)
.with_max_level(Level::from(log_level))
.init();
}
10 changes: 7 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::{Context, Result};
use clap::Parser;
use logging::LogLevel;
use serde::Deserialize;
use std::collections::HashMap;
use std::path::PathBuf;
Expand All @@ -22,6 +23,9 @@ struct Args {
/// Optional output directory path (defaults to current directory)
#[arg(short, long)]
output_path: Option<PathBuf>,

#[arg(short, long, value_enum, default_value = "info")]
log_level: LogLevel,
}

#[derive(Debug, Deserialize)]
Expand All @@ -38,12 +42,12 @@ struct TokenConfig {

#[tokio::main]
async fn main() -> Result<()> {
// Initialize logging
logging::init();

// Parse command line arguments
let args = Args::parse();

// Initialize logging
logging::init(args.log_level);

// Use provided output path or current directory as base
let base_path = args.output_path.unwrap_or_else(|| PathBuf::from("."));

Expand Down

0 comments on commit d581ac6

Please sign in to comment.