replace zxq.co/ripple/hanayo
This commit is contained in:
10
vendor/github.com/johnniedoe/contrib/ginrus/Godeps/Godeps.json
generated
vendored
Normal file
10
vendor/github.com/johnniedoe/contrib/ginrus/Godeps/Godeps.json
generated
vendored
Normal file
@@ -0,0 +1,10 @@
|
||||
{
|
||||
"ImportPath": "github.com/gin-gonic/contrib/ginrus",
|
||||
"GoVersion": "go1.3",
|
||||
"Deps": [
|
||||
{
|
||||
"ImportPath": "github.com/gin-gonic/gin",
|
||||
"Rev": "ac0ad2fed865d40a0adc1ac3ccaadc3acff5db4b"
|
||||
}
|
||||
]
|
||||
}
|
39
vendor/github.com/johnniedoe/contrib/ginrus/example/example.go
generated
vendored
Normal file
39
vendor/github.com/johnniedoe/contrib/ginrus/example/example.go
generated
vendored
Normal file
@@ -0,0 +1,39 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
"github.com/gin-gonic/contrib/ginrus"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
func main() {
|
||||
r := gin.New()
|
||||
|
||||
// Add a ginrus middleware, which:
|
||||
// - Logs all requests, like a combined access and error log.
|
||||
// - Logs to stdout.
|
||||
// - RFC3339 with UTC time format.
|
||||
r.Use(ginrus.Ginrus(logrus.StandardLogger(), time.RFC3339, true))
|
||||
|
||||
// Add similar middleware, but:
|
||||
// - Only logs requests with errors, like an error log.
|
||||
// - Logs to stderr instead of stdout.
|
||||
// - Local time zone instead of UTC.
|
||||
logger := logrus.New()
|
||||
logger.Level = logrus.ErrorLevel
|
||||
logger.SetOutput(os.Stderr)
|
||||
r.Use(ginrus.Ginrus(logger, time.RFC3339, false))
|
||||
|
||||
// Example ping request.
|
||||
r.GET("/ping", func(c *gin.Context) {
|
||||
c.String(200, "pong "+fmt.Sprint(time.Now().Unix()))
|
||||
})
|
||||
|
||||
// Listen and Server in 0.0.0.0:8080
|
||||
r.Run(":8080")
|
||||
}
|
51
vendor/github.com/johnniedoe/contrib/ginrus/ginrus.go
generated
vendored
Normal file
51
vendor/github.com/johnniedoe/contrib/ginrus/ginrus.go
generated
vendored
Normal file
@@ -0,0 +1,51 @@
|
||||
// Package ginrus provides log handling using logrus package.
|
||||
//
|
||||
// Based on github.com/stephenmuss/ginerus but adds more options.
|
||||
package ginrus
|
||||
|
||||
import (
|
||||
"time"
|
||||
|
||||
"github.com/Sirupsen/logrus"
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
||||
// Ginrus returns a gin.HandlerFunc (middleware) that logs requests using logrus.
|
||||
//
|
||||
// Requests with errors are logged using logrus.Error().
|
||||
// Requests without errors are logged using logrus.Info().
|
||||
//
|
||||
// It receives:
|
||||
// 1. A time package format string (e.g. time.RFC3339).
|
||||
// 2. A boolean stating whether to use UTC time zone or local.
|
||||
func Ginrus(logger *logrus.Logger, timeFormat string, utc bool) gin.HandlerFunc {
|
||||
return func(c *gin.Context) {
|
||||
start := time.Now()
|
||||
// some evil middlewares modify this values
|
||||
path := c.Request.URL.Path
|
||||
c.Next()
|
||||
|
||||
end := time.Now()
|
||||
latency := end.Sub(start)
|
||||
if utc {
|
||||
end = end.UTC()
|
||||
}
|
||||
|
||||
entry := logger.WithFields(logrus.Fields{
|
||||
"status": c.Writer.Status(),
|
||||
"method": c.Request.Method,
|
||||
"path": path,
|
||||
"ip": c.ClientIP(),
|
||||
"latency": latency,
|
||||
"user-agent": c.Request.UserAgent(),
|
||||
"time": end.Format(timeFormat),
|
||||
})
|
||||
|
||||
if len(c.Errors) > 0 {
|
||||
// Append error field if this is an erroneous request.
|
||||
entry.Error(c.Errors.String())
|
||||
} else {
|
||||
entry.Info()
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user