Join GitHub today
GitHub is home to over 31 million developers working together to host and review code, manage projects, and build software together.
Sign upAllow custom timestamp formats. #35
Conversation
This comment has been minimized.
This comment has been minimized.
|
r? @KodrAus |
This comment has been minimized.
This comment has been minimized.
|
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 |
This comment has been minimized.
This comment has been minimized.
|
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! |
This comment has been minimized.
This comment has been minimized.
|
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 |
This comment has been minimized.
This comment has been minimized.
|
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. :) |
mjkillough commentedNov 6, 2017
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).