inconshreveable / log15

Structured, composable logging for Go
https://godoc.org/github.com/inconshreveable/log15
Other
1.1k stars 144 forks source link

Allow logging in UTC #149

Closed timou closed 6 years ago

timou commented 6 years ago

We have a requirement to have log times in UTC. The default Logger implementation logs in localtime, and cannot, I believe, be easily changed. The solution I used was to re-implement the default logger, just to change one line in the logger.write method to use time.Now().UTC() instead of time.Now().

May I propose that, without changing the Logger interface, we add a SetUTC(v bool) method to the default logger? It would default to false.

I am happy to contribute this if others think it is worthwhile.

ChrisHines commented 6 years ago

I believe you can implement a log15.Handler that updates the Record.Time field to have UTC time without adding any methods to the default logger. I believe the code would look something like this:

func UTCHandler(h log15.Handler) log15.Handler {
    return log15.FuncHandler(func(r *log15.Record) error {
        r.Time = r.Time.UTC()
        return h.Log(r)
    })
}
timou commented 6 years ago

This is a super idea, and I wish I'd thought of it! Thanks Chris.