Skip to content

Commit

Permalink
Rename with_custom_timestamps to with_timestamp_format
Browse files Browse the repository at this point in the history
  • Loading branch information
borntyping committed Mar 18, 2023
1 parent 404005c commit cc011a8
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 13 deletions.
2 changes: 1 addition & 1 deletion examples/timestamps_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use time::macros::format_description;
fn main() {
SimpleLogger::new()
.env()
.with_custom_timestamps(format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"))
.with_timestamp_format(format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"))
.init()
.unwrap();

Expand Down
26 changes: 14 additions & 12 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ pub struct SimpleLogger {
#[cfg(feature = "timestamps")]
timestamps: Timestamps,
#[cfg(feature = "timestamps")]
timeformat: Option<&'static [FormatItem<'static>]>,
timestamps_format: Option<&'static [FormatItem<'static>]>,

/// Whether to use color output or not.
///
Expand Down Expand Up @@ -115,7 +115,7 @@ impl SimpleLogger {
timestamps: Timestamps::Utc,

#[cfg(feature = "timestamps")]
timeformat: None,
timestamps_format: None,

#[cfg(feature = "colored")]
colors: true,
Expand Down Expand Up @@ -270,7 +270,9 @@ impl SimpleLogger {
self
}

/// Custom timestamps format
/// Control the format used for timestamps.
///
/// Without this, a default format is used depending on the timestamps type.
///
/// The syntax for the format_description macro can be found in the
/// [`time` crate book](https://time-rs.github.io/book/api/format-description.html).
Expand All @@ -279,14 +281,14 @@ impl SimpleLogger {
/// simple_logger::SimpleLogger::new()
/// .with_level(log::LevelFilter::Debug)
/// .env()
/// .with_custom_timestamps(time::macros::format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"))
/// .with_timestamp_format(time::macros::format_description!("[year]-[month]-[day] [hour]:[minute]:[second]"))
/// .init()
/// .unwrap();
/// ```
#[must_use = "You must call init() to begin logging"]
#[cfg(feature = "timestamps")]
pub fn with_custom_timestamps(mut self, timeformat: &'static [FormatItem<'static>]) -> SimpleLogger {
self.timeformat = Some(timeformat);
pub fn with_timestamp_format(mut self, format: &'static [FormatItem<'static>]) -> SimpleLogger {
self.timestamps_format = Some(format);
self
}

Expand Down Expand Up @@ -451,20 +453,20 @@ impl Log for SimpleLogger {
"behaviour. See the time crate's documentation for more information. ",
"(https://time-rs.github.io/internal-api/time/index.html#feature-flags)"
))
.format(&self.timeformat.unwrap_or(TIMESTAMP_FORMAT_OFFSET))
.format(&self.timestamps_format.unwrap_or(TIMESTAMP_FORMAT_OFFSET))
.unwrap()
),
Timestamps::Utc => format!(
"{} ",
OffsetDateTime::now_utc()
.format(&self.timeformat.unwrap_or(TIMESTAMP_FORMAT_UTC))
.format(&self.timestamps_format.unwrap_or(TIMESTAMP_FORMAT_UTC))
.unwrap()
),
Timestamps::UtcOffset(offset) => format!(
"{} ",
OffsetDateTime::now_utc()
.to_offset(offset)
.format(&self.timeformat.unwrap_or(TIMESTAMP_FORMAT_OFFSET))
.format(&self.timestamps_format.unwrap_or(TIMESTAMP_FORMAT_OFFSET))
.unwrap()
),
}
Expand Down Expand Up @@ -628,10 +630,10 @@ mod test {
#[test]
#[cfg(feature = "timestamps")]
#[allow(deprecated)]
fn test_with_timeformat() {
fn test_with_timestamps_format() {
let builder =
SimpleLogger::new().with_custom_timestamps(time::macros::format_description!("[hour]:[minute]:[second]"));
assert!(builder.timeformat.is_some());
SimpleLogger::new().with_timestamp_format(time::macros::format_description!("[hour]:[minute]:[second]"));
assert!(builder.timestamps_format.is_some());
}

#[test]
Expand Down

0 comments on commit cc011a8

Please sign in to comment.