log15 icon indicating copy to clipboard operation
log15 copied to clipboard

Allow easy support for different time formats

Open mobileben opened this issue 11 years ago • 7 comments

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.

mobileben avatar Nov 17 '14 02:11 mobileben

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.

ChrisHines avatar Nov 17 '14 06:11 ChrisHines

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)
    })
}

inconshreveable avatar Nov 19 '14 20:11 inconshreveable

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.

mobileben avatar Nov 20 '14 07:11 mobileben

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.

inconshreveable avatar Nov 21 '14 09:11 inconshreveable

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 formatLogfmtValue and formatShared.
  • 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 (or SetFormats(...) for all of them)
  • Refactor LogfmtFormat to 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.

grahamking avatar May 14 '15 22:05 grahamking

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.

ChrisHines avatar May 15 '15 04:05 ChrisHines

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

jerome-laforge avatar Dec 15 '15 16:12 jerome-laforge