Skip to content

Commit

Permalink
chore(logger): replace trace ID with custom field
Browse files Browse the repository at this point in the history
  • Loading branch information
appleboy committed Sep 16, 2022
1 parent 2045af8 commit 731b1c7
Show file tree
Hide file tree
Showing 7 changed files with 8 additions and 25 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ func main() {
c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
})

// add custom fields.
r.GET("/id", requestid.New(requestid.WithGenerator(func() string {
return "foobar"
})), logger.SetLogger(
Expand All @@ -79,6 +80,7 @@ func main() {
Str("id", requestid.Get(c)).
Str("foo", "bar").
Str("path", c.Request.URL.Path).
Str("traceID", trace.SpanFromContext(c.Request.Context()).SpanContext().TraceID().String()).
Logger()
}),
), func(c *gin.Context) {
Expand Down
1 change: 1 addition & 0 deletions _example/go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ require (
github.com/gin-contrib/requestid v0.0.5
github.com/gin-gonic/gin v1.8.1
github.com/rs/zerolog v1.28.0
go.opentelemetry.io/otel/trace v1.10.0
)
3 changes: 3 additions & 0 deletions _example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
"github.com/gin-gonic/gin"
"github.com/rs/zerolog"
"github.com/rs/zerolog/log"
"go.opentelemetry.io/otel/trace"
)

var rxURL = regexp.MustCompile(`^/regexp\d*`)
Expand Down Expand Up @@ -58,6 +59,7 @@ func main() {
c.String(http.StatusOK, "pong "+fmt.Sprint(time.Now().Unix()))
})

