From 65fc0f06419b4a331f541db257386a7cf298fbed Mon Sep 17 00:00:00 2001 From: FroVolod Date: Wed, 11 Sep 2024 11:14:41 +0300 Subject: [PATCH] refactored --- Cargo.lock | 1 + cargo-near/Cargo.toml | 1 + cargo-near/src/main.rs | 3 +-- cargo-near/src/types/my_formatter.rs | 8 +++++--- 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 6d679a64..6ba33aff 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -474,6 +474,7 @@ dependencies = [ "tracing", "tracing-core", "tracing-indicatif", + "tracing-log", "tracing-subscriber", "tracing-test", ] diff --git a/cargo-near/Cargo.toml b/cargo-near/Cargo.toml index 591f1908..869851f6 100644 --- a/cargo-near/Cargo.toml +++ b/cargo-near/Cargo.toml @@ -46,6 +46,7 @@ indicatif = "0.17.8" indenter = "0.3" tracing-core = "0.1.32" tracing = "0.1.40" +tracing-log = "0.2.0" tracing-indicatif = "0.3.6" tracing-subscriber = { version = "0.3", features = ["env-filter"] } tokio = { version = "1.0", default-features = false, features = [ diff --git a/cargo-near/src/main.rs b/cargo-near/src/main.rs index 6206538a..deab4953 100644 --- a/cargo-near/src/main.rs +++ b/cargo-near/src/main.rs @@ -29,9 +29,8 @@ fn main() -> CliResult { tracing_subscriber::registry() .with( tracing_subscriber::fmt::layer() - .with_line_number(true) - .fmt_fields(format) .event_format(my_formatter) + .fmt_fields(format) .with_filter(env_filter), ) .init(); diff --git a/cargo-near/src/types/my_formatter.rs b/cargo-near/src/types/my_formatter.rs index eb3fca56..611fe13d 100644 --- a/cargo-near/src/types/my_formatter.rs +++ b/cargo-near/src/types/my_formatter.rs @@ -1,6 +1,7 @@ use colored::Colorize; use tracing::Level; use tracing_core::{Event, Subscriber}; +use tracing_log::NormalizeEvent; use tracing_subscriber::fmt::{ format::{self, FormatEvent, FormatFields}, FmtContext, @@ -29,10 +30,11 @@ where mut writer: format::Writer<'_>, event: &Event<'_>, ) -> std::fmt::Result { - let metadata = event.metadata(); + let normalized_meta = event.normalized_metadata(); + let metadata = normalized_meta.as_ref().unwrap_or_else(|| event.metadata()); let level = format!("[{}]", metadata.level()); - let level = match *metadata.level() { + let fmt_level = match *metadata.level() { Level::ERROR => level.red(), Level::WARN => level.yellow(), Level::INFO => level.cyan(), @@ -43,7 +45,7 @@ where write!( &mut writer, "{}-[{}] {}:{} - ", - level, + fmt_level, self.environment, metadata.file().unwrap_or("log"), metadata.line().unwrap_or_default()