Skip to content

Commit 932b369

Browse files
authored
Fix cache middleware and minor fix on logger (#69)
* Fix cache middleware and minor fix on logger * minor change * tests
1 parent 428c359 commit 932b369

File tree

6 files changed

+32
-7
lines changed

6 files changed

+32
-7
lines changed

pkg/logger/logger.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -50,27 +50,33 @@ func InitLogger(config Config) {
5050
baseLogger = configureAndBuildLogger(config)
5151
zap.ReplaceGlobals(baseLogger)
5252
}
53+
5354
func NewLogger(opts ...interface{}) *Logger {
5455
lock.Lock()
5556
defer lock.Unlock()
5657

57-
var config Config
58+
var config *Config
5859
var fields []Field
5960

6061
for _, opt := range opts {
6162
switch opt := opt.(type) {
6263
case Config:
63-
config = opt
64+
config = &opt
6465
case Field:
6566
fields = append(fields, opt)
6667
}
6768
}
6869

69-
if config == (Config{}) {
70-
config = DefaultConfig()
70+
logger := configureAndBuildLogger(defaultConfig)
71+
if baseLogger != nil {
72+
logger = baseLogger.WithOptions(zap.AddCallerSkip(1))
73+
}
74+
75+
if config != nil {
76+
logger = configureAndBuildLogger(*config)
7177
}
7278

73-
logger := configureAndBuildLogger(config).With(toZapFields(fields)...)
79+
logger = logger.With(toZapFields(fields)...)
7480
return &Logger{logger: logger}
7581
}
7682

pkg/logger/methods.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ func (l *Logger) WithFields(fields ...zap.Field) *Logger {
7070
return &Logger{logger: l.logger.With(fields...)}
7171
}
7272

73+
func (l *Logger) IsDebugEnabled() bool {
74+
return baseLogger.Core().Enabled(zap.DebugLevel)
75+
}
76+
7377
func GetLoggerFromContext(ctx context.Context) *Logger {
7478
logger, ok := ctx.Value(loggerKey).(*Logger)
7579
if !ok {

pkg/zrouter/zmiddlewares/cache.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,10 @@ func CacheMiddleware(cache zcache.ZCache, config domain.CacheConfig) func(next h
3030
rw := &responseWriter{ResponseWriter: w}
3131
next.ServeHTTP(rw, r) // Important: This line needs to be BEFORE setting the cache.
3232
cacheResponseIfNeeded(rw, r, cache, key, ttl)
33+
return
3334
}
35+
36+
next.ServeHTTP(w, r)
3437
})
3538
}
3639
}

pkg/zrouter/zmiddlewares/error_handler_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@ import (
44
"encoding/json"
55
"github.com/go-chi/chi/v5"
66
"github.com/stretchr/testify/assert"
7+
"github.com/zondax/golem/pkg/logger"
78
"github.com/zondax/golem/pkg/zrouter/domain"
89
"net/http"
910
"net/http/httptest"
1011
"testing"
1112
)
1213

1314
func TestErrorHandlerMiddleware(t *testing.T) {
15+
logger.InitLogger(logger.Config{})
1416
r := chi.NewRouter()
1517
r.Use(ErrorHandlerMiddleware())
1618

pkg/zrouter/zmiddlewares/logging.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,15 @@ func LoggingMiddleware(next http.Handler) http.Handler {
2121
duration := time.Since(start)
2222
ctx := r.Context()
2323

24-
logger.GetLoggerFromContext(ctx).Debugf("Method: %s - URL: %s | Status: %d - Duration: %s - Response Body: %s",
25-
r.Method, r.URL.String(), rw.status, duration, rw.Body())
24+
log := logger.GetLoggerFromContext(ctx)
25+
26+
if log.IsDebugEnabled() {
27+
log.Debugf("Method: %s - URL: %s | Status: %d - Duration: %s - Response Body: %s",
28+
r.Method, r.URL.String(), rw.status, duration, string(rw.Body()))
29+
return
30+
}
31+
32+
log.Infof("Method: %s - URL: %s | Status: %d - Duration: %s",
33+
r.Method, r.URL.String(), rw.status, duration)
2634
})
2735
}

pkg/zrouter/zrouter_test.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package zrouter
33
import (
44
"github.com/stretchr/testify/assert"
55
"github.com/stretchr/testify/suite"
6+
"github.com/zondax/golem/pkg/logger"
67
"github.com/zondax/golem/pkg/zrouter/domain"
78
"net/http"
89
"net/http/httptest"
@@ -16,6 +17,7 @@ type ZRouterSuite struct {
1617

1718
func (suite *ZRouterSuite) SetupTest() {
1819
suite.router = New("testApp", nil, nil)
20+
logger.InitLogger(logger.Config{})
1921
}
2022

2123
func (suite *ZRouterSuite) TestRegisterAndGetRoutes() {

0 commit comments

Comments
 (0)