Drakulix / simplelog.rs

Simple Logging Facility for Rust
https://docs.rs/simplelog/
Apache License 2.0
411 stars 71 forks source link

logs with rfc3339 time have no consistent length #133

Open cscherrNT opened 11 months ago

cscherrNT commented 11 months ago

The width of the rfc3339 timestamp does not guarantee a fixed length, only that at least one digit in the subsecond part is present.

2023-08-01T11:10:57.758299073Z [WARN] trying to reinitialize the logger, ignoring
2023-08-01T11:10:57.75835825Z [WARN] trying to reinitialize the logger, ignoring
2023-08-01T11:10:57.75838119Z [WARN] trying to reinitialize the logger, ignoring
2023-08-01T11:10:57.758438693Z [WARN] trying to reinitialize the logger, ignoring
2023-08-01T11:10:57.758461685Z [INFO] Successfully ignored extra init

I generate a config like this:

let config = simplelog::ConfigBuilder::new()
    .set_time_format_rfc3339()
    .build();

Proposed fix: Add a config that enables padding for the timestamp.

Example:

let config = simplelog::ConfigBuilder::new()
    .set_time_format_rfc3339()
    .set_time_padding(simplelog::TimePadding::Right)
    .build();
2023-08-01T11:10:57.758299073Z [WARN] trying to reinitialize the logger, ignoring
2023-08-01T11:10:57.75835825Z  [WARN] trying to reinitialize the logger, ignoring
2023-08-01T11:10:57.75838119Z  [WARN] trying to reinitialize the logger, ignoring
2023-08-01T11:10:57.758438693Z [WARN] trying to reinitialize the logger, ignoring
2023-08-01T11:10:57.758461685Z [INFO] Successfully ignored extra init

Alternatively, show the zeros, or dont show subseconds at all

As I understand it rfc3339 would also allow it to show the zeros or not to show subseconds at all (like in the examples section).

let config = simplelog::ConfigBuilder::new()
    .set_time_format_rfc3339()
    .set_time_padding(simplelog::TimePadding::ShowZeros)
    .build();
2023-08-01T11:10:57.758299073Z [WARN] trying to reinitialize the logger, ignoring
2023-08-01T11:10:57.758358250Z [WARN] trying to reinitialize the logger, ignoring
2023-08-01T11:10:57.758381190Z [WARN] trying to reinitialize the logger, ignoring
2023-08-01T11:10:57.758438693Z [WARN] trying to reinitialize the logger, ignoring
2023-08-01T11:10:57.758461685Z [INFO] Successfully ignored extra init
Drakulix commented 11 months ago

Seems very reasonable.

I am happy to review and merge pull requests implementing any or all of the proposed solutions.

cscherrNT commented 11 months ago

I implemented a possible fix in #134