Skip to content

Commit fbb8d6e

Browse files
committed
Refactor usage of nconf from netlify-commons
1 parent a4682af commit fbb8d6e

File tree

5 files changed

+131
-118
lines changed

5 files changed

+131
-118
lines changed

conf/bugsnag.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package conf
2+
3+
import (
4+
"github.com/bugsnag/bugsnag-go"
5+
logrus_bugsnag "github.com/shopify/logrus-bugsnag"
6+
"github.com/sirupsen/logrus"
7+
)
8+
9+
type BugSnagConfig struct {
10+
Environment string
11+
APIKey string `envconfig:"api_key"`
12+
}
13+
14+
func AddBugSnagHook(config *BugSnagConfig) error {
15+
if config == nil || config.APIKey == "" {
16+
return nil
17+
}
18+
19+
bugsnag.Configure(bugsnag.Configuration{
20+
APIKey: config.APIKey,
21+
ReleaseStage: config.Environment,
22+
PanicHandler: func() {}, // this is to disable panic handling. The lib was forking and restarting the process (causing races)
23+
})
24+
hook, err := logrus_bugsnag.NewBugsnagHook()
25+
if err != nil {
26+
return err
27+
}
28+
logrus.AddHook(hook)
29+
logrus.Debug("Added bugsnag hook")
30+
return nil
31+
}

conf/configuration.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55

66
"github.com/joho/godotenv"
77
"github.com/kelseyhightower/envconfig"
8-
"github.com/netlify/netlify-commons/nconf"
98
)
109

