feat: Improve logging output (#139)
This commit is contained in:
parent
ab11362a0d
commit
189141924b
9 changed files with 107 additions and 40 deletions
36
Cargo.lock
generated
36
Cargo.lock
generated
|
|
@ -4,9 +4,9 @@ version = 3
|
|||
|
||||
[[package]]
|
||||
name = "aho-corasick"
|
||||
version = "1.1.2"
|
||||
version = "1.1.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0"
|
||||
checksum = "8e60d3430d3a69478ad0993f19238d2df97c507009a52b3c10addcd7f6bcb916"
|
||||
dependencies = [
|
||||
"memchr",
|
||||
]
|
||||
|
|
@ -162,11 +162,12 @@ dependencies = [
|
|||
"blue-build-recipe",
|
||||
"blue-build-template",
|
||||
"blue-build-utils",
|
||||
"chrono",
|
||||
"clap",
|
||||
"clap-verbosity-flag",
|
||||
"clap_complete",
|
||||
"clap_complete_nushell",
|
||||
"colorized",
|
||||
"colored",
|
||||
"dunce",
|
||||
"env_logger",
|
||||
"fuzzy-matcher",
|
||||
|
|
@ -222,7 +223,10 @@ name = "blue-build-utils"
|
|||
version = "0.8.2"
|
||||
dependencies = [
|
||||
"anyhow",
|
||||
"chrono",
|
||||
"colored",
|
||||
"directories",
|
||||
"env_logger",
|
||||
"format_serde_error",
|
||||
"log",
|
||||
"process_control",
|
||||
|
|
@ -256,9 +260,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
|
|||
|
||||
[[package]]
|
||||
name = "chrono"
|
||||
version = "0.4.34"
|
||||
version = "0.4.35"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5bc015644b92d5890fab7489e49d21f879d5c990186827d42ec511919404f38b"
|
||||
checksum = "8eaf5903dcbc0a39312feb77df2ff4c76387d591b9fc7b04a238dcf8bb62639a"
|
||||
dependencies = [
|
||||
"android-tzdata",
|
||||
"iana-time-zone",
|
||||
|
|
@ -364,12 +368,6 @@ dependencies = [
|
|||
"windows-sys 0.48.0",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "colorized"
|
||||
version = "1.0.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "739281952453521b532f476b5f8b77c4b16d5ab2a248466c8dc153b3f85d6564"
|
||||
|
||||
[[package]]
|
||||
name = "const_fn"
|
||||
version = "0.4.9"
|
||||
|
|
@ -481,9 +479,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "env_logger"
|
||||
version = "0.11.2"
|
||||
version = "0.11.3"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "6c012a26a7f605efc424dd53697843a72be7dc86ad2d01f7814337794a12231d"
|
||||
checksum = "38b35839ba51819680ba087cd351788c9a3c476841207e0b8cee0b04722343b9"
|
||||
dependencies = [
|
||||
"anstream",
|
||||
"anstyle",
|
||||
|
|
@ -816,9 +814,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "log"
|
||||
version = "0.4.20"
|
||||
version = "0.4.21"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b5e6163cb8c49088c2c36f57875e58ccd8c87c7427f7fbd50ea6710b2f3f2e8f"
|
||||
checksum = "90ed8c1e510134f979dbc4f070f87d4313098b704861a105fe34231c70a3901c"
|
||||
|
||||
[[package]]
|
||||
name = "memchr"
|
||||
|
|
@ -1053,9 +1051,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "regex"
|
||||
version = "1.10.3"
|
||||
version = "1.10.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b62dbe01f0b06f9d8dc7d49e05a0785f153b00b2c227856282f671e0318c9b15"
|
||||
checksum = "c117dbdfde9c8308975b6a18d71f3f385c89461f7b3fb054288ecf2a2058ba4c"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
|
|
@ -1065,9 +1063,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "regex-automata"
|
||||
version = "0.4.5"
|
||||
version = "0.4.6"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "5bb987efffd3c6d0d8f5f89510bb458559eab11e4f869acb20bf845e016259cd"
|
||||
checksum = "86b83b8b9847f9bf95ef68afb0b8e6cdb80f498442f5179a29fad448fcc1eaea"
|
||||
dependencies = [
|
||||
"aho-corasick",
|
||||
"memchr",
|
||||
|
|
|
|||
|
|
@ -10,6 +10,9 @@ categories = ["command-line-utilities"]
|
|||
|
||||
[workspace.dependencies]
|
||||
anyhow = "1"
|
||||
chrono = "0.4.35"
|
||||
colored = "2.1.0"
|
||||
env_logger = "0.11"
|
||||
format_serde_error = "0.3.0"
|
||||
log = "0.4"
|
||||
serde = { version = "1", features = ["derive"] }
|
||||
|
|
@ -54,8 +57,6 @@ clap = { version = "4", features = ["derive", "cargo", "unicode"] }
|
|||
clap-verbosity-flag = "2"
|
||||
clap_complete = "4"
|
||||
clap_complete_nushell = "4"
|
||||
colorized = "1"
|
||||
env_logger = "0.11"
|
||||
fuzzy-matcher = "0.3"
|
||||
once_cell = "1.19.0"
|
||||
open = "5"
|
||||
|
|
@ -68,6 +69,9 @@ users = "0.11.0"
|
|||
|
||||
# Workspace dependencies
|
||||
anyhow.workspace = true
|
||||
chrono.workspace = true
|
||||
colored.workspace = true
|
||||
env_logger.workspace = true
|
||||
log.workspace = true
|
||||
serde.workspace = true
|
||||
serde_json.workspace = true
|
||||
|
|
|
|||
|
|
@ -1,14 +1,17 @@
|
|||
use blue_build::commands::{BlueBuildArgs, BlueBuildCommand, CommandArgs};
|
||||
use blue_build_utils::logging;
|
||||
use clap::Parser;
|
||||
use env_logger::WriteStyle;
|
||||
use log::LevelFilter;
|
||||
|
||||
fn main() {
|
||||
let args = BlueBuildArgs::parse();
|
||||
|
||||
let log_level = args.verbosity.log_level_filter();
|
||||
|
||||
env_logger::builder()
|
||||
.filter_level(args.verbosity.log_level_filter())
|
||||
.filter_module("hyper::proto", log::LevelFilter::Info)
|
||||
.write_style(WriteStyle::Always)
|
||||
.filter_module("hyper::proto", LevelFilter::Info)
|
||||
.format(logging::format_log(log_level))
|
||||
.init();
|
||||
|
||||
log::trace!("Parsed arguments: {args:#?}");
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ use blue_build_utils::constants::{
|
|||
};
|
||||
use clap::Args;
|
||||
use clap_complete::Shell;
|
||||
use colored::Colorize;
|
||||
use fuzzy_matcher::{skim::SkimMatcherV2, FuzzyMatcher};
|
||||
use log::{debug, error, trace};
|
||||
use requestty::question::{completions, Completions};
|
||||
|
|
@ -54,8 +55,6 @@ impl BugReportCommand {
|
|||
///
|
||||
/// This function will panic if it fails to get the current shell or terminal version.
|
||||
pub fn create_bugreport(&self) -> anyhow::Result<()> {
|
||||
use colorized::{Color, Colors};
|
||||
|
||||
let os_info = os_info::get();
|
||||
let recipe = self.get_recipe();
|
||||
|
||||
|
|
@ -69,31 +68,27 @@ impl BugReportCommand {
|
|||
let issue_body = match generate_github_issue(&environment, &recipe) {
|
||||
Ok(body) => body,
|
||||
Err(e) => {
|
||||
println!(
|
||||
"{}: {e}",
|
||||
"Failed to generate bug report".color(Colors::BrightRedFg)
|
||||
);
|
||||
println!("{}: {e}", "Failed to generate bug report".bright_red());
|
||||
return Err(e);
|
||||
}
|
||||
};
|
||||
|
||||
println!(
|
||||
"\n{}\n{}\n",
|
||||
"Generated bug report:".color(Colors::BrightGreenFg),
|
||||
issue_body
|
||||
.color(Colors::BrightBlackBg)
|
||||
.color(Colors::BrightWhiteFg)
|
||||
"Generated bug report:".bright_green(),
|
||||
issue_body.on_bright_black().bright_white()
|
||||
);
|
||||
|
||||
let question = requestty::Question::confirm("anonymous")
|
||||
.message(
|
||||
"Forward the pre-filled report above to GitHub in your browser?"
|
||||
.color(Colors::BrightYellowFg),
|
||||
.bright_yellow()
|
||||
.to_string(),
|
||||
)
|
||||
.default(true)
|
||||
.build();
|
||||
|
||||
println!("{} To avoid any sensitive data from being exposed, please review the included information before proceeding.", "Warning:".color(Colors::BrightRedBg).color(Colors::BrightWhiteFg));
|
||||
println!("{} To avoid any sensitive data from being exposed, please review the included information before proceeding.", "Warning:".on_bright_red().bright_white());
|
||||
println!("Data forwarded to GitHub is subject to GitHub's privacy policy. For more information, see https://docs.github.com/en/github/site-policy/github-privacy-statement.\n");
|
||||
match requestty::prompt_one(question) {
|
||||
Ok(answer) => {
|
||||
|
|
@ -115,7 +110,7 @@ impl BugReportCommand {
|
|||
|
||||
println!(
|
||||
"\n{}",
|
||||
"Thanks for using the BlueBuild bug report tool!".color(Colors::BrightCyanFg)
|
||||
"Thanks for using the BlueBuild bug report tool!".bright_cyan()
|
||||
);
|
||||
|
||||
Ok(())
|
||||
|
|
|
|||
|
|
@ -14,7 +14,7 @@ use blue_build_utils::constants::{
|
|||
RECIPE_PATH, SIGSTORE_ID_TOKEN,
|
||||
};
|
||||
use clap::Args;
|
||||
use colorized::{Color, Colors};
|
||||
use colored::Colorize;
|
||||
use log::{debug, info, trace, warn};
|
||||
use typed_builder::TypedBuilder;
|
||||
|
||||
|
|
@ -146,7 +146,8 @@ impl BlueBuildCommand for BuildCommand {
|
|||
} else {
|
||||
NO_LABEL_ERROR_MESSAGE
|
||||
}
|
||||
.color(Colors::BrightYellowFg),
|
||||
.bright_yellow()
|
||||
.to_string(),
|
||||
)
|
||||
.default(true)
|
||||
.build();
|
||||
|
|
|
|||
|
|
@ -29,8 +29,10 @@ RUN \
|
|||
{%- endif %}
|
||||
--mount=type=bind,from=stage-exports,src=/exports.sh,dst=/tmp/exports.sh \
|
||||
--mount=type=cache,dst=/var/cache/rpm-ostree,id=rpm-ostree-cache-{{ recipe.name }}-{{ recipe.image_version }},sharing=locked \
|
||||
chmod +x /tmp/modules/{{ type }}/{{ type }}.sh \
|
||||
echo "========== Start {{ type|capitalize }} module ==========" \
|
||||
&& chmod +x /tmp/modules/{{ type }}/{{ type }}.sh \
|
||||
&& source /tmp/exports.sh && /tmp/modules/{{ type }}/{{ type }}.sh '{{ module.print_module_context() }}' \
|
||||
&& echo "========== End {{ type|capitalize }} module ==========" \
|
||||
&& ostree container commit
|
||||
{%- endif %}
|
||||
{%- endif %}
|
||||
|
|
|
|||
|
|
@ -14,6 +14,9 @@ process_control = { version = "4.0.3", features = ["crossbeam-channel"] }
|
|||
which = "6"
|
||||
|
||||
anyhow.workspace = true
|
||||
chrono.workspace = true
|
||||
colored.workspace = true
|
||||
env_logger.workspace = true
|
||||
format_serde_error.workspace = true
|
||||
log.workspace = true
|
||||
serde.workspace = true
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
pub mod command_output;
|
||||
pub mod constants;
|
||||
pub mod logging;
|
||||
|
||||
use std::{ffi::OsStr, io::Write, path::PathBuf, process::Command, thread, time::Duration};
|
||||
|
||||
|
|
|
|||
60
utils/src/logging.rs
Normal file
60
utils/src/logging.rs
Normal file
|
|
@ -0,0 +1,60 @@
|
|||
use std::io::{self, Write};
|
||||
|
||||
use chrono::Local;
|
||||
use colored::{ColoredString, Colorize};
|
||||
use env_logger::fmt::Formatter;
|
||||
use log::{Level, LevelFilter, Record};
|
||||
|
||||
fn colored_level(level: Level) -> ColoredString {
|
||||
match level {
|
||||
Level::Error => Level::Error.as_str().bright_red(),
|
||||
Level::Warn => Level::Warn.as_str().yellow(),
|
||||
Level::Info => Level::Info.as_str().bright_green(),
|
||||
Level::Debug => Level::Debug.as_str().blue(),
|
||||
Level::Trace => Level::Trace.as_str().bright_cyan(),
|
||||
}
|
||||
}
|
||||
|
||||
/// Given a `LevelFilter`, returns the function
|
||||
/// used to format logs. The more verbose the log level,
|
||||
/// the more info is displayed in each log header.
|
||||
pub fn format_log(
|
||||
log_level: LevelFilter,
|
||||
) -> impl Fn(&mut Formatter, &Record) -> io::Result<()> + Sync + Send {
|
||||
move |buf: &mut Formatter, record: &Record| match log_level {
|
||||
LevelFilter::Error | LevelFilter::Warn | LevelFilter::Info => {
|
||||
writeln!(
|
||||
buf,
|
||||
"{:width$} => {}",
|
||||
colored_level(record.level()),
|
||||
record.args(),
|
||||
width = 5,
|
||||
)
|
||||
}
|
||||
LevelFilter::Debug => writeln!(
|
||||
buf,
|
||||
"[{} {:>width$}] => {}",
|
||||
Local::now().format("%H:%M:%S"),
|
||||
colored_level(record.level()),
|
||||
record.args(),
|
||||
width = 5,
|
||||
),
|
||||
LevelFilter::Trace => writeln!(
|
||||
buf,
|
||||
"[{} {:width$} {}:{}] => {}",
|
||||
Local::now().format("%H:%M:%S"),
|
||||
colored_level(record.level()),
|
||||
record
|
||||
.module_path()
|
||||
.map_or_else(|| "", |p| p)
|
||||
.bright_yellow(),
|
||||
record
|
||||
.line()
|
||||
.map_or_else(String::new, |l| l.to_string())
|
||||
.bright_green(),
|
||||
record.args(),
|
||||
width = 5,
|
||||
),
|
||||
LevelFilter::Off => Ok(()),
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue