From afa85bd45095685e3f450fdda2ccaf04fba8614f Mon Sep 17 00:00:00 2001 From: bschlosser Date: Wed, 9 Mar 2022 18:57:02 -0800 Subject: [PATCH] fix #100: allow config to receive FieldLogger interface --- client/client.go | 34 ++++++++++++++++++++-------------- client/config.go | 7 ++++--- 2 files changed, 24 insertions(+), 17 deletions(-) diff --git a/client/client.go b/client/client.go index 4f61d1e..9bcdb27 100644 --- a/client/client.go +++ b/client/client.go @@ -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) { @@ -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) } diff --git a/client/config.go b/client/config.go index e97cee5..2181d7e 100644 --- a/client/config.go +++ b/client/config.go @@ -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 { @@ -29,7 +30,7 @@ type Config struct { LogLevel logrus.Level - Logger *logrus.Logger + Logger logrus.FieldLogger } type ApiUrl string