-
Notifications
You must be signed in to change notification settings - Fork 210
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor usage of nconf from netlify-commons
- Loading branch information
Showing
5 changed files
with
131 additions
and
118 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.