Skip to content

Commit

Permalink
early return
Browse files Browse the repository at this point in the history
  • Loading branch information
Milo123459 committed Nov 27, 2023
1 parent 95332b9 commit 5e19c27
Showing 1 changed file with 44 additions and 43 deletions.
87 changes: 44 additions & 43 deletions src/util/logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,49 +2,50 @@ use crate::subscriptions;
use colored::Colorize;

pub fn format_attr_log(log: subscriptions::deployment_logs::LogFields) {
if !log.attributes.is_empty() {
let mut level: Option<String> = None;
let message = log.message;
let mut others = Vec::new();
// get attributes using a match
for attr in &log.attributes {
match attr.key.to_lowercase().as_str() {
"level" | "lvl" | "severity" => level = Some(attr.value.clone()),
_ => others.push(format!(
"{}{}{}",
attr.key.clone().magenta(),
"=",
attr.value
.clone()
.normal()
.replace('"', "\"".dimmed().to_string().as_str())
)),
}
}
// get the level and colour it
let level = level
.map(|level| {
// make it uppercase so we dont have to make another variable
// for some reason, .uppercase() removes formatting

match level.replace('"', "").to_lowercase().as_str() {
"info" => "[INFO]".blue(),
"error" | "err" => "[ERRO]".red(),
"warn" => "[WARN]".yellow(),
"debug" => "[DBUG]".dimmed(),
_ => format!("[{}]", level).normal(),
}
.bold()
})
.unwrap();
println!(
"{} {} {} {}",
log.timestamp.replace('"', "").normal(),
level,
message,
others.join(" ")
);
} else {
if log.attributes.is_empty() {
println!("{}", log.message);
return;
}

let mut level: Option<String> = None;
let message = log.message;
let mut others = Vec::new();
// get attributes using a match
for attr in &log.attributes {
match attr.key.to_lowercase().as_str() {
"level" | "lvl" | "severity" => level = Some(attr.value.clone()),
_ => others.push(format!(
"{}{}{}",
attr.key.clone().magenta(),
"=",
attr.value
.clone()
.normal()
.replace('"', "\"".dimmed().to_string().as_str())
)),
}
}
// get the level and colour it
let level = level
.map(|level| {
// make it uppercase so we dont have to make another variable
// for some reason, .uppercase() removes formatting

match level.replace('"', "").to_lowercase().as_str() {
"info" => "[INFO]".blue(),
"error" | "err" => "[ERRO]".red(),
"warn" => "[WARN]".yellow(),
"debug" => "[DBUG]".dimmed(),
_ => format!("[{}]", level).normal(),
}
.bold()
})
.unwrap();
println!(
"{} {} {} {}",
log.timestamp.replace('"', "").normal(),
level,
message,
others.join(" ")
);
}

0 comments on commit 5e19c27

Please sign in to comment.