1110
// DBConfiguration holds all the database related configuration.
@@ -39,7 +38,7 @@ type GlobalConfiguration struct {
3938
Endpoint string
4039
}
4140
DB DBConfiguration
42-
Logging nconf.LoggingConfig `envconfig:"LOG"`
41+
Logging LoggingConfig `envconfig:"LOG"`
4342
OperatorToken string `split_words:"true"`
4443
MultiInstanceMode bool
4544
SMTP SMTPConfiguration `json:"smtp"`
@@ -128,7 +127,7 @@ func LoadGlobal(filename string) (*GlobalConfiguration, error) {
128127
if err := envconfig.Process("gocommerce", config); err != nil {
129128
return nil, err
130129
}
131-
if _, err := nconf.ConfigureLogging(&config.Logging); err != nil {
130+
if _, err := ConfigureLogging(&config.Logging); err != nil {
132131
return nil, err
133132
}
134133
return config, nil

conf/logging.go

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package conf
2+
3+
import (
4+
"os"
5+
"time"
6+
7+
"github.com/pkg/errors"
8+
"github.com/sirupsen/logrus"
9+
)
10+
11+
type LoggingConfig struct {
12+
Level string `mapstructure:"log_level" json:"log_level"`
13+
File string `mapstructure:"log_file" json:"log_file"`
14+
DisableColors bool `mapstructure:"disable_colors" split_words:"true" json:"disable_colors"`
15+
QuoteEmptyFields bool `mapstructure:"quote_empty_fields" split_words:"true" json:"quote_empty_fields"`
16+
TSFormat string `mapstructure:"ts_format" json:"ts_format"`
17+
Fields map[string]interface{} `mapstructure:"fields" json:"fields"`
18+
UseNewLogger bool `mapstructure:"use_new_logger",split_words:"true"`
19+
20+
BugSnag *BugSnagConfig
21+
}
22+
23+
func ConfigureLogging(config *LoggingConfig) (*logrus.Entry, error) {
24+
logger := logrus.New()
25+
26+
tsFormat := time.RFC3339Nano
27+
if config.TSFormat != "" {
28+
tsFormat = config.TSFormat
29+
}
30+
// always use the full timestamp
31+
logger.SetFormatter(&logrus.TextFormatter{
32+
FullTimestamp: true,
33+
DisableTimestamp: false,
34+
TimestampFormat: tsFormat,
35+
DisableColors: config.DisableColors,
36+
QuoteEmptyFields: config.QuoteEmptyFields,
37+
})
38+
39+
// use a file if you want
40+
if config.File != "" {
41+
f, errOpen := os.OpenFile(config.File, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0664)
42+
if errOpen != nil {
43+
return nil, errOpen
44+
}
45+
logger.SetOutput(f)
46+
logger.Infof("Set output file to %s", config.File)
47+
}
48+
49+
if config.Level != "" {
50+
level, err := logrus.ParseLevel(config.Level)
51+
if err != nil {
52+
return nil, err
53+
}
54+
logger.SetLevel(level)
55+
logger.Debug("Set log level to: " + logger.GetLevel().String())
56+
}
57+
58+
if err := AddBugSnagHook(config.BugSnag); err != nil {
59+
return nil, errors.Wrap(err, "Failed to configure bugsnag")
60+
}
61+
62+
f := logrus.Fields{}
63+
for k, v := range config.Fields {
64+
f[k] = v
65+
}
66+
67+
return logger.WithFields(f), nil
68+
}

go.mod

Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,81 +2,54 @@ module github.com/netlify/gocommerce
22

33
require (
44
cloud.google.com/go v0.0.0-20170822200954-98f5696b1026 // indirect
5-
github.com/BurntSushi/toml v0.3.1 // indirect
65
github.com/GoogleCloudPlatform/cloudsql-proxy v0.0.0-20170623214735-571947b0f240
76
github.com/PuerkitoBio/goquery v1.1.0
87
github.com/andybalholm/cascadia v0.0.0-20161224141413-349dd0209470 // indirect
9-
github.com/davecgh/go-spew v1.1.1 // indirect
8+
github.com/bitly/go-simplejson v0.5.0 // indirect
9+
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
10+
github.com/bugsnag/bugsnag-go v1.5.3
11+
github.com/bugsnag/panicwrap v1.2.0 // indirect
1012
github.com/denisenkom/go-mssqldb v0.0.0-20190906004059-62cf760a6c9e // indirect
1113
github.com/dgrijalva/jwt-go v3.0.0+incompatible
12-
github.com/dropbox/godropbox v0.0.0-20190501155911-5749d3b71cbe // indirect
1314
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 // indirect
14-
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 // indirect
15-
github.com/facebookgo/stackerr v0.0.0-20150612192056-c2fcf88613f4 // indirect
16-
github.com/fsnotify/fsnotify v0.0.0-20170329110642-4da3e2cfbabc // indirect
1715
github.com/go-chi/chi v3.1.0+incompatible
18-
github.com/go-kit/kit v0.9.0 // indirect
19-
github.com/go-logfmt/logfmt v0.4.0 // indirect
2016
github.com/go-sql-driver/mysql v1.3.0
21-
github.com/go-stack/stack v1.8.0 // indirect
17+
github.com/gofrs/uuid v3.2.0+incompatible // indirect
2218
github.com/golang/protobuf v0.0.0-20170816001514-ab9f9a6dab16 // indirect
23-
github.com/hashicorp/hcl v0.0.0-20170509225359-392dba7d905e // indirect
2419
github.com/inconshreveable/mousetrap v1.0.0 // indirect
2520
github.com/jinzhu/gorm v1.9.1
2621
github.com/jinzhu/inflection v0.0.0-20170102125226-1c35d901db3d // indirect
2722
github.com/jinzhu/now v1.0.1 // indirect
2823
github.com/joho/godotenv v0.0.0-20161216230537-726cc8b906e3
29-
github.com/juju/errors v0.0.0-20190806202954-0232dcc7464d // indirect
30-
github.com/juju/loggo v0.0.0-20190526231331-6e530bcce5d8 // indirect
31-
github.com/juju/testing v0.0.0-20190723135506-ce30eb24acd2 // indirect
24+
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 // indirect
3225
github.com/kelseyhightower/envconfig v1.3.0
3326
github.com/kr/pretty v0.1.0 // indirect
3427
github.com/lib/pq v0.0.0-20170306183709-ca5bc43047f2
3528
github.com/logpacker/PayPal-Go-SDK v2.0.5+incompatible // indirect
36-
github.com/magiconair/properties v1.7.3 // indirect
3729
github.com/mattes/vat v0.0.0-20160607175015-805d21ad0739
3830
github.com/mattn/go-sqlite3 v1.10.0
3931
github.com/mitchellh/mapstructure v0.0.0-20170523030023-d0303fe80992
40-
github.com/nats-io/gnatsd v1.4.1 // indirect
41-
github.com/nats-io/go-nats v1.7.2 // indirect
42-
github.com/nats-io/nats v1.2.2 // indirect
43-
github.com/nats-io/nkeys v0.1.0 // indirect
44-
github.com/nats-io/nuid v0.0.0-20170303150224-3cf34f9fca4e // indirect
4532
github.com/netlify/PayPal-Go-SDK v0.0.0-20180614154051-732c3d08bf8a
4633
github.com/netlify/mailme v0.0.0-20170821082834-c4a76ce443c1
47-
github.com/netlify/netlify-commons v0.4.5
4834
github.com/pariz/gountries v0.0.0-20171019111738-adb00f6513a3
4935
github.com/pborman/uuid v0.0.0-20160209185913-a97ce2ca70fa
50-
github.com/pelletier/go-toml v0.0.0-20170628012637-69d355db5304 // indirect
5136
github.com/pkg/errors v0.8.0
5237
github.com/plutov/paypal v2.0.5+incompatible // indirect
53-
github.com/pmezard/go-difflib v1.0.0 // indirect
5438
github.com/rs/cors v0.0.0-20170608165155-8dd4211afb5d
55-
github.com/rybit/nats_logrus_hook v1.0.4 // indirect
5639
github.com/sebest/xff v0.0.0-20160910043805-6c115e0ffa35
57-
github.com/signalfx/com_signalfx_metrics_protobuf v0.0.0-20170330202426-93e507b42f43 // indirect
58-
github.com/signalfx/gohistogram v0.0.0-20160107210732-1ccfd2ff5083 // indirect
59-
github.com/signalfx/golib v1.0.0 // indirect
60-
github.com/sirupsen/logrus v0.0.0-20170713114250-a3f95b5c4235
61-
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 // indirect
62-
github.com/spf13/afero v0.0.0-20170217164146-9be650865eab // indirect
63-
github.com/spf13/cast v1.1.0 // indirect
40+
github.com/shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d
41+
github.com/sirupsen/logrus v1.4.2
6442
github.com/spf13/cobra v0.0.0-20170228191748-fcd0c5a1df88
65-
github.com/spf13/jwalterweatherman v0.0.0-20170523133247-0efa5202c046 // indirect
6643
github.com/spf13/pflag v1.0.0 // indirect
67-
github.com/spf13/viper v0.0.0-20170217163817-7538d73b4eb9 // indirect
68-
github.com/streadway/amqp v0.0.0-20170707203015-2cbfe40c9341 // indirect
6944
github.com/stretchr/testify v1.2.2
7045
github.com/stripe/stripe-go v62.9.0+incompatible
46+
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 // indirect
7147
golang.org/x/oauth2 v0.0.0-20170807180024-9a379c6b3e95 // indirect
7248
golang.org/x/sync v0.0.0-20190423024810-112230192c58 // indirect
7349
google.golang.org/api v0.0.0-20170821230356-dd6bdadc5852 // indirect
7450
google.golang.org/appengine v0.0.0-20170814190942-d9a072cfa7b9 // indirect
7551
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
7652
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
7753
gopkg.in/gomail.v2 v2.0.0-20150902115704-41f357289737 // indirect
78-
gopkg.in/logfmt.v0 v0.3.0 // indirect
79-
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 // indirect
80-
gopkg.in/stack.v1 v1.6.0 // indirect
8154
gopkg.in/yaml.v2 v2.0.0-20170721122051-25c4ec802a7d // indirect
8255
)

0 commit comments

Comments
 (0)