-
Notifications
You must be signed in to change notification settings - Fork 144
Allow custom timestamp formats. #35
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
Conversation
Past experience has taught me that you'll never have one logging time format that pleases everyone, so perhaps we should just allow it to be configurable. :) We can use `StrftimeItems` to parse the format string once when we build the `Logger`. We limit the lifetime of the format string to `'static` as it keeps things simple. (If we wanted to accept arbitrary lifetimes, I think we'd either need to introduce a lifetime parameter on `Logger`, or have a self-referential struct somewhere). This commit also removes the nanoseconds, as suggested in #34. It does not alias `+00:00` to `+Z`, as there doesn't seem to be a way to do this with `chrono::format::strftime`. (It is possible to do it by constructing our own iterator of formatting `Item`s, but I think `chrono` should learn to allow this via `StftimeItems` instead).
|
r? @KodrAus |
|
Er, whoops. This doesn't actually do what I intended it to do, as it does parse the format string each time a log message is formatted. I think I meant to drain the |
|
Thanks @mjkillough! I think we'll want to think about the question of supporting multiple formats eventually and it might look a lot like this, but I'm a bit nervous about leaking We can get the default more compact RFC3339 format by manually constructing a slice of items, something like: use chrono::format::Item;
const ITEMS: &'static [Item<'static>] = {
use chrono::format::Item::*;
use chrono::format::Numeric::*;
use chrono::format::Fixed::*;
use chrono::format::Pad::*;
&[
Numeric(Year, Zero),
Literal("-"),
Numeric(Month, Zero),
Literal("-"),
Numeric(Day, Zero),
Literal("T"),
Numeric(Hour, Zero),
Literal(":"),
Numeric(Minute, Zero),
Literal(":"),
Numeric(Second, Zero),
Fixed(TimezoneOffsetZ),
]
}It's not exactly pretty, but it's simple and means we can keep all the formatting localised to the What do you think? Thanks again for tackling this! |
|
It turns out the gymnastics I went through in the last PR for making a custom iterator were also not necessary. We can just use |
|
Thanks a lot for the quick response!
Yeah, that's fair. I sat down to do this because I was curious about how difficult it would be, but I agree it might be worth deferring this type of customization until later. I had hoped we could expose it as "
That's a great point - I hadn't considered that.
Yeah, I think that is best for now. It's certainly concise as a slice, and it does allow us to use I'll close this. :) |
This PR contains the following updates: | Package | Type | Update | Change | |---|---|---|---| | [actions/checkout](https://redirect.github.com/actions/checkout) | action | major | `v5` -> `v6` | --- ### Release Notes <details> <summary>actions/checkout (actions/checkout)</summary> ### [`v6`](https://redirect.github.com/actions/checkout/compare/v5...v6) [Compare Source](https://redirect.github.com/actions/checkout/compare/v5...v6) </details> --- ### Configuration 📅 **Schedule**: Branch creation - "before 5am on the first day of the month" (UTC), Automerge - At any time (no schedule defined). 🚦 **Automerge**: Disabled by config. Please merge this manually once you are satisfied. ♻ **Rebasing**: Whenever PR becomes conflicted, or you tick the rebase/retry checkbox. 🔕 **Ignore**: Close this PR and you won't be reminded about this update again. --- - [ ] <!-- rebase-check -->If you want to rebase/retry this PR, check this box --- This PR was generated by [Mend Renovate](https://mend.io/renovate/). View the [repository job log](https://developer.mend.io/github/epage/_rust). <!--renovate-debug:eyJjcmVhdGVkSW5WZXIiOiI0Mi4xOS45IiwidXBkYXRlZEluVmVyIjoiNDIuMTkuOSIsInRhcmdldEJyYW5jaCI6Im1haW4iLCJsYWJlbHMiOltdfQ==-->
Past experience has taught me that you'll never have one logging time
format that pleases everyone, so perhaps we should just allow it to be
configurable. :)
We can use
StrftimeItemsto parse the format string once when we buildthe
Logger. We limit the lifetime of the format string to'staticasit keeps things simple. (If we wanted to accept arbitrary lifetimes,
I think we'd either need to introduce a lifetime parameter on
Logger,or have a self-referential struct somewhere).
This commit also removes the nanoseconds, as suggested in #34. It does
not alias
+00:00to+Z, as there doesn't seem to be a way to do thiswith
chrono::format::strftime. (It is possible to do it byconstructing our own iterator of formatting
Items, but I thinkchronoshould learn to allow this viaStftimeItemsinstead).