Skip to content

Commit

Permalink
Initial work on logger
Browse files Browse the repository at this point in the history
  • Loading branch information
mpchadwick committed Nov 4, 2020
1 parent 3e9ba2e commit e4d0862
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 4 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ require (
github.com/blastrain/vitess-sqlparser v0.0.0-20200914074247-af18b79da035
github.com/rhysd/go-github-selfupdate v1.2.2
github.com/shuLhan/go-bindata v4.0.0+incompatible // indirect
github.com/sirupsen/logrus v1.7.0
gopkg.in/yaml.v2 v2.3.0
syreclabs.com/go/faker v1.2.2
)
7 changes: 7 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ github.com/blang/semver v3.5.1+incompatible h1:cQNTCjp13qL8KC3Nbxr/y2Bqb63oX6wdn
github.com/blang/semver v3.5.1+incompatible/go.mod h1:kRBLl5iJ+tD4TcOOxsy/0fnwebNt5EWlYSAyrTnjyyk=
github.com/blastrain/vitess-sqlparser v0.0.0-20200914074247-af18b79da035 h1:DV6H5VrvYhI77Y3rH1L0YHL9XxDgZOH8Yw30kskIZnI=
github.com/blastrain/vitess-sqlparser v0.0.0-20200914074247-af18b79da035/go.mod h1:FGQp+RNQwVmLzDq6HBrYCww9qJQyNwH9Qji/quTQII4=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs=
Expand All @@ -23,10 +24,14 @@ github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+WWjE=
github.com/onsi/gomega v1.4.2/go.mod h1:ex+gbHU/CVuBBDIJjb2X0qEXbFg53c61hWP/1CpauHY=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rhysd/go-github-selfupdate v1.2.2 h1:G+mNzkc1wEtpmM6sFS/Ghkeq+ad4Yp6EZEHyp//wGEo=
github.com/rhysd/go-github-selfupdate v1.2.2/go.mod h1:khesvSyKcXDUxeySCedFh621iawCks0dS/QnHPcpCws=
github.com/shuLhan/go-bindata v4.0.0+incompatible h1:xD8LkuVZLV5OOn/IEuFdt6EEAW7deWiqgwaaSGhjAJc=
github.com/shuLhan/go-bindata v4.0.0+incompatible/go.mod h1:pkcPAATLBDD2+SpAPnX5vEM90F7fcwHCvvLCMXcmw3g=
github.com/sirupsen/logrus v1.7.0 h1:ShrD1U9pZB12TX0cVy0DtePoCH97K8EtX+mg7ZARUtM=
github.com/sirupsen/logrus v1.7.0/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/tcnksm/go-gitconfig v0.1.2 h1:iiDhRitByXAEyjgBqsKi9QU4o2TNtv9kPP3RgPgXBPw=
github.com/tcnksm/go-gitconfig v0.1.2/go.mod h1:/8EhP4H7oJZdIPyT+/UIsG87kTzrzM4UsLGSItWYCpE=
github.com/ulikunitz/xz v0.5.5 h1:pFrO0lVpTBXLpYw+pnLj6TbvHuyjXMfjGeCwSqCVwok=
Expand All @@ -43,6 +48,8 @@ golang.org/x/oauth2 v0.0.0-20181106182150-f42d05182288/go.mod h1:N/0e6XlmueqKjAG
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.0.0-20180302201248-b7ef84aaf62a/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
Expand Down
4 changes: 4 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ func main() {
os.Exit(0)
}

dbanonLogger := dbanon.GetLogger()
file, _ := os.OpenFile("dbanon.log", os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
dbanonLogger.Out = file

config, err := dbanon.NewConfig(*requested)
if err != nil {
fmt.Fprintln(os.Stderr, err)
Expand Down
19 changes: 19 additions & 0 deletions src/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package dbanon

import (
"github.com/sirupsen/logrus"
)

var logger *logrus.Logger

func init() {
logger = logrus.New()
}

func SetLogger(l *logrus.Logger) {
logger = l
}

func GetLogger() *logrus.Logger {
return logger
}
3 changes: 2 additions & 1 deletion src/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,12 @@ func (p Provider) Get(s string) string {
}

func (p Provider) raw (s string) string {
logger := GetLogger()
parts := strings.Split(s, ".")

className, ok := rawProviders[parts[1]]
if !ok {
// TODO: Cover this with logging
logger.Error(parts[1] + " is not a supported")
return ""
}

Expand Down
10 changes: 7 additions & 3 deletions src/provider_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
package dbanon

import (
"github.com/sirupsen/logrus/hooks/test"
"testing"
)

func TestGet(t *testing.T) {
testLogger, hook := test.NewNullLogger()
SetLogger(testLogger)

fakeEmail = func() string {
return "bob@example.com"
}
Expand Down Expand Up @@ -32,9 +36,9 @@ func TestGet(t *testing.T) {
t.Errorf("Got empty string, expecting number between 1 and 550")
}

r5 := provider.Get("faker.Whoops1")
if r5 != "" {
t.Errorf("Got a value and was expecting empty string")
_ = provider.Get("faker.Whoops1")
if hook.LastEntry().Message != "Whoops1 is not a supported" {
t.Errorf("Unsupported provider not handled correctly")
}

r6 := provider.Get("faker.Number().Whoops2()")
Expand Down

0 comments on commit e4d0862

Please sign in to comment.