Skip to content

Commit

Permalink
added customizable log level
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric Solender committed Nov 22, 2020
1 parent 84fe1ac commit b819765
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions config.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,29 +30,17 @@ import (
"strings"
)

var externalLog *logrus.Entry
var neoVersion float64

var log = getLogger()
var log logrus.FieldLogger

func getLogger() *logrus.Entry {
if externalLog == nil {
//create default logger
toReturn := logrus.New()

return toReturn.WithField("source", "gogm")
}

return externalLog
func init() {
makeDefaultLogger()
}

// SetLogger sets logrus logger
func SetLogger(logger *logrus.Entry) error {
if logger == nil {
return errors.New("logger can not be nil")
}
externalLog = logger
return nil
func makeDefaultLogger() {
_log := logrus.New()
log = _log.WithField("error", "not initialized")
}

func getIsV4() bool {
Expand Down Expand Up @@ -84,6 +72,10 @@ type Config struct {
// Index Strategy defines the index strategy for GoGM
IndexStrategy IndexStrategy `yaml:"index_strategy" json:"index_strategy" mapstructure:"index_strategy"`
TargetDbs []string `yaml:"target_dbs" json:"target_dbs" mapstructure:"target_dbs"`

Logger logrus.FieldLogger `yaml:"-" json:"-" mapstructure:"-"`
// if logger is not nil log level will be ignored
LogLevel string `json:"log_level" yaml:"log_level" mapstructure:"log_level"`
}

// ConnectionString builds the neo4j bolt/bolt+routing connection string
Expand Down Expand Up @@ -157,6 +149,23 @@ func setupInit(isTest bool, conf *Config, mapTypes ...interface{}) error {
}

if conf != nil {
if conf.Logger != nil {
log = conf.Logger
} else {
_log := logrus.New()

// set info if nothing has been set
if conf.LogLevel == "" {
conf.LogLevel = "INFO"
}
lvl, err := logrus.ParseLevel(conf.LogLevel)
if err != nil {
return err
}
_log.SetLevel(lvl)
log = _log
}

if conf.TargetDbs == nil || len(conf.TargetDbs) == 0 {
conf.TargetDbs = []string{"neo4j"}
}
Expand Down

0 comments on commit b819765

Please sign in to comment.