Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Link logger with gin context. #76

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ type config struct {
serverErrorLevel zerolog.Level
}

const loggerKey = "_gin-contrib/logger_"

var isTerm bool = isatty.IsTerminal(os.Stdout.Fd())

// SetLogger initializes the logging middleware.
Expand Down Expand Up @@ -79,9 +81,7 @@ func SetLogger(opts ...Option) gin.HandlerFunc {
path = path + "?" + raw
}

c.Next()
track := true

if _, ok := skip[path]; ok {
track = false
}
Expand All @@ -97,6 +97,17 @@ func SetLogger(opts ...Option) gin.HandlerFunc {
}
}

if track {
l = l.With().
Str("method", c.Request.Method).
Str("path", path).
Str("ip", c.ClientIP()).
Str("user_agent", c.Request.UserAgent()).Logger()
}
c.Set(loggerKey, l)

c.Next()

if track {
end := time.Now()
if cfg.utc {
Expand All @@ -106,11 +117,7 @@ func SetLogger(opts ...Option) gin.HandlerFunc {

l = l.With().
Int("status", c.Writer.Status()).
Str("method", c.Request.Method).
Str("path", path).
Str("ip", c.ClientIP()).
Dur("latency", latency).
Str("user_agent", c.Request.UserAgent()).Logger()
Dur("latency", latency).Logger()

msg := "Request"
if len(c.Errors) > 0 {
Expand Down Expand Up @@ -141,3 +148,8 @@ func SetLogger(opts ...Option) gin.HandlerFunc {
func ParseLevel(levelStr string) (zerolog.Level, error) {
return zerolog.ParseLevel(levelStr)
}

// Get return the logger associated with a gin context
func Get(c *gin.Context) zerolog.Logger {
return c.MustGet(loggerKey).(zerolog.Logger)
}
11 changes: 11 additions & 0 deletions logger_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,17 @@ func TestLogger(t *testing.T) {
assert.Contains(t, buffer.String(), "/example")
assert.Contains(t, buffer.String(), "ERR")
assert.Contains(t, buffer.String(), "path=/example?a=100")

buffer.Reset()
r.GET("/example-with-additional-log", func(ctx *gin.Context) {
l := Get(ctx)
l.Info().Msg("additional log")
})
performRequest(r, "GET", "/example-with-additional-log")
assert.Contains(t, buffer.String(), "200")
assert.Contains(t, buffer.String(), "GET")
assert.Contains(t, buffer.String(), "/example-with-additional-log")
assert.Contains(t, buffer.String(), "additional log")
}

func TestLoggerWithLogger(t *testing.T) {
Expand Down
Loading