Skip to content

Commit

Permalink
feat: add zap logger implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
inaciogu committed Jan 23, 2024
1 parent 1672a13 commit 6244220
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 9 deletions.
24 changes: 18 additions & 6 deletions consumer/logger/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,33 @@ package logger

import (
"fmt"
"log/slog"
"os"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

type DefaultLogger struct {
*slog.Logger
*zap.Logger
}

func New() *DefaultLogger {
logger := slog.New(slog.NewJSONHandler(os.Stdout, nil))
logger := zap.Config{
Level: zap.NewAtomicLevelAt(zapcore.InfoLevel),
OutputPaths: []string{"stdout"},
Encoding: "json",
EncoderConfig: zapcore.EncoderConfig{
MessageKey: "message",
TimeKey: "time",
LevelKey: "level",
EncodeTime: zapcore.ISO8601TimeEncoder,
EncodeLevel: zapcore.LowercaseLevelEncoder,
},
}

slog.SetDefault(logger)
zapLogger, _ := logger.Build()

return &DefaultLogger{
Logger: logger,
Logger: zapLogger,
}
}

Expand Down
14 changes: 14 additions & 0 deletions consumer/logger/logger_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package logger_test

import (
"testing"

"github.com/inaciogu/go-sqs/consumer/logger"
"github.com/stretchr/testify/suite"
)
Expand All @@ -9,8 +11,20 @@ type UnitTestSuite struct {
suite.Suite
}

func TestUnitSuites(t *testing.T) {
suite.Run(t, new(UnitTestSuite))
}

func (ut *UnitTestSuite) TestNew() {
logger := logger.New()

ut.NotNil(logger)
}

func (ut *UnitTestSuite) TestLog() {
logger := logger.New()

logger.Log("test")

ut.NotNil(logger)
}
4 changes: 1 addition & 3 deletions consumer/sqsclient.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,10 +137,8 @@ func (s *SQSClient) ReceiveMessages(queueUrl string, ch chan *sqs.Message) error

queueName := splittedUrl[len(splittedUrl)-1]

s.Logger.Log("polling messages from queue %s", queueName)

for {
s.Logger.Log("polling messages from queue %s\n", queueName)
s.Logger.Log("polling messages from queue %s", queueName)

result, err := s.Client.ReceiveMessage(&sqs.ReceiveMessageInput{
QueueUrl: aws.String(queueUrl),
Expand Down
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ require (
github.com/jmespath/go-jmespath v0.4.0 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/stretchr/objx v0.5.0 // indirect
go.uber.org/multierr v1.10.0 // indirect
go.uber.org/zap v1.26.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
Expand Down

0 comments on commit 6244220

Please sign in to comment.