Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add prefix customization to default formatter #210

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
65 changes: 65 additions & 0 deletions src/fmt/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ pub(crate) struct Builder {
pub format_level: bool,
pub format_indent: Option<usize>,
pub custom_format: Option<FormatFn>,
pub format_prefix: Option<&'static str>,
pub format_suffix: &'static str,
built: bool,
}
Expand All @@ -156,6 +157,7 @@ impl Default for Builder {
format_level: true,
format_indent: Some(4),
custom_format: None,
format_prefix: None,
format_suffix: "\n",
built: false,
}
Expand Down Expand Up @@ -189,6 +191,7 @@ impl Builder {
level: built.format_level,
written_header_value: false,
indent: built.format_indent,
prefix: built.format_prefix,
suffix: built.format_suffix,
buf,
};
Expand All @@ -214,11 +217,13 @@ struct DefaultFormat<'a> {
written_header_value: bool,
indent: Option<usize>,
buf: &'a mut Formatter,
prefix: Option<&'a str>,
suffix: &'a str,
}

impl<'a> DefaultFormat<'a> {
fn write(mut self, record: &Record) -> io::Result<()> {
self.write_prefix()?;
self.write_timestamp()?;
self.write_level(record)?;
self.write_module_path(record)?;
Expand Down Expand Up @@ -257,6 +262,15 @@ impl<'a> DefaultFormat<'a> {
}
}

fn write_prefix(&mut self) -> io::Result<()> {
match self.prefix {
None => Ok(()),
Some(prefix) => {
self.write_header_value(prefix)
}
}
}

fn write_level(&mut self, record: &Record) -> io::Result<()> {
if !self.level {
return Ok(());
Expand Down Expand Up @@ -412,6 +426,7 @@ mod tests {
level: true,
written_header_value: false,
indent: None,
prefix: None,
suffix: "\n",
buf: &mut f,
});
Expand All @@ -433,6 +448,7 @@ mod tests {
level: false,
written_header_value: false,
indent: None,
prefix: None,
suffix: "\n",
buf: &mut f,
});
Expand All @@ -454,6 +470,7 @@ mod tests {
level: true,
written_header_value: false,
indent: Some(4),
prefix: None,
suffix: "\n",
buf: &mut f,
});
Expand All @@ -475,6 +492,7 @@ mod tests {
level: true,
written_header_value: false,
indent: Some(0),
prefix: None,
suffix: "\n",
buf: &mut f,
});
Expand All @@ -496,13 +514,58 @@ mod tests {
level: false,
written_header_value: false,
indent: Some(4),
prefix: None,
suffix: "\n",
buf: &mut f,
});

assert_eq!("log\n message\n", written);
}

#[test]
fn format_prefix() {
let writer = writer::Builder::new()
.write_style(WriteStyle::Never)
.build();

let mut f = Formatter::new(&writer);

let written = write(DefaultFormat {
timestamp: None,
module_path: false,
level: false,
written_header_value: false,
indent: None,
prefix: Some("PREFIX"),
suffix: "\n",
buf: &mut f,
});

assert_eq!("[PREFIX] log\nmessage\n", written);
}

#[test]
fn format_prefix_with_level_and_module_path() {
let writer = writer::Builder::new()
.write_style(WriteStyle::Never)
.build();

let mut f = Formatter::new(&writer);

let written = write(DefaultFormat {
timestamp: None,
module_path: true,
level: true,
written_header_value: false,
indent: None,
prefix: Some("PREFIX"),
suffix: "\n",
buf: &mut f,
});

assert_eq!("[PREFIX INFO test::path] log\nmessage\n", written);
}

#[test]
fn format_suffix() {
let writer = writer::Builder::new()
Expand All @@ -517,6 +580,7 @@ mod tests {
level: false,
written_header_value: false,
indent: None,
prefix: None,
suffix: "\n\n",
buf: &mut f,
});
Expand All @@ -538,6 +602,7 @@ mod tests {
level: false,
written_header_value: false,
indent: Some(4),
prefix: None,
suffix: "\n\n",
buf: &mut f,
});
Expand Down
9 changes: 8 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,11 +213,12 @@
//! ## Tweaking the default format
//!
//! Parts of the default format can be excluded from the log output using the [`Builder`].
//! The following example excludes the timestamp from the log output:
//! The following example excludes the timestamp from the log output, and sets a prefix:
//!
//! ```
//! env_logger::builder()
//! .format_timestamp(None)
//! .format_prefix("PREFIX")
//! .init();
//! ```
//!
Expand Down Expand Up @@ -631,6 +632,12 @@ impl Builder {
self.format_timestamp(Some(fmt::TimestampPrecision::Nanos))
}

/// Configures the start of line prefix.
pub fn format_prefix(&mut self, prefix: &'static str) -> &mut Self {
self.format.format_prefix = Some(prefix);
self
}

/// Configures the end of line suffix.
pub fn format_suffix(&mut self, suffix: &'static str) -> &mut Self {
self.format.format_suffix = suffix;
Expand Down