// add custom fields.
r.GET("/id", requestid.New(requestid.WithGenerator(func() string {
return "foobar"
})), logger.SetLogger(
Expand All @@ -66,6 +68,7 @@ func main() {
Str("id", requestid.Get(c)).
Str("foo", "bar").
Str("path", c.Request.URL.Path).
Str("traceID", trace.SpanFromContext(c.Request.Context()).SpanContext().TraceID().String()).
Logger()
}),
), func(c *gin.Context) {
Expand Down
1 change: 0 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,4 @@ require (
github.com/mattn/go-isatty v0.0.16
github.com/rs/zerolog v1.28.0
github.com/stretchr/testify v1.8.0
go.opentelemetry.io/otel/trace v1.10.0
)
11 changes: 2 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,6 @@ github.com/gin-contrib/sse v0.1.0 h1:Y/yl/+YNO8GZSjAhjMsSuLt29uWRFHdHYUb5lYOV9qE
github.com/gin-contrib/sse v0.1.0/go.mod h1:RHrZQHXnP2xjPF+u1gW/2HnVO7nvIa9PG3Gm+fLHvGI=
github.com/gin-gonic/gin v1.8.1 h1:4+fr/el88TOO3ewCmQr8cx/CtZ/umlIRIs5M4NTNjf8=
github.com/gin-gonic/gin v1.8.1/go.mod h1:ji8BvRH1azfM+SYow9zQ6SZMvR8qOMZHmsCuWR9tTTk=
github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE=
github.com/go-playground/assert/v2 v2.0.1 h1:MsBgLAaY856+nPRTKrp3/OZK38U/wa0CcBYNjji3q3A=
github.com/go-playground/assert/v2 v2.0.1/go.mod h1:VDjEfimB/XKnb+ZQfWdccd7VUvScMdVu0Titje2rxJ4=
github.com/go-playground/locales v0.14.0 h1:u50s323jtVGugKlcYeyzC0etD1HifMjqmJqb8WugfUU=
Expand All @@ -22,9 +19,8 @@ github.com/goccy/go-json v0.9.7 h1:IcB+Aqpx/iMHu5Yooh7jEzJk1JZ7Pjtmys2ukPr7EeM=
github.com/goccy/go-json v0.9.7/go.mod h1:6MelG93GURQebXPDq3khkgXZkazVtN9CRI+MGFi0w8I=
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaSAoJOfIk=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/gofuzz v1.0.0/go.mod h1:dBl0BpW6vV/+mYPU4Po3pmUjxk6FQPldtuIdl/M65Eg=
github.com/json-iterator/go v1.1.12 h1:PV8peI4a0ysnczrg+LtxykD8LfKY9ML6u2jnxaEnrnM=
github.com/json-iterator/go v1.1.12/go.mod h1:e30LSqwooZae/UwlEbR2852Gd8hjQvJoHmT4TnhNGBo=
Expand Down Expand Up @@ -71,10 +67,6 @@ github.com/ugorji/go v1.2.7 h1:qYhyWUUd6WbiM+C6JZAUkIJt/1WrjzNHY9+KCIjVqTo=
github.com/ugorji/go v1.2.7/go.mod h1:nF9osbDWLy6bDVv/Rtoh6QgnvNDpmCalQV5urGCCS6M=
github.com/ugorji/go/codec v1.2.7 h1:YPXUKf7fYbp/y8xloBqZOw2qaVggbfwMlI8WM3wZUJ0=
github.com/ugorji/go/codec v1.2.7/go.mod h1:WGN1fab3R1fzQlVQTkfxVtIBhWDRqOviHU95kRgeqEY=
go.opentelemetry.io/otel v1.10.0 h1:Y7DTJMR6zs1xkS/upamJYk0SxxN4C9AqRd77jmZnyY4=
go.opentelemetry.io/otel v1.10.0/go.mod h1:NbvWjCthWHKBEUMpf0/v8ZRZlni86PpGFEMA9pnQSnQ=
go.opentelemetry.io/otel/trace v1.10.0 h1:npQMbR8o7mum8uF95yFbOEJffhs1sbCOfDh8zAJiH5E=
go.opentelemetry.io/otel/trace v1.10.0/go.mod h1:Sij3YYczqAdz+EhmGhE6TpTxUO5/F/AzrK+kxfGqySM=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97 h1:/UOmuWzQfxxo9UtlXMwuQU8CMgg1eZXqTRwkSQJWKOI=
golang.org/x/crypto v0.0.0-20210711020723-a769d52b0f97/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/net v0.0.0-20210226172049-e18ecbb05110 h1:qWPm9rbaAMKs8Bq/9LRpbMqxWRVUAQwMI9fVrssnTfw=
Expand All @@ -91,6 +83,7 @@ golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6 h1:aRYxNxv6iGQlyVaZmk6ZgYEDa+Jg18DxebPSrd6bg1M=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543 h1:E7g+9GITq07hpfrRu66IVDexMakfv52eLZ2CXBWiKr4=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
google.golang.org/protobuf v1.28.0 h1:w43yiav+6bVFTBQFZX0r7ipe9JQ1QsbMgHwbBziscLw=
Expand Down
9 changes: 0 additions & 9 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/gin-gonic/gin"
"github.com/mattn/go-isatty"
"github.com/rs/zerolog"
"go.opentelemetry.io/otel/trace"
)

type Fn func(*gin.Context, zerolog.Logger) zerolog.Logger
Expand All @@ -31,8 +30,6 @@ type config struct {
clientErrorLevel zerolog.Level
// the log level used for request with status code >= 500
serverErrorLevel zerolog.Level
// optionally log Open Telemetry TraceID
traceID bool
}

// SetLogger initializes the logging middleware.
Expand Down Expand Up @@ -110,12 +107,6 @@ func SetLogger(opts ...Option) gin.HandlerFunc {
Dur("latency", latency).
Str("user_agent", c.Request.UserAgent()).Logger()

if cfg.traceID {
l = l.With().
Str("traceID", trace.SpanFromContext(c.Request.Context()).SpanContext().TraceID().String()).
Logger()
}

msg := "Request"
if len(c.Errors) > 0 {
msg = c.Errors.String()
Expand Down
6 changes: 0 additions & 6 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,3 @@ func WithServerErrorLevel(lvl zerolog.Level) Option {
c.serverErrorLevel = lvl
})
}

func WithAddTraceID(v bool) Option {
return optionFunc(func(c *config) {
c.traceID = v
})
}

0 comments on commit 731b1c7

Please sign in to comment.