Skip to content

Commit

Permalink
Refactor usage of nconf from netlify-commons
Browse files Browse the repository at this point in the history
  • Loading branch information
mraerino committed Sep 6, 2019
1 parent a4682af commit fbb8d6e
Show file tree
Hide file tree
Showing 5 changed files with 131 additions and 118 deletions.
31 changes: 31 additions & 0 deletions conf/bugsnag.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package conf

import (
"github.com/bugsnag/bugsnag-go"
logrus_bugsnag "github.com/shopify/logrus-bugsnag"
"github.com/sirupsen/logrus"
)

type BugSnagConfig struct {
Environment string
APIKey string `envconfig:"api_key"`
}

func AddBugSnagHook(config *BugSnagConfig) error {
if config == nil || config.APIKey == "" {
return nil
}

bugsnag.Configure(bugsnag.Configuration{
APIKey: config.APIKey,
ReleaseStage: config.Environment,
PanicHandler: func() {}, // this is to disable panic handling. The lib was forking and restarting the process (causing races)
})
hook, err := logrus_bugsnag.NewBugsnagHook()
if err != nil {
return err
}
logrus.AddHook(hook)
logrus.Debug("Added bugsnag hook")
return nil
}
5 changes: 2 additions & 3 deletions conf/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (

"github.com/joho/godotenv"
"github.com/kelseyhightower/envconfig"
"github.com/netlify/netlify-commons/nconf"
)

