log15
log15 copied to clipboard
Allow easy support for different time formats
I'd like to use epoch time in my log files since I happen to be using epoch time within the server. Is it possible to have this natively supported as opposed to having to make a specific formatter for this? It seems useful in the sense of being able to reuse the existing formatter code versus adding a new formatter to modify only one field.
I think this is worth considering. I have also wanted to change the time format. In my case I want to add fractional seconds for more precision.
Also, we recognize that writing custom formatters is more burdensome that it should be and would like to improve that in the future.
You could write a simple handler to add this for you, but it's going to be a separate key instead of 't':
func epochHandler(nextHandler log.Handler) log.Handler {
return log.FuncHandler(func(r *log.Record) {
r.Ctx = append(r.Ctx, "time_epoch", r.Time.Unix())
nextHandler.Log(r)
})
}
That works, but the only drawback is it is redundant data. I try and log everything, or rather as much as possible. Over time, this translates to more network as well as disk bandwidth.
Yep, I agree. It's a temporary work around. We'd like to make formatters more flexible to tweaking, like Chris said in a future release.
In my case I want to add fractional seconds for more precision.
I would like this too.
I could see a range of ways to do this:
- I could make my own formatter, but I'd have to copy-paste
formatLogfmtValueandformatShared. - Make these const into public variables. No-one likes public globals, but it would be simple and effective. This seems the most intuitively appealing to me.
- Make a package level
SetTimeFormat(orSetFormats(...)for all of them) - Refactor
LogfmtFormatto take them as parameters (and keep a default so the API doesn't change).
Is there a better way? If any of the above appeal I'd be happy to submit a pull request.
To date I've just modified the timeFormat constant in my internal fork of log15.
If we just want to resolve the issue of customizing the time format produced by LogfmtFormat—not worrying about the larger issue of making formatters more flexible—then I think the way forward is to add support for functional options to LogfmtFormat and provide an option to control the time format.
@grahamking I'll gladly review the code if you want to submit a pull request along those lines.
Hello, Currently, the simple way I found for modifying the time format (for example for millisecond precision) is to change these constants, but that seems to me little weird.
Does it exist another way? Thx in adv