Skip to content

Commit 3b80382

Browse files
authored
Merge pull request #22 from rog-golang-buddies/feature/logger
Feature/logger
2 parents 0d013a5 + ad43ae4 commit 3b80382

16 files changed

+537
-38
lines changed

go.mod

+3
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ require (
88
github.com/rabbitmq/amqp091-go v1.4.0
99
github.com/stretchr/testify v1.8.0
1010
github.com/wagslane/go-rabbitmq v0.10.0
11+
go.uber.org/zap v1.21.0
1112
)
1213

1314
require (
1415
github.com/davecgh/go-spew v1.1.1 // indirect
1516
github.com/pmezard/go-difflib v1.0.0 // indirect
17+
go.uber.org/atomic v1.7.0 // indirect
18+
go.uber.org/multierr v1.6.0 // indirect
1619
gopkg.in/yaml.v3 v3.0.1 // indirect
1720
)

go.sum

+15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
2+
github.com/benbjohnson/clock v1.1.0/go.mod h1:J11/hYXuz8f4ySSvYwY0FKfm+ezbsZBKZxNJlLklBHA=
13
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
24
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
35
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -10,21 +12,31 @@ github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORN
1012
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
1113
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
1214
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
15+
github.com/pkg/errors v0.8.1 h1:iURUrRGxPUNPdy5/HRSm+Yj6okJ6UtLINN0Q9M4+h3I=
16+
github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=
1317
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
1418
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1519
github.com/rabbitmq/amqp091-go v1.4.0 h1:T2G+J9W9OY4p64Di23J6yH7tOkMocgnESvYeBjuG9cY=
1620
github.com/rabbitmq/amqp091-go v1.4.0/go.mod h1:JsV0ofX5f1nwOGafb8L5rBItt9GyhfQfcJj+oyz0dGg=
1721
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
1822
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
23+
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=
1924
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
2025
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
2126
github.com/stretchr/testify v1.8.0 h1:pSgiaMZlXftHpm5L7V1+rVB+AZJydKsMxsQBIJw4PKk=
2227
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
2328
github.com/wagslane/go-rabbitmq v0.10.0 h1:y9Bw8Q/9gOvsHfjMOGQjCW3033aYTKabxDm8eyjUGjs=
2429
github.com/wagslane/go-rabbitmq v0.10.0/go.mod h1:u6xM1V7OO4D0szUy/F6Bya/9r0lLae/2FXBijkAQmn0=
2530
github.com/yuin/goldmark v1.3.5/go.mod h1:mwnBkeHKe2W/ZEtQ+71ViKU8L12m81fl3OWwC1Zlc8k=
31+
go.uber.org/atomic v1.7.0 h1:ADUqmZGgLDDfbSL9ZmPxKTybcoEYHgpYfELNoN+7hsw=
32+
go.uber.org/atomic v1.7.0/go.mod h1:fEN4uk6kAWBTFdckzkM89CLk9XfWZrxpCo0nPH17wJc=
33+
go.uber.org/goleak v1.1.11/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
2634
go.uber.org/goleak v1.1.12 h1:gZAh5/EyT/HQwlpkCy6wTpqfH9H8Lz8zbm3dZh+OyzA=
2735
go.uber.org/goleak v1.1.12/go.mod h1:cwTWslyiVhfpKIDGSZEM2HlOvcqm+tG4zioyIeLoqMQ=
36+
go.uber.org/multierr v1.6.0 h1:y6IPFStTAIT5Ytl7/XYmHvzXQ7S3g/IeZW9hyZ5thw4=
37+
go.uber.org/multierr v1.6.0/go.mod h1:cdWPpRnG4AhwMwsgIHip0KRBQjJy5kYEpYjJxpXp9iU=
38+
go.uber.org/zap v1.21.0 h1:WefMeulhovoZ2sYXz7st6K0sLj7bBhpiFaud4r4zST8=
39+
go.uber.org/zap v1.21.0/go.mod h1:wjWOCqI0f2ZZrJF/UufIOkiC8ii6tm1iqIsLo76RfJw=
2840
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
2941
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
3042
golang.org/x/lint v0.0.0-20190930215403-16217165b5de/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
@@ -54,6 +66,9 @@ golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8T
5466
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
5567
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 h1:qIbj1fsPNlZgppZ+VLlY7N33q108Sa+fhmuc+sWQYwY=
5668
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
69+
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
70+
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
5771
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
72+
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
5873
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
5974
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

internal/app.go

+18-13
Original file line numberDiff line numberDiff line change
@@ -2,50 +2,55 @@ package internal
22

33
import (
44
"context"
5+
"fmt"
56
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/config"
7+
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/logger"
68
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/queue"
79
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/queue/handler"
810
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/queue/publisher"
9-
"log"
1011
)
1112

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

16-
conf, err := config.ReadConfig() //read configuration from file & env
17+
conf, err := config.ReadConfig() //read configuration from env
1718
if err != nil {
18-
log.Println("error while reading configuration")
19+
fmt.Println("error while reading configuration")
20+
return 1
21+
}
22+
log, err := logger.NewLogger(conf)
23+
if err != nil {
24+
fmt.Println("error creating logger: ", err)
1925
return 1
2026
}
21-
2227
//initialize publisher connection to the queue
2328
//this library assumes using one publisher and one consumer per application
2429
//https://github.com/wagslane/go-rabbitmq/issues/79
25-
pub, err := publisher.NewPublisher(conf.Queue) //TODO pass logger here and add it to publisher options
30+
pub, err := publisher.NewPublisher(conf.Queue, log)
2631
if err != nil {
27-
log.Println("error while starting publisher: ", err)
32+
log.Error("error while starting publisher: ", err)
2833
return 1
2934
}
30-
defer publisher.ClosePublisher(pub)
35+
defer publisher.ClosePublisher(pub, log)
3136
//initialize consumer connection to the queue
32-
consumer, err := queue.NewConsumer(conf.Queue) //TODO pass logger here and add it to consumer options
37+
consumer, err := queue.NewConsumer(conf.Queue, log)
3338
if err != nil {
34-
log.Println("error while connecting to the queue: ", err)
39+
log.Error("error while connecting to the queue: ", err)
3540
return 1
3641
}
37-
defer queue.CloseConsumer(consumer)
42+
defer queue.CloseConsumer(consumer, log)
3843

39-
handl := handler.NewApiSpecDocHandler(pub, conf.Queue)
44+
handl := handler.NewApiSpecDocHandler(pub, conf.Queue, log)
4045
listener := queue.NewListener()
4146
err = listener.Start(consumer, &conf.Queue, handl)
4247
if err != nil {
43-
log.Println("error while listening queue ", err)
48+
log.Error("error while listening queue ", err)
4449
return 1
4550
}
4651

4752
<-ctx.Done()
4853

49-
log.Println("application stopped gracefully (not)")
54+
log.Info("application stopped gracefully (not)")
5055
return 0
5156
}

internal/config/application.go

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66
)
77

88
type ApplicationConfig struct {
9+
Env Environment `default:"dev"`
10+
Logger LoggerConfig
911
Queue QueueConfig
1012
}
1113

internal/config/logger.go

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package config
2+
3+
//LoggerConfig represents configuration of the logger
4+
type LoggerConfig struct {
5+
Level LoggerLevel `default:"info"` //Level of minimum logging
6+
}

internal/config/types.go

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package config
2+
3+
import "go.uber.org/zap/zapcore"
4+
5+
//Environment defines the application environment to adjust settings to it.
6+
type Environment string
7+
8+
const (
9+
Dev Environment = "dev"
10+
Prod Environment = "prod"
11+
)
12+
13+
//LoggerLevel defines the minimum logging level to process
14+
type LoggerLevel string
15+
16+
const (
17+
TraceLevel LoggerLevel = "trace"
18+
DebugLevel LoggerLevel = "debug"
19+
InfoLevel LoggerLevel = "info"
20+
WarnLevel LoggerLevel = "warn"
21+
ErrorLevel LoggerLevel = "error"
22+
PanicLevel LoggerLevel = "panic"
23+
)
24+
25+
func (ll LoggerLevel) ToZapLevel() zapcore.Level {
26+
switch ll {
27+
case TraceLevel, DebugLevel:
28+
return zapcore.DebugLevel
29+
case InfoLevel:
30+
return zapcore.InfoLevel
31+
case WarnLevel:
32+
return zapcore.WarnLevel
33+
case ErrorLevel:
34+
return zapcore.ErrorLevel
35+
case PanicLevel:
36+
return zapcore.PanicLevel
37+
default:
38+
return zapcore.InfoLevel
39+
}
40+
}

internal/logger/logger.go

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package logger
2+
3+
import (
4+
"github.com/rog-golang-buddies/api-hub_data-scraping-service/internal/config"
5+
)
6+
7+
//Logger represents common logger interface
8+
//go:generate mockgen -source=logger.go -destination=./mocks/logger.go
9+
type Logger interface {
10+
Fatal(args ...interface{})
11+
Fatalf(format string, args ...interface{})
12+
13+
Error(args ...interface{})
14+
Errorf(format string, args ...interface{})
15+
16+
Warn(args ...interface{})
17+
Warnf(format string, args ...interface{})
18+
19+
Info(args ...interface{})
20+
Infof(format string, args ...interface{})
21+
22+
Debug(args ...interface{})
23+
Debugf(format string, args ...interface{})
24+
25+
Trace(args ...interface{})
26+
Tracef(format string, args ...interface{})
27+
}
28+
29+
func NewLogger(conf *config.ApplicationConfig) (Logger, error) {
30+
return newZapLogger(conf)
31+
}

0 commit comments

Comments
 (0)