logparse.rs
· 1.1 KiB · Rust
Raw
use anyhow::{Context, Result};
use colored::*;
pub fn ejecutar(archivo: &str, nivel: &str, quiet: bool) -> Result<()> {
let contenido =
std::fs::read_to_string(archivo).with_context(|| format!("No se pudo leer '{}'", archivo))?;
let nivel_upper = nivel.to_uppercase();
let mut encontradas = 0;
for linea in contenido.lines() {
if linea.to_uppercase().contains(&nivel_upper) {
if quiet {
println!("{}", linea);
} else {
let nivel_tag = match nivel_upper.as_str() {
"ERROR" => "ERROR".red().bold(),
"WARN" => "WARN".yellow().bold(),
"INFO" => "INFO".green(),
"DEBUG" => "DEBUG".cyan(),
_ => nivel.normal(),
};
println!("[{}] {}", nivel_tag, linea);
encontradas += 1;
}
}
}
if !quiet {
println!(
"\n{} lineas encontradas con nivel '{}' en {}",
encontradas.to_string().yellow(),
nivel,
archivo
);
}
Ok(())
}
| 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 | } |