Use interface to create logger

By using the FieldLogger interface the logger supports both new logrus
loggers and existing field loggers such as an entry. This is useful if
the enduser uses an existing logrus logger with existing fields.
This commit is contained in:
Simon Sawert 2019-07-01 09:52:19 +02:00
parent 8887861896
commit 4b2c7d212a
No known key found for this signature in database
GPG Key ID: 9FDB4E07D9DACC91
1 changed files with 2 additions and 2 deletions

View File

@ -19,7 +19,7 @@ import (
var timeFormat = "02/Jan/2006:15:04:05 -0700" var timeFormat = "02/Jan/2006:15:04:05 -0700"
// Logger is the logrus logger handler // Logger is the logrus logger handler
func Logger(log *logrus.Logger) gin.HandlerFunc { func Logger(logger logrus.FieldLogger) gin.HandlerFunc {
hostname, err := os.Hostname() hostname, err := os.Hostname()
if err != nil { if err != nil {
hostname = "unknow" hostname = "unknow"
@ -40,7 +40,7 @@ func Logger(log *logrus.Logger) gin.HandlerFunc {
dataLength = 0 dataLength = 0
} }
entry := logrus.NewEntry(log).WithFields(logrus.Fields{ entry := logger.WithFields(logrus.Fields{
"hostname": hostname, "hostname": hostname,
"statusCode": statusCode, "statusCode": statusCode,
"latency": latency, // time to process "latency": latency, // time to process