Skip to content

Commit

Permalink
Merge pull request #102 from brensch/fix-#100
Browse files Browse the repository at this point in the history
Allow config to receive FieldLogger interface
  • Loading branch information
prakhar-mudaiya authored May 4, 2022
2 parents 2a8f799 + afa85bd commit 9f7c0b8
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 17 deletions.
34 changes: 20 additions & 14 deletions client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,19 +174,26 @@ func setConfiguration(opsGenieClient *OpsGenieClient, cfg *Config) {
}

func setLogger(conf *Config) {
if conf.Logger == nil {
conf.Logger = logrus.New()
if conf.LogLevel != (logrus.Level(0)) { // todo fix panic level
conf.Logger.SetLevel(conf.LogLevel)
}
conf.Logger.SetFormatter(
&logrus.TextFormatter{
ForceColors: true,
FullTimestamp: true,
TimestampFormat: time.RFC3339Nano,
},
)
// if user has already set logger, skip
if conf.Logger != nil {
return
}

// otherwise, create a new logger for the user
logger := logrus.New()
// set log level if user has specified one
if conf.LogLevel != (logrus.Level(0)) {
logger.SetLevel(conf.LogLevel)
}
logger.SetFormatter(
&logrus.TextFormatter{
ForceColors: true,
FullTimestamp: true,
TimestampFormat: time.RFC3339Nano,
},
)
conf.Logger = logger

}

func setRetryPolicy(opsGenieClient *OpsGenieClient, cfg *Config) {
Expand Down Expand Up @@ -247,9 +254,8 @@ func NewOpsGenieClient(cfg *Config) (*OpsGenieClient, error) {
}

func printInfoLog(client *OpsGenieClient) {
client.Config.Logger.Infof("Client is configured with ApiUrl: %s, LogLevel: %s, RetryMaxCount: %v",
client.Config.Logger.Infof("Client is configured with ApiUrl: %s, RetryMaxCount: %v",
client.Config.OpsGenieAPIURL,
client.Config.Logger.GetLevel().String(),
client.RetryableClient.RetryMax)
}

Expand Down
7 changes: 4 additions & 3 deletions client/config.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package client

import (
"net/http"
"time"

"github.com/hashicorp/go-retryablehttp"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"net/http"
"time"
)

type Config struct {
Expand All @@ -29,7 +30,7 @@ type Config struct {

LogLevel logrus.Level

Logger *logrus.Logger
Logger logrus.FieldLogger
}

type ApiUrl string
Expand Down

0 comments on commit 9f7c0b8

Please sign in to comment.