Skip to content

add TimePadding with Right and AddZeros#134

Open
cscherrNT wants to merge 1 commit intoDrakulix:masterfrom
cscherrNT:master
Open

add TimePadding with Right and AddZeros#134
cscherrNT wants to merge 1 commit intoDrakulix:masterfrom
cscherrNT:master

Conversation

@cscherrNT
Copy link

This is a proposed fix for #133.

I added a TimePadding Enum. in write_time, the formatted time stamp is generated with the time module like before, then I do some formatting.

  • TimePadding::Right just applys an offset to the timestamp, like other paddings.
  • TimePadding::AddZeros adds zeros to the Timestamp, accodring to it's length. (only with rfc3339 formatting)

Usage

Build a Config like this:

                let conf = conf_builder
                    .set_location_level(elem)
                    .set_target_level(elem)
                    .set_max_level(elem)
                    .set_time_level(elem)
                    .set_time_format_rfc3339()
                    .set_time_padding(config::TimePadding::AddZeros)
                    .build();

Example output:

2023-08-02T08:08:45.217511363Z [INFO] simplelog::tests: [src/lib.rs:238] Test Information
2023-08-02T08:08:45.217519856Z [INFO] simplelog::tests: [src/lib.rs:238] Test Information
2023-08-02T08:08:45.217590429Z [INFO] simplelog::tests: [src/lib.rs:238] Test Information
2023-08-02T08:08:45.217599035Z [INFO] simplelog::tests: [src/lib.rs:238] Test Information
2023-08-02T08:08:45.217624283Z [INFO] simplelog::tests: [src/lib.rs:238] Test Information
2023-08-02T08:08:45.217628916Z [INFO] simplelog::tests: [src/lib.rs:238] Test Information
2023-08-02T08:08:45.217652289Z [INFO] simplelog::tests: [src/lib.rs:238] Test Information
2023-08-02T08:08:45.217656130Z [INFO] simplelog::tests: [src/lib.rs:238] Test Information
2023-08-02T08:08:45.217680170Z [INFO] simplelog::tests: [src/lib.rs:238] Test Information
2023-08-02T08:08:45.217684238Z [INFO] simplelog::tests: [src/lib.rs:238] Test Information
2023-08-02T08:08:45.217714112Z [INFO] simplelog::tests: [src/lib.rs:238] Test Information
2023-08-02T08:08:45.217717736Z [INFO] simplelog::tests: [src/lib.rs:238] Test Information
2023-08-02T08:08:45.217741200Z [INFO] simplelog::tests: [src/lib.rs:238] Test Information
2023-08-02T08:08:45.217782376Z [INFO] simplelog::tests: [src/lib.rs:238] Test Information
2023-08-02T08:08:45.217815202Z [INFO] simplelog::tests: [src/lib.rs:238] Test Information
2023-08-02T08:08:45.217819815Z [INFO] simplelog::tests: [src/lib.rs:238] Test Information
2023-08-02T08:08:45.217886072Z [INFO] simplelog::tests: [src/lib.rs:238] Test Information
2023-08-02T08:08:45.217891502Z [INFO] simplelog::tests: [src/lib.rs:238] Test Information

Note: This is my first Pull Request, I hope I didn't miss anything important.

only do timepadding for rfc3339

fix a doc comment
//
// the rfc3339 timestamp is 30 characters long, if it would not end with a 0 in the
// subseconds part. To fix this inconsistency, we add zeros before the Z.
while formatted.len() < 30 {
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe make the 30 into a constant?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea

Copy link
Owner

@Drakulix Drakulix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like a really solid start.

A couple of smaller remarks and also please add your changes in the Unreleased section of the CHANGELOG.md. Otherwise very good job!

module: LevelFilter::Off,
time_format: TimeFormat::Custom(format_description!("[hour]:[minute]:[second]")),
time_offset: UtcOffset::UTC,
time_padding: TimePadding::AddZeros,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this modifies the current default (of no padding), which I would want to release as a minor version. Can we maybe consider this to be Off by default?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving the padding as Off is certainly possible, but I would argue that setting a padding by default would improve usability a little.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree, but then we will need a new major version.

Err(err) => panic!("Invalid time format: {}", err),
_ => {}
let mut formatted: String =
formatted.unwrap_or_else(|err| panic!("Invalid time format: {}", err));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this drops the special casing for Format::StdIo, you should be able to use assign using the match here instead.

Suggested change
formatted.unwrap_or_else(|err| panic!("Invalid time format: {}", err));
match formatted {
Ok(res) => res,
Err(Format::StdIo(err)) => return Err(err),
Err(err) => panic!("Invalid time format: {}", err),
};

// subseconds part. To fix this inconsistency, we add zeros before the Z.
while formatted.len() < 30 {
formatted.insert(formatted.len() - 1, '0')
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like this is not very performant and there should be a way to not add the zeros one by one.

//
// the rfc3339 timestamp is 30 characters long, if it would not end with a 0 in the
// subseconds part. To fix this inconsistency, we add zeros before the Z.
while formatted.len() < 30 {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good idea

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants