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

feat: add specific log-message #106

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
13 changes: 13 additions & 0 deletions _example/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"errors"
"fmt"
"math/rand"
"net/http"
Expand Down Expand Up @@ -109,6 +110,18 @@ func main() {
c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
})

// Example of a custom message to the log
r.GET("/message", logger.SetLogger(
logger.WithLogger(func(_ *gin.Context, l zerolog.Logger) zerolog.Logger {
return l.Output(gin.DefaultWriter).With().Logger()
}),
logger.WithMessage("Request ended"),
), func(c *gin.Context) {
c.Error(errors.New("some error has occured here"))
c.Error(errors.New("and some error has occured there"))
c.String(http.StatusBadGateway, "pong "+fmt.Sprint(time.Now().Unix()))
})

// Example of skipper usage
r.GET("/health", logger.SetLogger(
logger.WithSkipper(func(c *gin.Context) bool {
Expand Down
8 changes: 5 additions & 3 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,8 @@ type config struct {
serverErrorLevel zerolog.Level
// pathLevels is a map of specific paths to log levels for requests with status code < 400.
pathLevels map[string]zerolog.Level
// message is a custom string that sets a log-message when http-request has finished
message string
}

const loggerKey = "_gin-contrib/logger_"
Expand Down Expand Up @@ -85,6 +87,7 @@ func SetLogger(opts ...Option) gin.HandlerFunc {
clientErrorLevel: zerolog.WarnLevel,
serverErrorLevel: zerolog.ErrorLevel,
output: os.Stderr,
message: "Request",
}

// Apply each option to the config
Expand Down Expand Up @@ -151,9 +154,8 @@ func SetLogger(opts ...Option) gin.HandlerFunc {
}
latency := end.Sub(start)

msg := "Request"
if len(c.Errors) > 0 {
msg = c.Errors.String()
cfg.message += " with errors: " + c.Errors.String()
}

var evt *zerolog.Event
Expand Down Expand Up @@ -182,7 +184,7 @@ func SetLogger(opts ...Option) gin.HandlerFunc {
Dur("latency", latency).
Str("user_agent", c.Request.UserAgent()).
Int("body_size", c.Writer.Size()).
Msg(msg)
Msg(cfg.message)
}
}
}
Expand Down
8 changes: 8 additions & 0 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,11 @@ func WithContext(fn func(*gin.Context, *zerolog.Event) *zerolog.Event) Option {
c.context = fn
})
}

// WithMessage is an option to set a custom log-message, when http-request has finished and logged
// It takes a string as an argument and returns an Option.
func WithMessage(message string) Option {
return optionFunc(func(c *config) {
c.message = message
})
}