Skip to content

Instantly share code, notes, and snippets.

@thechriswalker
Last active June 7, 2017 12:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save thechriswalker/ae6cbd163219934f52f36e896c7170c9 to your computer and use it in GitHub Desktop.
Save thechriswalker/ae6cbd163219934f52f36e896c7170c9 to your computer and use it in GitHub Desktop.
higher order formatter/hook to enforce UTC times
package log
import (
log "github.com/sirupsen/logrus"
)
/**
* You can enforce UTC on a single formatter with this wrapper
*/
func EnforceUTC(f log.Formatter) log.Formatter {
return utc_formatter{fmt: f}
}
type utc_formatter struct {
fmt log.Formatter
}
func (u utc_formatter) Format(e *log.Entry) ([]byte, error) {
e.Time = e.Time.UTC()
return u.fmt.Format(e)
}
/**
* You can enforce UTC on all formatters with this Hook
*/
type EnforceUTCHook struct{}
func (_ EnforceUTCHook) Fire(e *log.Entry) error {
e.Time = e.Time.UTC()
return nil
}
func (e EnforceUTCHook) Levels() []log.Level {
return log.AllLevels
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment