Skip to content

Commit

Permalink
Merge pull request #22 from rog-golang-buddies/feature/logger
Browse files Browse the repository at this point in the history
Feature/logger
  • Loading branch information
ldmi3i committed Aug 13, 2022
2 parents 0d013a5 + ad43ae4 commit 3b80382
Show file tree
Hide file tree
Showing 16 changed files with 537 additions and 38 deletions.
3 changes: 3 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ require (
github.com/rabbitmq/amqp091-go v1.4.0
github.com/stretchr/testify v1.8.0
github.com/wagslane/go-rabbitmq v0.10.0
go.uber.org/zap v1.21.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
go.uber.org/atomic v1.7.0 // indirect
go.uber.org/multierr v1.6.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
15 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
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=
Expand All @@ -10,21 +12,31 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
github.com/pkg/errors v0.8.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/rabbitmq/amqp091-go v1.4.0 h1:T2G+J9W9OY4p64Di23J6yH7tOkMocgnESvYeBjuG9cY=
github.com/rabbitmq/amqp091-go v1.4.0/go.mod h1:JsV0ofX5f1nwOGafb8L5rBItt9GyhfQfcJj+oyz0dGg=
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/wagslane/go-rabbitmq v0.10.0 h1:y9Bw8Q/9gOvsHfjMOGQjCW3033aYTKabxDm8eyjUGjs=
github.com/wagslane/go-rabbitmq v0.10.0/go.mod h1:u6xM1V7OO4D0szUy/F6Bya/9r0lLae/2FXBijkAQmn0=
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
go.uber.org/goleak v1.1.12 h1:gZAh5/EyT/HQwlpkCy6wTpqfH9H8Lz8zbm3dZh+OyzA=
go.uber.org/goleak v1.1.12/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
go.uber.org/zap v1.21.0 h1:WefMeulhovoZ2sYXz7st6K0sLj7bBhpiFaud4r4zST8=
go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
Expand Down Expand Up @@ -54,6 +66,9 @@ golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8T
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
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=
31 changes: 18 additions & 13 deletions internal/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,50 +2,55 @@ package internal

import (
"context"
"fmt"
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/config"
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/logger"
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/queue"
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/queue/handler"
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/queue/publisher"
"log"
)

func Start() int {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

conf, err := config.ReadConfig() //read configuration from file & env
conf, err := config.ReadConfig() //read configuration from env
if err != nil {
log.Println("error while reading configuration")
fmt.Println("error while reading configuration")
return 1
}
log, err := logger.NewLogger(conf)
if err != nil {
fmt.Println("error creating logger: ", err)
return 1
}

//initialize publisher connection to the queue
//this library assumes using one publisher and one consumer per application
//https://github.com/wagslane/go-rabbitmq/issues/79
pub, err := publisher.NewPublisher(conf.Queue) //TODO pass logger here and add it to publisher options
pub, err := publisher.NewPublisher(conf.Queue, log)
if err != nil {
log.Println("error while starting publisher: ", err)
log.Error("error while starting publisher: ", err)
return 1
}
defer publisher.ClosePublisher(pub)
defer publisher.ClosePublisher(pub, log)
//initialize consumer connection to the queue
consumer, err := queue.NewConsumer(conf.Queue) //TODO pass logger here and add it to consumer options
consumer, err := queue.NewConsumer(conf.Queue, log)
if err != nil {
log.Println("error while connecting to the queue: ", err)
log.Error("error while connecting to the queue: ", err)
return 1
}
defer queue.CloseConsumer(consumer)
defer queue.CloseConsumer(consumer, log)

handl := handler.NewApiSpecDocHandler(pub, conf.Queue)
handl := handler.NewApiSpecDocHandler(pub, conf.Queue, log)
listener := queue.NewListener()
err = listener.Start(consumer, &conf.Queue, handl)
if err != nil {
log.Println("error while listening queue ", err)
log.Error("error while listening queue ", err)
return 1
}

<-ctx.Done()

log.Println("application stopped gracefully (not)")
log.Info("application stopped gracefully (not)")
return 0
}
2 changes: 2 additions & 0 deletions internal/config/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import (
)

type ApplicationConfig struct {
Env Environment `default:"dev"`
Logger LoggerConfig
Queue QueueConfig
}

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

//LoggerConfig represents configuration of the logger
type LoggerConfig struct {
Level LoggerLevel `default:"info"` //Level of minimum logging
}
40 changes: 40 additions & 0 deletions internal/config/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package config

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

//Environment defines the application environment to adjust settings to it.
type Environment string

const (
Dev Environment = "dev"
Prod Environment = "prod"
)

//LoggerLevel defines the minimum logging level to process
type LoggerLevel string

const (
TraceLevel LoggerLevel = "trace"
DebugLevel LoggerLevel = "debug"
InfoLevel LoggerLevel = "info"
WarnLevel LoggerLevel = "warn"
ErrorLevel LoggerLevel = "error"
PanicLevel LoggerLevel = "panic"
)

func (ll LoggerLevel) ToZapLevel() zapcore.Level {
switch ll {
case TraceLevel, DebugLevel:
return zapcore.DebugLevel
case InfoLevel:
return zapcore.InfoLevel
case WarnLevel:
return zapcore.WarnLevel
case ErrorLevel:
return zapcore.ErrorLevel
case PanicLevel:
return zapcore.PanicLevel
default:
return zapcore.InfoLevel
}
}
31 changes: 31 additions & 0 deletions internal/logger/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package logger

import (
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/config"
)

//Logger represents common logger interface
//go:generate mockgen -source=logger.go -destination=./mocks/logger.go
type Logger interface {
Fatal(args ...interface{})
Fatalf(format string, args ...interface{})

Error(args ...interface{})
Errorf(format string, args ...interface{})

Warn(args ...interface{})
Warnf(format string, args ...interface{})

Info(args ...interface{})
Infof(format string, args ...interface{})

Debug(args ...interface{})
Debugf(format string, args ...interface{})

Trace(args ...interface{})
Tracef(format string, args ...interface{})
}

func NewLogger(conf *config.ApplicationConfig) (Logger, error) {
return newZapLogger(conf)
}
Loading

0 comments on commit 3b80382

Please sign in to comment.