Skip to content

Commit

Permalink
Merge pull request #1 from Arrim/glog
Browse files Browse the repository at this point in the history
Added support custom logger
  • Loading branch information
ildarusmanov committed Mar 4, 2020
2 parents d370f81 + 1b378d1 commit 3c0714a
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 3c0714a

Please sign in to comment.