From fbac1b95ee704f808b48642d56dc4fdd77b48f13 Mon Sep 17 00:00:00 2001 From: tison Date: Thu, 22 Aug 2024 02:04:52 +0800 Subject: [PATCH] feat: revert to short filename for TextLayout (#57) 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() +}