Skip to content

Commit

Permalink
fix commons issue
Browse files Browse the repository at this point in the history
  • Loading branch information
rybit committed Sep 9, 2019
1 parent dfb5ee9 commit f3deff1
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 4 deletions.
7 changes: 3 additions & 4 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"
)

const DefaultGitHubEndpoint = "https://api.github.com"
Expand Down Expand Up @@ -56,8 +55,8 @@ type GlobalConfiguration struct {
Endpoint string
}
DB DBConfiguration
Logging nconf.LoggingConfig `envconfig:"LOG"`
OperatorToken string `split_words:"true"`
Logging LoggingConfig `envconfig:"LOG"`
OperatorToken string `split_words:"true"`
MultiInstanceMode bool
}

Expand Down Expand Up @@ -94,7 +93,7 @@ func LoadGlobal(filename string) (*GlobalConfiguration, error) {
if err := envconfig.Process("gitgateway", 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
61 changes: 61 additions & 0 deletions conf/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package conf

import (
"os"
"time"

"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"`
}

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.Formatter = &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.Out = 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.Level.String())
}

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

return logger.WithFields(f), nil
}

0 comments on commit f3deff1

Please sign in to comment.