Skip to content

Commit

Permalink
Merge pull request #34 from inaciogu/feature/add-logger
Browse files Browse the repository at this point in the history
feature/add-default-logger
  • Loading branch information
inaciogu committed Jan 23, 2024
2 parents 0c15089 + a18858a commit 4dc212b
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 4 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ This is a simple package to help you consume messages from AWS SQS.
- [x] Consume messages from different defined queues
- [x] Consume messages from different queues by a prefix
- [x] Error handling
- [x] Message unmarshalling
- [x] Message deletion
- [x] Logging


### Installation
Expand Down
37 changes: 37 additions & 0 deletions consumer/logger/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package logger

import (
"fmt"

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

type DefaultLogger struct {
*zap.Logger
}

func New() *DefaultLogger {
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,
},
}

zapLogger, _ := logger.Build()

return &DefaultLogger{
Logger: zapLogger,
}
}

func (l *DefaultLogger) Log(message string, v ...interface{}) {
l.Info(fmt.Sprintf(message, v...))
}
30 changes: 30 additions & 0 deletions consumer/logger/logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package logger_test

import (
"testing"

"github.com/inaciogu/go-sqs/consumer/logger"
"github.com/stretchr/testify/suite"
)

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)
}
19 changes: 15 additions & 4 deletions consumer/sqsclient.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package sqsclient

import (
"fmt"
"strings"

"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/credentials"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/sqs"
"github.com/inaciogu/go-sqs/consumer/logger"
"github.com/inaciogu/go-sqs/consumer/message"
)

Expand All @@ -19,6 +19,10 @@ type SQSService interface {
ListQueues(input *sqs.ListQueuesInput) (*sqs.ListQueuesOutput, error)
}

type Logger interface {
Log(message string, v ...interface{})
}

type SQSClientInterface interface {
GetQueueUrl() *string
ReceiveMessages(queueUrl string, ch chan *sqs.Message) error
Expand All @@ -45,6 +49,7 @@ type SQSClientOptions struct {
type SQSClient struct {
Client SQSService
ClientOptions *SQSClientOptions
Logger Logger
}

const (
Expand Down Expand Up @@ -72,9 +77,11 @@ func New(sqsService SQSService, options SQSClientOptions) *SQSClient {

setDefaultOptions(&options)

logger := logger.New()
return &SQSClient{
Client: sqsService,
ClientOptions: &options,
Logger: logger,
}
}

Expand All @@ -96,6 +103,10 @@ func setDefaultOptions(options *SQSClientOptions) {
}
}

func (s *SQSClient) SetLogger(logger Logger) {
s.Logger = logger
}

// GetQueueUrl returns the URL of the queue based on the queue name
func (s *SQSClient) GetQueueUrl() *string {
urlResult, err := s.Client.GetQueueUrl(&sqs.GetQueueUrlInput{
Expand Down Expand Up @@ -131,7 +142,7 @@ func (s *SQSClient) ReceiveMessages(queueUrl string, ch chan *sqs.Message) error
queueName := splittedUrl[len(splittedUrl)-1]

for {
fmt.Printf("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 Expand Up @@ -167,7 +178,7 @@ func (s *SQSClient) ProcessMessage(sqsMessage *sqs.Message, queueUrl string) {
panic(err)
}

fmt.Printf("failed to handle message with ID: %s\n", message.Metadata.MessageId)
s.Logger.Log("failed to handle message with ID: %s", message.Metadata.MessageId)

return
}
Expand All @@ -181,7 +192,7 @@ func (s *SQSClient) ProcessMessage(sqsMessage *sqs.Message, queueUrl string) {
panic(err)
}

fmt.Printf("message handled ID: %s\n", message.Metadata.MessageId)
s.Logger.Log("message handled ID: %s", message.Metadata.MessageId)
}

// Poll starts polling messages from the queue
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 4dc212b

Please sign in to comment.