Last active
June 7, 2017 12:58
-
-
Save thechriswalker/ae6cbd163219934f52f36e896c7170c9 to your computer and use it in GitHub Desktop.
higher order formatter/hook to enforce UTC times
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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