Skip to content

Commit

Permalink
Introduce logger v2 (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
piotrowski authored Sep 25, 2024
1 parent e394241 commit 8b4ef09
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 104 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ l := logger.Use(zapLogger)

Error with a message and extra fields;
```go
import l "github.com/surfe/logger"
import "github.com/surfe/logger"

...

fields := []any{l.UserKey, "abc@xyz.com"}
l.Log().Err(err).With(ctx, fields...).Error("Add Contact (SF)")
logger.Log(ctx).Err(err).With(fields...).Error("Add Contact (SF)")
```

### Echo Middleware
Expand Down
25 changes: 12 additions & 13 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,28 +1,27 @@
module github.com/surfe/logger

go 1.21
go 1.22

require (
github.com/golang-jwt/jwt/v5 v5.2.0
github.com/labstack/echo/v4 v4.7.2
github.com/labstack/gommon v0.3.1
github.com/golang-jwt/jwt/v5 v5.2.1
github.com/labstack/echo/v4 v4.12.0
github.com/labstack/gommon v0.4.2
github.com/pkg/errors v0.9.1
github.com/stretchr/testify v1.8.0
go.uber.org/zap v1.23.0
github.com/stretchr/testify v1.9.0
go.uber.org/zap v1.27.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.16 // indirect
github.com/mattn/go-isatty v0.0.20 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.2 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/crypto v0.1.0 // indirect
golang.org/x/net v0.1.0 // indirect
golang.org/x/sys v0.1.0 // indirect
golang.org/x/text v0.4.0 // indirect
go.uber.org/multierr v1.11.0 // indirect
golang.org/x/crypto v0.27.0 // indirect
golang.org/x/net v0.29.0 // indirect
golang.org/x/sys v0.25.0 // indirect
golang.org/x/text v0.18.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
67 changes: 25 additions & 42 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,60 +1,43 @@
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/golang-jwt/jwt/v5 v5.2.0 h1:d/ix8ftRUorsN+5eMIlF4T6J8CAt9rch3My2winC1Jw=
github.com/golang-jwt/jwt/v5 v5.2.0/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/labstack/echo/v4 v4.7.2 h1:Kv2/p8OaQ+M6Ex4eGimg9b9e6icoxA42JSlOR3msKtI=
github.com/labstack/echo/v4 v4.7.2/go.mod h1:xkCDAdFCIf8jsFQ5NnbK7oqaF/yU1A1X20Ltm0OvSks=
github.com/labstack/gommon v0.3.1 h1:OomWaJXm7xR6L1HmEtGyQf26TEn7V6X88mktX9kee9o=
github.com/labstack/gommon v0.3.1/go.mod h1:uW6kP17uPlLJsD3ijUYn3/M5bAxtlZhMI6m3MFxTMTM=
github.com/mattn/go-colorable v0.1.11/go.mod h1:u5H1YNBxpqRaxsYJYSkiCWKzEfiAb1Gb520KVy5xxl4=
github.com/golang-jwt/jwt/v5 v5.2.1 h1:OuVbFODueb089Lh128TAcimifWaLhJwVflnrgM17wHk=
github.com/golang-jwt/jwt/v5 v5.2.1/go.mod h1:pqrtFR0X4osieyHYxtmOUWsAWrfe1Q5UVIyoH402zdk=
github.com/labstack/echo/v4 v4.12.0 h1:IKpw49IMryVB2p1a4dzwlhP1O2Tf2E0Ir/450lH+kI0=
github.com/labstack/echo/v4 v4.12.0/go.mod h1:UP9Cr2DJXbOK3Kr9ONYzNowSh7HP0aG0ShAyycHSJvM=
github.com/labstack/gommon v0.4.2 h1:F8qTUNXgG1+6WQmqoUWnz8WiEU60mXVVw0P4ht1WRA0=
github.com/labstack/gommon v0.4.2/go.mod h1:QlUFxVM+SNXhDL/Z7YhocGIBYOiwB0mXm1+1bAPHPyU=
github.com/mattn/go-colorable v0.1.13 h1:fFA4WZxdEF4tXPZVKMLwD8oUnCTTo08duU7wxecdEvA=
github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovkB8vQcUbaXHg=
github.com/mattn/go-isatty v0.0.14/go.mod h1:7GGIvUiUoEMVVmxf/4nioHXj79iQHKdU27kJ6hsGG94=
github.com/mattn/go-isatty v0.0.16 h1:bq3VjFmv/sOjHtdEhmkEV4x1AJtvUvOJ2PFAZ5+peKQ=
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.20 h1:xfD0iDuEKnDkl03q4limB+vH+GxLEtL/jb4xVJSWWEY=
github.com/mattn/go-isatty v0.0.20/go.mod h1:W+V8PltTTMOvKvAeJH7IuucS94S2C6jfK/D7dTCTo3Y=
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.2.1/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
github.com/valyala/fasttemplate v1.2.2 h1:lxLXG0uE3Qnshl9QyaK6XJxMXlQZELvChBOCmQD0Loo=
github.com/valyala/fasttemplate v1.2.2/go.mod h1:KHLXt3tVN2HBp8eijSv/kGJopbvo7S+qRAEEKiv+SiQ=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/atomic v1.10.0 h1:9qC72Qh0+3MqyJbAn8YU5xVq1frD8bn3JtD2oXtafVQ=
go.uber.org/atomic v1.10.0/go.mod h1:LUxbIzbOniOlMKjJjyPfpl4v+PKK2cNJn91OQbhoJI0=
go.uber.org/goleak v1.1.11 h1:wy28qYRKZgnJTxGxvye5/wgWr1EKjmUDGYox5mGlRlI=
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
go.uber.org/multierr v1.8.0 h1:dg6GjLku4EH+249NNmoIciG9N/jURbDG+pFlTkhzIC8=
go.uber.org/multierr v1.8.0/go.mod h1:7EAYxJLBy9rStEaz58O2t4Uvip6FSURkq8/ppBp95ak=
go.uber.org/zap v1.23.0 h1:OjGQ5KQDEUawVHxNwQgPpiypGHOxo2mNZsOqTak4fFY=
go.uber.org/zap v1.23.0/go.mod h1:D+nX8jyLsMHMYrln8A0rJjFt/T/9/bGgIhAqxv5URuY=
golang.org/x/crypto v0.1.0 h1:MDRAIl0xIo9Io2xV565hzXHw3zVseKrJKodhohM5CjU=
golang.org/x/crypto v0.1.0/go.mod h1:RecgLatLF4+eUMCP1PoPZQb+cVrJcOPbHkTkbkB9sbw=
golang.org/x/net v0.1.0 h1:hZ/3BUoy5aId7sCpA/Tc5lt8DkFgdVS2onTpJsZ/fl0=
golang.org/x/net v0.1.0/go.mod h1:Cx3nUiGt4eDBEyega/BKRp+/AlGL8hYe7U9odMt2Cco=
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20210927094055-39ccf1dd6fa6/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20211103235746-7861aae1554b/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto=
go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE=
go.uber.org/multierr v1.11.0 h1:blXXJkSxSSfBVBlC76pxqeO+LN3aDfLQo+309xJstO0=
go.uber.org/multierr v1.11.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.27.0 h1:aJMhYGrd5QSmlpLMr2MftRKl7t8J8PTZPA732ud/XR8=
go.uber.org/zap v1.27.0/go.mod h1:GB2qFLM7cTU87MWRP2mPIjqfIDnGu+VIO4V/SdhGo2E=
golang.org/x/crypto v0.27.0 h1:GXm2NjJrPaiv/h1tb2UH8QfgC/hOf/+z0p6PT8o1w7A=
golang.org/x/crypto v0.27.0/go.mod h1:1Xngt8kV6Dvbssa53Ziq6Eqn0HqbZi5Z6R0ZpwQzt70=
golang.org/x/net v0.29.0 h1:5ORfpBpCs4HzDYoodCDBbwHzdR5UrLBZ3sOnUJmFoHo=
golang.org/x/net v0.29.0/go.mod h1:gLkgy8jTGERgjzMic6DS9+SP0ajcu6Xu3Orq/SpETg0=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.1.0 h1:kunALQeHf1/185U1i0GOB/fy1IPRDDpuoOOqRReG57U=
golang.org/x/sys v0.1.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/text v0.4.0 h1:BrVqGRd7+k1DiOgtnFvAkoQEWQvBc25ouMJM6429SFg=
golang.org/x/text v0.4.0/go.mod h1:mrYo+phRRbMaCq/xk9113O4dZlRixOauAjOtrjsXDZ8=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.25.0 h1:r+8e+loiHxRqhXVl6ML1nO3l1+oFoWbnlu2Ehimmi34=
golang.org/x/sys v0.25.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.18.0 h1:XvMDiNzPAl0jr17s6W9lcaIhGUfUORdGCNsuLmPG224=
golang.org/x/text v0.18.0/go.mod h1:BuEKDfySbSR4drPmRPG/7iBdf8hvFMuRexcpahXilzY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
6 changes: 6 additions & 0 deletions key/key.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const (
Email = "email"
CompanyKey = "company_key"
Latency = "latency"
ProcessingTime = "processing_time"
Method = "method"
URI = "uri"
Path = "path"
Expand All @@ -17,6 +18,10 @@ const (
CorrelationID = "correlation_id"
Tool = "tool"
ProductFeature = "product_feature"

HeaderCorrelationID = "correlation-id"
HeaderEmail = "email"
HeaderCompanyKey = "company-key"
)

type ctxKey int
Expand All @@ -27,4 +32,5 @@ const (
CtxCorrelationID
CtxTool
CtxProductFeature
CtxAPIVersion
)
6 changes: 4 additions & 2 deletions logger.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package logger

import (
"context"

"github.com/surfe/logger/logi"
"github.com/surfe/logger/simple"
"github.com/surfe/logger/zap"
Expand All @@ -10,6 +12,6 @@ var logger logi.Logger = &simple.Logger{}
var _ logi.Logger = &zap.Logger{}

// Log is the getter for global `logger` variable
func Log() logi.Logger {
return logger
func Log(ctx context.Context) logi.Logger {
return logger.Ctx(ctx)
}
5 changes: 4 additions & 1 deletion logi/logi.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,11 @@ import (

// Logger represents common interface for logging functionality
type Logger interface {
// Adds context to logger. Logger will extract key/value pairs from it.
Ctx(ctx context.Context) Logger

// With returns logger with basic fields based on context and your custom fields
With(ctx context.Context, keysAndValues ...any) Logger
With(keysAndValues ...any) Logger

// Err returns logger with the provided error
Err(err error) Logger
Expand Down
19 changes: 16 additions & 3 deletions reqctx/reqctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"github.com/surfe/logger/key"
)

func ContextMiddleware() echo.MiddlewareFunc {
func ContextMiddleware(apiVersion string) echo.MiddlewareFunc {
return func(next echo.HandlerFunc) echo.HandlerFunc {
return func(c echo.Context) error {
var email string
Expand All @@ -21,12 +21,25 @@ func ContextMiddleware() echo.MiddlewareFunc {
companyKey = claims["companyKey"].(string)
}

correlationID := random.String(32)
// Extract data from headers if no user.
if email == "" {
email = c.Request().Header.Get(key.HeaderEmail)
}
if companyKey == "" {
companyKey = c.Request().Header.Get(key.HeaderCompanyKey)
}

correlationID := c.Request().Header.Get(key.HeaderCorrelationID)
if correlationID == "" {
correlationID = random.String(32)
}

c.SetRequest(c.Request().WithContext(context.WithValue(c.Request().Context(), key.CtxEmail, email)))
c.SetRequest(c.Request().WithContext(context.WithValue(c.Request().Context(), key.CtxCompany, companyKey)))
c.SetRequest(c.Request().WithContext(context.WithValue(c.Request().Context(), key.CtxCorrelationID, correlationID)))
c.SetRequest(c.Request().WithContext(context.WithValue(c.Request().Context(), key.CtxAPIVersion, apiVersion)))

c.Response().Header().Set("x-correlation-id", correlationID)
c.Response().Header().Set(key.HeaderCorrelationID, correlationID)

return next(c)
}
Expand Down
8 changes: 6 additions & 2 deletions simple/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ type Logger struct {
toAppend string
}

func (w *Logger) With(ctx context.Context, args ...any) logi.Logger {
func (w *Logger) Ctx(ctx context.Context) logi.Logger {
return w
}

func (w *Logger) With(args ...any) logi.Logger {
var sb strings.Builder

if len(w.toAppend) > 0 {
Expand Down Expand Up @@ -48,7 +52,7 @@ func (w *Logger) With(ctx context.Context, args ...any) logi.Logger {
}

func (w *Logger) Err(err error) logi.Logger {
return w.With(context.TODO(), "error", err)
return w.With("error", err)
}

func (w *Logger) Errorf(template string, args ...any) {
Expand Down
3 changes: 1 addition & 2 deletions simple/wrapper_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package simple

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -37,7 +36,7 @@ func TestLogger_With(t *testing.T) {
w := &Logger{
toAppend: tt.toAppend,
}
got := w.With(context.TODO(), tt.args...)
got := w.With(tt.args...)
assert.Equal(t, tt.want, got)
})
}
Expand Down
41 changes: 18 additions & 23 deletions zap/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,36 +31,31 @@ func (w *Logger) EchoMiddleware() echo.MiddlewareFunc {
companyKey = claims["companyKey"].(string)
}

fields := []any{
"remote_ip", c.RealIP(),
key.External, false,
key.Email, email,
key.CompanyKey, companyKey,
key.Latency, time.Since(start).Milliseconds(),
key.Method, req.Method,
key.URI, req.RequestURI,
key.Path, req.URL.Path,
key.Status, res.Status,
key.UserAgent, req.UserAgent(),
key.APIVersion, req.Header.Get("API-Version"),
key.ExtensionVersion, req.Header.Get("Extension-Version"),
}
fields := []any{}
appendFilledFieldsOnly(&fields, "remote_ip", c.RealIP())
appendFilledFieldsOnly(&fields, key.External, false)
appendFilledFieldsOnly(&fields, key.Email, email)
appendFilledFieldsOnly(&fields, key.CompanyKey, companyKey)
appendFilledFieldsOnly(&fields, key.ProcessingTime, time.Since(start).Milliseconds())
appendFilledFieldsOnly(&fields, key.Method, req.Method)
appendFilledFieldsOnly(&fields, key.URI, req.RequestURI)
appendFilledFieldsOnly(&fields, key.Path, req.URL.Path)
appendFilledFieldsOnly(&fields, key.Status, res.Status)
appendFilledFieldsOnly(&fields, key.UserAgent, req.UserAgent())
appendFilledFieldsOnly(&fields, key.APIVersion, req.Header.Get("API-Version"))
appendFilledFieldsOnly(&fields, key.ExtensionVersion, req.Header.Get("Extension-Version"))

if correlationID, isOk := req.Context().Value(key.CtxCorrelationID).(string); isOk && correlationID != "" {
fields = append(fields, key.CorrelationID, correlationID)
}

log := w.With(c.Request().Context(), fields...)
log := w.Ctx(c.Request().Context()).With(fields...)
n := res.Status
switch {
case n >= 500:
// Status 5XX is logged as error as this should not happen in production.
if n >= 500 {
log.Err(err).Error("CRM Error")
case n >= 400:
log.Err(err).Warn("Client error")
case n >= 300:
log.Info("Redirection")
default:
log.Info("Success")
} else {
log.Info("Incoming request")
}

return nil
Expand Down
27 changes: 13 additions & 14 deletions zap/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,30 @@ type Logger struct {
log *zap.SugaredLogger
}

func (w *Logger) With(ctx context.Context, keysAndValues ...any) logi.Logger {
func (w *Logger) Ctx(ctx context.Context) logi.Logger {
fields := []any{}
addNotEmpty := func(key string, value any) {
val, isOk := value.(string)
if isOk && key != "" && val != "" {
fields = append(fields, key, value)
}
}

if ctx != nil {
// We do not want to add empty key-value pairs
addNotEmpty(key.Email, ctx.Value(key.CtxEmail))
addNotEmpty(key.CompanyKey, ctx.Value(key.CtxCompany))
addNotEmpty(key.CorrelationID, ctx.Value(key.CtxCorrelationID))
addNotEmpty(key.Tool, ctx.Value(key.CtxTool))
addNotEmpty(key.ProductFeature, ctx.Value(key.CtxProductFeature))
appendFilledFieldsOnly(&fields, key.Email, ctx.Value(key.CtxEmail))
appendFilledFieldsOnly(&fields, key.CompanyKey, ctx.Value(key.CtxCompany))
appendFilledFieldsOnly(&fields, key.CorrelationID, ctx.Value(key.CtxCorrelationID))
appendFilledFieldsOnly(&fields, key.Tool, ctx.Value(key.CtxTool))
appendFilledFieldsOnly(&fields, key.ProductFeature, ctx.Value(key.CtxProductFeature))
appendFilledFieldsOnly(&fields, key.ProductFeature, ctx.Value(key.CtxAPIVersion))
}

fields = append(fields, keysAndValues...)

return &Logger{
log: w.log.With(fields...),
}
}

func (w *Logger) With(keysAndValues ...any) logi.Logger {
return &Logger{
log: w.log.With(keysAndValues...),
}
}

func (w *Logger) Err(err error) logi.Logger {
return &Logger{
log: w.log.With(zap.Error(err)),
Expand Down
7 changes: 7 additions & 0 deletions zap/zap.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,10 @@ func Init() (*Logger, error) {

return &Logger{sugarLog}, nil
}

func appendFilledFieldsOnly(fields *[]any, key string, value any) {
val, isOk := value.(string)
if isOk && key != "" && val != "" {
*fields = append(*fields, key, value)
}
}

0 comments on commit 8b4ef09

Please sign in to comment.