generated from srid/rust-nix-template
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
logging: Make output bare of level/target/fields (#30)
- Loading branch information
Showing
6 changed files
with
61 additions
and
25 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
use std::fmt; | ||
use std::io; | ||
|
||
use tracing::{Event, Level, Subscriber}; | ||
use tracing_subscriber::fmt::format; | ||
use tracing_subscriber::{ | ||
fmt::{FmtContext, FormatEvent, FormatFields}, | ||
registry::LookupSpan, | ||
}; | ||
|
||
/// A [tracing_subscriber] event formatter that suppresses everything but the | ||
/// log message. | ||
/// | ||
/// Level/target are displayed for non-INFO messages. | ||
struct BareFormatter; | ||
|
||
impl<S, N> FormatEvent<S, N> for BareFormatter | ||
where | ||
S: Subscriber + for<'a> LookupSpan<'a>, | ||
N: for<'a> FormatFields<'a> + 'static, | ||
{ | ||
fn format_event( | ||
&self, | ||
ctx: &FmtContext<'_, S, N>, | ||
mut writer: format::Writer<'_>, | ||
event: &Event<'_>, | ||
) -> fmt::Result { | ||
let metadata = event.metadata(); | ||
if metadata.level() != &Level::INFO { | ||
write!(&mut writer, "{} {}: ", metadata.level(), metadata.target())?; | ||
} | ||
ctx.field_format().format_fields(writer.by_ref(), event)?; | ||
writeln!(writer) | ||
} | ||
} | ||
|
||
pub fn setup_logging(verbose: bool) { | ||
let env_filter = if verbose { | ||
"nixci=debug,nix_rs=debug" | ||
} else { | ||
"nixci=info,nix_rs=info" | ||
}; | ||
let builder = tracing_subscriber::fmt() | ||
.with_writer(io::stderr) | ||
.with_max_level(Level::INFO) | ||
.with_env_filter(env_filter); | ||
|
||
if !verbose { | ||
builder.event_format(BareFormatter).init(); | ||
} else { | ||
builder.init() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters