This repository has been archived by the owner on Oct 22, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
757 additions
and
275 deletions.
There are no files selected for viewing
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
package middleware | ||
|
||
import ( | ||
"fmt" | ||
"math" | ||
"net/http" | ||
"os" | ||
"time" | ||
|
||
"github.com/gin-gonic/gin" | ||
"github.com/sirupsen/logrus" | ||
) | ||
|
||
var timeFormat = "02/Jan/2006:15:04:05 -0700" | ||
|
||
// Logger is the logrus logger handler | ||
func Logger(logger logrus.FieldLogger, notLogged ...string) gin.HandlerFunc { | ||
hostname, err := os.Hostname() | ||
if err != nil { | ||
hostname = "unknow" | ||
} | ||
|
||
var skip map[string]struct{} | ||
|
||
if length := len(notLogged); length > 0 { | ||
skip = make(map[string]struct{}, length) | ||
|
||
for _, p := range notLogged { | ||
skip[p] = struct{}{} | ||
} | ||
} | ||
|
||
return func(c *gin.Context) { | ||
// other handler can change c.Path so: | ||
path := c.Request.URL.Path | ||
start := time.Now() | ||
c.Next() | ||
stop := time.Since(start) | ||
latency := int(math.Ceil(float64(stop.Nanoseconds()) / 1000000.0)) | ||
statusCode := c.Writer.Status() | ||
clientIP := c.ClientIP() | ||
clientUserAgent := c.Request.UserAgent() | ||
referer := c.Request.Referer() | ||
dataLength := c.Writer.Size() | ||
if dataLength < 0 { | ||
dataLength = 0 | ||
} | ||
|
||
if _, ok := skip[path]; ok { | ||
return | ||
} | ||
|
||
entry := logger.WithFields(logrus.Fields{ | ||
"hostname": hostname, | ||
"statusCode": statusCode, | ||
"latency": latency, // time to process | ||
"clientIP": clientIP, | ||
"method": c.Request.Method, | ||
"path": path, | ||
"referer": referer, | ||
"dataLength": dataLength, | ||
"userAgent": clientUserAgent, | ||
}) | ||
|
||
if len(c.Errors) > 0 { | ||
entry.Error(c.Errors.ByType(gin.ErrorTypePrivate).String()) | ||
} else { | ||
msg := fmt.Sprintf("%s - %s [%s] \"%s %s\" %d %d \"%s\" \"%s\" (%dms)", clientIP, hostname, time.Now().Format(timeFormat), c.Request.Method, path, statusCode, dataLength, referer, clientUserAgent, latency) | ||
if statusCode >= http.StatusInternalServerError { | ||
entry.Error(msg) | ||
} else if statusCode >= http.StatusBadRequest { | ||
entry.Warn(msg) | ||
} else { | ||
entry.Info(msg) | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.