Skip to content

Commit

Permalink
app: refine log messages
Browse files Browse the repository at this point in the history
Refine log messages:
1) remove file:line from INFO
2) strip prefix of file

Now the output becomes:
root@5ad838c8bf7b:/nydus# target/debug/nydus-image create -t directory -D images/  src/
[2022-10-17 15:54:35.102076 +00:00] INFO rafs superblock features: HASH_SHA256 | EXPLICIT_UID_GID | COMPRESSION_ZSTD
[2022-10-17 15:54:35.139154 +00:00] INFO successfully build RAFS filesystem:
meta blob path: images/6fb4d603307df0e8c523a14825181cc0c9c3de4c2797b5e341c7c489b40ab13c
data blob size: 0x22de7
data blobs: ["c508580ee90cbd543713067cb22c28b7bf444c457de5e4a53b797e5575307b7a"]

Signed-off-by: Jiang Liu <[email protected]>
  • Loading branch information
jiangliu committed Oct 18, 2022
1 parent 66ff20c commit 1f3458c
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 3 deletions.
69 changes: 67 additions & 2 deletions app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ use std::io::Result;
use std::path::PathBuf;

use flexi_logger::{
self, colored_opt_format, opt_format, Cleanup, Criterion, FileSpec, Logger, Naming,
self, style, Cleanup, Criterion, DeferredNow, FileSpec, Logger, Naming,
TS_DASHES_BLANK_COLONS_DOT_BLANK,
};
use log::LevelFilter;
use log::{Level, LevelFilter, Record};

pub mod signal;

Expand Down Expand Up @@ -114,6 +115,70 @@ impl BuildTimeInfo {
}
}

fn get_file_name<'a>(record: &'a Record) -> Option<&'a str> {
record.file().map(|v| match v.rfind("/src/") {
None => v,
Some(pos) => match v[..pos].rfind('/') {
None => &v[pos..],
Some(p) => &v[p..],
},
})
}

fn opt_format(
w: &mut dyn std::io::Write,
now: &mut DeferredNow,
record: &Record,
) -> std::result::Result<(), std::io::Error> {
let level = record.level();
if level == Level::Info {
write!(
w,
"[{}] {} {}",
now.format(TS_DASHES_BLANK_COLONS_DOT_BLANK),
record.level(),
&record.args()
)
} else {
write!(
w,
"[{}] {} [{}:{}] {}",
now.format(TS_DASHES_BLANK_COLONS_DOT_BLANK),
record.level(),
get_file_name(record).unwrap_or("<unnamed>"),
record.line().unwrap_or(0),
&record.args()
)
}
}

fn colored_opt_format(
w: &mut dyn std::io::Write,
now: &mut DeferredNow,
record: &Record,
) -> std::result::Result<(), std::io::Error> {
let level = record.level();
if level == Level::Info {
write!(
w,
"[{}] {} {}",
style(level).paint(now.format(TS_DASHES_BLANK_COLONS_DOT_BLANK)),
style(level).paint(level.to_string()),
style(level).paint(&record.args().to_string())
)
} else {
write!(
w,
"[{}] {} [{}:{}] {}",
style(level).paint(now.format(TS_DASHES_BLANK_COLONS_DOT_BLANK)),
style(level).paint(level.to_string()),
get_file_name(record).unwrap_or("<unnamed>"),
record.line().unwrap_or(0),
style(level).paint(&record.args().to_string())
)
}
}

/// Setup logging infrastructure for application.
///
/// `log_file_path` is an absolute path to logging files or relative path from current working
Expand Down
3 changes: 2 additions & 1 deletion src/bin/nydus-image/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,8 @@ impl OutputSerializer {
trace,
};

serde_json::to_writer(w, &output).context("failed to write result to output file")?;
serde_json::to_writer_pretty(w, &output)
.context("failed to write result to output file")?;
}

Ok(())
Expand Down

0 comments on commit 1f3458c

Please sign in to comment.