Skip to content

Commit

Permalink
Fix cache middleware and minor fix on logger (#69)
Browse files Browse the repository at this point in the history
* Fix cache middleware and minor fix on logger

* minor change

* tests
  • Loading branch information
lucaslopezf authored Feb 23, 2024
1 parent 428c359 commit 932b369
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 7 deletions.
16 changes: 11 additions & 5 deletions pkg/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,27 +50,33 @@ func InitLogger(config Config) {
baseLogger = configureAndBuildLogger(config)
zap.ReplaceGlobals(baseLogger)
}

func NewLogger(opts ...interface{}) *Logger {
lock.Lock()
defer lock.Unlock()

var config Config
var config *Config
var fields []Field

for _, opt := range opts {
switch opt := opt.(type) {
case Config:
config = opt
config = &opt
case Field:
fields = append(fields, opt)
}
}

if config == (Config{}) {
config = DefaultConfig()
logger := configureAndBuildLogger(defaultConfig)
if baseLogger != nil {
logger = baseLogger.WithOptions(zap.AddCallerSkip(1))
}

if config != nil {
logger = configureAndBuildLogger(*config)
}

logger := configureAndBuildLogger(config).With(toZapFields(fields)...)
logger = logger.With(toZapFields(fields)...)
return &Logger{logger: logger}
}

Expand Down
4 changes: 4 additions & 0 deletions pkg/logger/methods.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ func (l *Logger) WithFields(fields ...zap.Field) *Logger {
return &Logger{logger: l.logger.With(fields...)}
}

func (l *Logger) IsDebugEnabled() bool {
return baseLogger.Core().Enabled(zap.DebugLevel)
}

func GetLoggerFromContext(ctx context.Context) *Logger {
logger, ok := ctx.Value(loggerKey).(*Logger)
if !ok {
Expand Down
3 changes: 3 additions & 0 deletions pkg/zrouter/zmiddlewares/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ func CacheMiddleware(cache zcache.ZCache, config domain.CacheConfig) func(next h
rw := &responseWriter{ResponseWriter: w}
next.ServeHTTP(rw, r) // Important: This line needs to be BEFORE setting the cache.
cacheResponseIfNeeded(rw, r, cache, key, ttl)
return
}

next.ServeHTTP(w, r)
})
}
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/zrouter/zmiddlewares/error_handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import (
"encoding/json"
"github.com/go-chi/chi/v5"
"github.com/stretchr/testify/assert"
"github.com/zondax/golem/pkg/logger"
"github.com/zondax/golem/pkg/zrouter/domain"
"net/http"
"net/http/httptest"
"testing"
)

func TestErrorHandlerMiddleware(t *testing.T) {
logger.InitLogger(logger.Config{})
r := chi.NewRouter()
r.Use(ErrorHandlerMiddleware())

Expand Down
12 changes: 10 additions & 2 deletions pkg/zrouter/zmiddlewares/logging.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,15 @@ func LoggingMiddleware(next http.Handler) http.Handler {
duration := time.Since(start)
ctx := r.Context()

logger.GetLoggerFromContext(ctx).Debugf("Method: %s - URL: %s | Status: %d - Duration: %s - Response Body: %s",
r.Method, r.URL.String(), rw.status, duration, rw.Body())
log := logger.GetLoggerFromContext(ctx)

if log.IsDebugEnabled() {
log.Debugf("Method: %s - URL: %s | Status: %d - Duration: %s - Response Body: %s",
r.Method, r.URL.String(), rw.status, duration, string(rw.Body()))
return
}

log.Infof("Method: %s - URL: %s | Status: %d - Duration: %s",
r.Method, r.URL.String(), rw.status, duration)
})
}
2 changes: 2 additions & 0 deletions pkg/zrouter/zrouter_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package zrouter
import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/suite"
"github.com/zondax/golem/pkg/logger"
"github.com/zondax/golem/pkg/zrouter/domain"
"net/http"
"net/http/httptest"
Expand All @@ -16,6 +17,7 @@ type ZRouterSuite struct {

func (suite *ZRouterSuite) SetupTest() {
suite.router = New("testApp", nil, nil)
logger.InitLogger(logger.Config{})
}

func (suite *ZRouterSuite) TestRegisterAndGetRoutes() {
Expand Down

0 comments on commit 932b369

Please sign in to comment.