Zuletzt aktiv 1 month ago

Subcomando logparse con filtrado por nivel y colores (capitulo 07)

atareao's Avatar atareao hat die Gist bearbeitet 1 month ago. Zu Änderung gehen

1 file changed, 39 insertions

logparse.rs(Datei erstellt)

@@ -0,0 +1,39 @@
1 + use anyhow::{Context, Result};
2 + use colored::*;
3 +
4 + pub fn ejecutar(archivo: &str, nivel: &str, quiet: bool) -> Result<()> {
5 + let contenido =
6 + std::fs::read_to_string(archivo).with_context(|| format!("No se pudo leer '{}'", archivo))?;
7 +
8 + let nivel_upper = nivel.to_uppercase();
9 + let mut encontradas = 0;
10 +
11 + for linea in contenido.lines() {
12 + if linea.to_uppercase().contains(&nivel_upper) {
13 + if quiet {
14 + println!("{}", linea);
15 + } else {
16 + let nivel_tag = match nivel_upper.as_str() {
17 + "ERROR" => "ERROR".red().bold(),
18 + "WARN" => "WARN".yellow().bold(),
19 + "INFO" => "INFO".green(),
20 + "DEBUG" => "DEBUG".cyan(),
21 + _ => nivel.normal(),
22 + };
23 + println!("[{}] {}", nivel_tag, linea);
24 + encontradas += 1;
25 + }
26 + }
27 + }
28 +
29 + if !quiet {
30 + println!(
31 + "\n{} lineas encontradas con nivel '{}' en {}",
32 + encontradas.to_string().yellow(),
33 + nivel,
34 + archivo
35 + );
36 + }
37 +
38 + Ok(())
39 + }
Neuer Älter