Skip to content

Commit

Permalink
Added support custom logger
Browse files Browse the repository at this point in the history
  • Loading branch information
Arrim committed Mar 4, 2020
1 parent d370f81 commit 1b378d1
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions middleware/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"time"

"github.com/gin-gonic/gin"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
)

Expand All @@ -14,20 +15,20 @@ type LoggerConfig struct {
}

func Logger() gin.HandlerFunc {
return LoggerWithConfig(LoggerConfig{})
return LoggerWithConfig(LoggerConfig{}, log.Logger)
}

func LoggerWithWriter(out io.Writer, notlogged ...string) gin.HandlerFunc {
return LoggerWithConfig(LoggerConfig{
SkipPaths: notlogged,
})
}, log.Logger)
}

func LoggerWithConfig(conf LoggerConfig) gin.HandlerFunc {
return newLoggerMiddleware(conf)
func LoggerWithConfig(conf LoggerConfig, logger zerolog.Logger) gin.HandlerFunc {
return newLoggerMiddleware(conf, logger)
}

func newLoggerMiddleware(conf LoggerConfig) gin.HandlerFunc {
func newLoggerMiddleware(conf LoggerConfig, logger zerolog.Logger) gin.HandlerFunc {
skip := computeSkip(conf)

return func(c *gin.Context) {
Expand All @@ -44,7 +45,7 @@ func newLoggerMiddleware(conf LoggerConfig) gin.HandlerFunc {
return
}

log.Info().
logger.Info().
Str("StartTimestamp", fmt.Sprintf("%d", start.Unix())).
Str("ClientIP", c.ClientIP()).
Str("Method", c.Request.Method).
Expand Down

0 comments on commit 1b378d1

Please sign in to comment.