Skip to content

Commit 5685c13

Browse files
committed
[validation] add --log-level flag and structured logging
Adds env_logger/log crates to the validation_cli binary and introduces a --log-level flag (error/warn/info/debug/trace, default: warn) matching the convention used by puml_cli and the linker.
1 parent 6698b0e commit 5685c13

2 files changed

Lines changed: 37 additions & 4 deletions

File tree

validation/core/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ rust_binary(
5050
deps = [
5151
":validation",
5252
"@crates//:clap",
53+
"@crates//:env_logger",
54+
"@crates//:log",
5355
],
5456
)
5557

validation/core/src/main.rs

Lines changed: 35 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,37 @@ use std::fs;
2020
use std::mem;
2121
use std::process;
2222

23-
use clap::Parser;
23+
use clap::{Parser, ValueEnum};
24+
use env_logger::Builder;
2425
use validation::{
2526
validate_bazel_component, validate_component_class, BazelArchitecture, BazelInput, BazelReader,
2627
ClassDiagramIndex, ClassDiagramInputs, ClassDiagramReader, ComponentDiagramArchitecture,
2728
ComponentDiagramInputs, ComponentDiagramReader, Errors, Reader, RequiredInput,
2829
SelectedValidator, ValidatorSpec, ALL_VALIDATORS,
2930
};
3031

32+
/// CLI-visible log level (mirrors the parser/linker convention).
33+
#[derive(Copy, Clone, ValueEnum, Debug)]
34+
enum CliLogLevel {
35+
Error,
36+
Warn,
37+
Info,
38+
Debug,
39+
Trace,
40+
}
41+
42+
impl CliLogLevel {
43+
fn to_level_filter(self) -> log::LevelFilter {
44+
match self {
45+
CliLogLevel::Error => log::LevelFilter::Error,
46+
CliLogLevel::Warn => log::LevelFilter::Warn,
47+
CliLogLevel::Info => log::LevelFilter::Info,
48+
CliLogLevel::Debug => log::LevelFilter::Debug,
49+
CliLogLevel::Trace => log::LevelFilter::Trace,
50+
}
51+
}
52+
}
53+
3154
#[derive(Parser, Debug)]
3255
#[command(name = "validation")]
3356
#[command(version = "1.0")]
@@ -49,6 +72,10 @@ struct Args {
4972
/// with code 0. Intended for use during development (maturity=development).
5073
#[arg(long, default_value_t = false)]
5174
warn_on_errors: bool,
75+
76+
/// Log level: error, warn, info, debug, trace
77+
#[arg(long, value_enum, default_value = "warn")]
78+
log_level: CliLogLevel,
5279
}
5380

5481
struct ValidationCliInputs {
@@ -231,7 +258,7 @@ fn finish_validation(
231258
details
232259
);
233260
if warn_on_errors {
234-
eprintln!("WARNING: {output}");
261+
log::warn!("{}", output);
235262
Ok(())
236263
} else {
237264
Err(output)
@@ -263,8 +290,12 @@ fn write_log(path: &str, errors: &Errors) -> Result<(), String> {
263290
}
264291

265292
fn main() {
266-
if let Err(msg) = run(Args::parse()) {
267-
eprintln!("{msg}");
293+
let args = Args::parse();
294+
Builder::new()
295+
.filter_level(args.log_level.to_level_filter())
296+
.init();
297+
if let Err(msg) = run(args) {
298+
log::error!("{}", msg);
268299
process::exit(1);
269300
}
270301
}

0 commit comments

Comments
 (0)