// DBConfiguration holds all the database related configuration.
Expand Down Expand Up @@ -39,7 +38,7 @@ type GlobalConfiguration struct {
Endpoint string
}
DB DBConfiguration
Logging nconf.LoggingConfig `envconfig:"LOG"`
Logging LoggingConfig `envconfig:"LOG"`
OperatorToken string `split_words:"true"`
MultiInstanceMode bool
SMTP SMTPConfiguration `json:"smtp"`
Expand Down Expand Up @@ -128,7 +127,7 @@ func LoadGlobal(filename string) (*GlobalConfiguration, error) {
if err := envconfig.Process("gocommerce", config); err != nil {
return nil, err
}
if _, err := nconf.ConfigureLogging(&config.Logging); err != nil {
if _, err := ConfigureLogging(&config.Logging); err != nil {
return nil, err
}
return config, nil
Expand Down
68 changes: 68 additions & 0 deletions conf/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package conf

import (
"os"
"time"

"github.com/pkg/errors"
"github.com/sirupsen/logrus"
)

type LoggingConfig struct {
Level string `mapstructure:"log_level" json:"log_level"`
File string `mapstructure:"log_file" json:"log_file"`
DisableColors bool `mapstructure:"disable_colors" split_words:"true" json:"disable_colors"`
QuoteEmptyFields bool `mapstructure:"quote_empty_fields" split_words:"true" json:"quote_empty_fields"`
TSFormat string `mapstructure:"ts_format" json:"ts_format"`
Fields map[string]interface{} `mapstructure:"fields" json:"fields"`
UseNewLogger bool `mapstructure:"use_new_logger",split_words:"true"`

BugSnag *BugSnagConfig
}

func ConfigureLogging(config *LoggingConfig) (*logrus.Entry, error) {
logger := logrus.New()

tsFormat := time.RFC3339Nano
if config.TSFormat != "" {
tsFormat = config.TSFormat
}
// always use the full timestamp
logger.SetFormatter(&logrus.TextFormatter{
FullTimestamp: true,
DisableTimestamp: false,
TimestampFormat: tsFormat,
DisableColors: config.DisableColors,
QuoteEmptyFields: config.QuoteEmptyFields,
})

// use a file if you want
if config.File != "" {
f, errOpen := os.OpenFile(config.File, os.O_RDWR|os.O_APPEND|os.O_CREATE, 0664)
if errOpen != nil {
return nil, errOpen
}
logger.SetOutput(f)
logger.Infof("Set output file to %s", config.File)
}

if config.Level != "" {
level, err := logrus.ParseLevel(config.Level)
if err != nil {
return nil, err
}
logger.SetLevel(level)
logger.Debug("Set log level to: " + logger.GetLevel().String())
}

if err := AddBugSnagHook(config.BugSnag); err != nil {
return nil, errors.Wrap(err, "Failed to configure bugsnag")
}

f := logrus.Fields{}
for k, v := range config.Fields {
f[k] = v
}

return logger.WithFields(f), nil
}
45 changes: 9 additions & 36 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -2,81 +2,54 @@ module github.com/netlify/gocommerce

require (
cloud.google.com/go v0.0.0-20170822200954-98f5696b1026 // indirect
github.com/BurntSushi/toml v0.3.1 // indirect
github.com/GoogleCloudPlatform/cloudsql-proxy v0.0.0-20170623214735-571947b0f240
github.com/PuerkitoBio/goquery v1.1.0
github.com/andybalholm/cascadia v0.0.0-20161224141413-349dd0209470 // indirect
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/bitly/go-simplejson v0.5.0 // indirect
github.com/bmizerany/assert v0.0.0-20160611221934-b7ed37b82869 // indirect
github.com/bugsnag/bugsnag-go v1.5.3
github.com/bugsnag/panicwrap v1.2.0 // indirect
github.com/denisenkom/go-mssqldb v0.0.0-20190906004059-62cf760a6c9e // indirect
github.com/dgrijalva/jwt-go v3.0.0+incompatible
github.com/dropbox/godropbox v0.0.0-20190501155911-5749d3b71cbe // indirect
github.com/erikstmartin/go-testdb v0.0.0-20160219214506-8d10e4a1bae5 // indirect
github.com/facebookgo/stack v0.0.0-20160209184415-751773369052 // indirect
github.com/facebookgo/stackerr v0.0.0-20150612192056-c2fcf88613f4 // indirect
github.com/fsnotify/fsnotify v0.0.0-20170329110642-4da3e2cfbabc // indirect
github.com/go-chi/chi v3.1.0+incompatible
github.com/go-kit/kit v0.9.0 // indirect
github.com/go-logfmt/logfmt v0.4.0 // indirect
github.com/go-sql-driver/mysql v1.3.0
github.com/go-stack/stack v1.8.0 // indirect
github.com/gofrs/uuid v3.2.0+incompatible // indirect
github.com/golang/protobuf v0.0.0-20170816001514-ab9f9a6dab16 // indirect
github.com/hashicorp/hcl v0.0.0-20170509225359-392dba7d905e // indirect
github.com/inconshreveable/mousetrap v1.0.0 // indirect
github.com/jinzhu/gorm v1.9.1
github.com/jinzhu/inflection v0.0.0-20170102125226-1c35d901db3d // indirect
github.com/jinzhu/now v1.0.1 // indirect
github.com/joho/godotenv v0.0.0-20161216230537-726cc8b906e3
github.com/juju/errors v0.0.0-20190806202954-0232dcc7464d // indirect
github.com/juju/loggo v0.0.0-20190526231331-6e530bcce5d8 // indirect
github.com/juju/testing v0.0.0-20190723135506-ce30eb24acd2 // indirect
github.com/kardianos/osext v0.0.0-20190222173326-2bc1f35cddc0 // indirect
github.com/kelseyhightower/envconfig v1.3.0
github.com/kr/pretty v0.1.0 // indirect
github.com/lib/pq v0.0.0-20170306183709-ca5bc43047f2
github.com/logpacker/PayPal-Go-SDK v2.0.5+incompatible // indirect
github.com/magiconair/properties v1.7.3 // indirect
github.com/mattes/vat v0.0.0-20160607175015-805d21ad0739
github.com/mattn/go-sqlite3 v1.10.0
github.com/mitchellh/mapstructure v0.0.0-20170523030023-d0303fe80992
github.com/nats-io/gnatsd v1.4.1 // indirect
github.com/nats-io/go-nats v1.7.2 // indirect
github.com/nats-io/nats v1.2.2 // indirect
github.com/nats-io/nkeys v0.1.0 // indirect
github.com/nats-io/nuid v0.0.0-20170303150224-3cf34f9fca4e // indirect
github.com/netlify/PayPal-Go-SDK v0.0.0-20180614154051-732c3d08bf8a
github.com/netlify/mailme v0.0.0-20170821082834-c4a76ce443c1
github.com/netlify/netlify-commons v0.4.5
github.com/pariz/gountries v0.0.0-20171019111738-adb00f6513a3
github.com/pborman/uuid v0.0.0-20160209185913-a97ce2ca70fa
github.com/pelletier/go-toml v0.0.0-20170628012637-69d355db5304 // indirect
github.com/pkg/errors v0.8.0
github.com/plutov/paypal v2.0.5+incompatible // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/rs/cors v0.0.0-20170608165155-8dd4211afb5d
github.com/rybit/nats_logrus_hook v1.0.4 // indirect
github.com/sebest/xff v0.0.0-20160910043805-6c115e0ffa35
github.com/signalfx/com_signalfx_metrics_protobuf v0.0.0-20170330202426-93e507b42f43 // indirect
github.com/signalfx/gohistogram v0.0.0-20160107210732-1ccfd2ff5083 // indirect
github.com/signalfx/golib v1.0.0 // indirect
github.com/sirupsen/logrus v0.0.0-20170713114250-a3f95b5c4235
github.com/smartystreets/goconvey v0.0.0-20190731233626-505e41936337 // indirect
github.com/spf13/afero v0.0.0-20170217164146-9be650865eab // indirect
github.com/spf13/cast v1.1.0 // indirect
github.com/shopify/logrus-bugsnag v0.0.0-20171204204709-577dee27f20d
github.com/sirupsen/logrus v1.4.2
github.com/spf13/cobra v0.0.0-20170228191748-fcd0c5a1df88
github.com/spf13/jwalterweatherman v0.0.0-20170523133247-0efa5202c046 // indirect
github.com/spf13/pflag v1.0.0 // indirect
github.com/spf13/viper v0.0.0-20170217163817-7538d73b4eb9 // indirect
github.com/streadway/amqp v0.0.0-20170707203015-2cbfe40c9341 // indirect
github.com/stretchr/testify v1.2.2
github.com/stripe/stripe-go v62.9.0+incompatible
golang.org/x/crypto v0.0.0-20190701094942-4def268fd1a4 // indirect
golang.org/x/oauth2 v0.0.0-20170807180024-9a379c6b3e95 // indirect
golang.org/x/sync v0.0.0-20190423024810-112230192c58 // indirect
google.golang.org/api v0.0.0-20170821230356-dd6bdadc5852 // indirect
google.golang.org/appengine v0.0.0-20170814190942-d9a072cfa7b9 // indirect
gopkg.in/alexcesaro/quotedprintable.v3 v3.0.0-20150716171945-2caba252f4dc // indirect
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15 // indirect
gopkg.in/gomail.v2 v2.0.0-20150902115704-41f357289737 // indirect
gopkg.in/logfmt.v0 v0.3.0 // indirect
gopkg.in/mgo.v2 v2.0.0-20190816093944-a6b53ec6cb22 // indirect
gopkg.in/stack.v1 v1.6.0 // indirect
gopkg.in/yaml.v2 v2.0.0-20170721122051-25c4ec802a7d // indirect
)
Loading

0 comments on commit fbb8d6e

Please sign in to comment.