mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2025-12-29 15:56:16 -06:00
set logger
This commit is contained in:
parent
cf6d2beb82
commit
d58c2b7b0d
2 changed files with 15 additions and 28 deletions
|
|
@ -19,7 +19,6 @@
|
|||
package router
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
|
|
@ -31,8 +30,7 @@ var skipPaths = map[string]interface{}{
|
|||
"/api/v1/streaming": nil,
|
||||
}
|
||||
|
||||
// LoggerWithConfig instance a Logger middleware with config.
|
||||
func LoggerWithConfig(log *logrus.Logger) gin.HandlerFunc {
|
||||
func loggerWithConfig(log *logrus.Logger) gin.HandlerFunc {
|
||||
logHandler := func(c *gin.Context) {
|
||||
start := time.Now()
|
||||
path := c.Request.URL.Path
|
||||
|
|
@ -54,12 +52,21 @@ func LoggerWithConfig(log *logrus.Logger) gin.HandlerFunc {
|
|||
}
|
||||
|
||||
l := log.WithFields(logrus.Fields{
|
||||
"latency": fmt.Sprintf("%13v", latency),
|
||||
"latency": latency,
|
||||
"clientIP": clientIP,
|
||||
"method": method,
|
||||
"statusCode": statusCode,
|
||||
"path": path,
|
||||
})
|
||||
|
||||
http.StatusText(statusCode)
|
||||
if errorMessage != "" {
|
||||
l.Error(errorMessage)
|
||||
return
|
||||
}
|
||||
|
||||
l.Infof("%s: wrote %d bytes in %v", http.StatusText(statusCode), bodySize, latency)
|
||||
}
|
||||
}
|
||||
|
||||
return logHandler
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,7 +21,6 @@ package router
|
|||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"log"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
|
|
@ -105,32 +104,13 @@ func (r *router) Stop(ctx context.Context) error {
|
|||
// The given DB is only used in the New function for parsing config values, and is not otherwise
|
||||
// pinned to the router.
|
||||
func New(ctx context.Context, cfg *config.Config, db db.DB, logger *logrus.Logger) (Router, error) {
|
||||
|
||||
// gin has different log modes; for convenience, we match the gin log mode to
|
||||
// whatever log mode has been set for logrus
|
||||
lvl, err := logrus.ParseLevel(cfg.LogLevel)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("couldn't parse log level %s to set router level: %s", cfg.LogLevel, err)
|
||||
}
|
||||
switch lvl {
|
||||
case logrus.TraceLevel, logrus.DebugLevel:
|
||||
gin.SetMode(gin.DebugMode)
|
||||
default:
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
}
|
||||
|
||||
gin.SetMode(gin.ReleaseMode)
|
||||
|
||||
// create the actual engine here -- this is the core request routing handler for gts
|
||||
engine := gin.New()
|
||||
|
||||
// instruct gin to write out to our logger
|
||||
loggingConfig := gin.LoggerConfig{
|
||||
Formatter: logFormatter,
|
||||
Output: log.Writer(),
|
||||
SkipPaths: dontLog,
|
||||
}
|
||||
|
||||
engine.Use(gin.RecoveryWithWriter(logger.Writer()))
|
||||
engine.Use(gin.LoggerWithConfig(loggingConfig))
|
||||
engine.Use(loggerWithConfig(logger))
|
||||
|
||||
// 8 MiB
|
||||
engine.MaxMultipartMemory = 8 << 20
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue