From d6353cde1afb7853e76b16d6e9fd352bc9e29d9e Mon Sep 17 00:00:00 2001 From: tison Date: Tue, 20 Aug 2024 19:53:33 +0800 Subject: [PATCH] feat: revert the short filename for TextLayout Signed-off-by: tison --- src/layout/text.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/src/layout/text.rs b/src/layout/text.rs index 3b1cf87..4358032 100644 --- a/src/layout/text.rs +++ b/src/layout/text.rs @@ -12,6 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. +use std::borrow::Cow; use std::fmt::Arguments; use colored::Color; @@ -153,7 +154,7 @@ impl TextLayout { ColoredString::from(record.level().to_string()).color(color) }; let module = record.module_path().unwrap_or_default(); - let file = record.file().unwrap_or_default(); + let file = filename(record); let line = record.line().unwrap_or_default(); let message = record.args(); let kvs = KvDisplay::new(record.key_values()); @@ -169,3 +170,14 @@ impl From for Layout { Layout::Text(layout) } } + +// obtain filename only from record's full file path +// reason: the module is already logged + full file path is noisy for text layout +fn filename<'a>(record: &'a log::Record<'a>) -> Cow<'a, str> { + record + .file() + .map(std::path::Path::new) + .and_then(std::path::Path::file_name) + .map(std::ffi::OsStr::to_string_lossy) + .unwrap_or_default() +}