diff --git a/core/engine.go b/core/engine.go index 396d3cd..7d5d16d 100644 --- a/core/engine.go +++ b/core/engine.go @@ -137,7 +137,12 @@ func (e *Engine) Router() *gin.Engine { // BuildHTTPClient builds HTTP client with provided configuration func (e *Engine) BuildHTTPClient(replaceDefault ...bool) *Engine { if e.Config.GetHTTPClientConfig() != nil { - if client, err := NewHTTPClientBuilder().FromEngine(e).Build(replaceDefault...); err != nil { + client, err := NewHTTPClientBuilder(). + WithLogger(e.Logger). + SetLogging(e.Config.IsDebug()). + FromEngine(e).Build(replaceDefault...) + + if err != nil { panic(err) } else { e.httpClient = client diff --git a/core/http_client_builder.go b/core/http_client_builder.go index f4dc325..a945468 100644 --- a/core/http_client_builder.go +++ b/core/http_client_builder.go @@ -8,6 +8,7 @@ import ( "net/http" "time" + "github.com/op/go-logging" "github.com/pkg/errors" ) @@ -45,7 +46,7 @@ type HTTPClientBuilder struct { httpClient *http.Client httpTransport *http.Transport dialer *net.Dialer - engine *Engine + logger *logging.Logger built bool logging bool timeout time.Duration @@ -68,6 +69,15 @@ func NewHTTPClientBuilder() *HTTPClientBuilder { } } +// WithLogger sets provided logger into HTTPClientBuilder +func (b *HTTPClientBuilder) WithLogger(logger *logging.Logger) *HTTPClientBuilder { + if logger != nil { + b.logger = logger + } + + return b +} + // SetTimeout sets timeout for http client func (b *HTTPClientBuilder) SetTimeout(timeout time.Duration) *HTTPClientBuilder { timeout = timeout * time.Second @@ -105,9 +115,9 @@ func (b *HTTPClientBuilder) SetSSLVerification(enabled bool) *HTTPClientBuilder return b } -// EnableLogging enables logging in mocks -func (b *HTTPClientBuilder) EnableLogging() *HTTPClientBuilder { - b.logging = true +// SetLogging enables or disables logging in mocks +func (b *HTTPClientBuilder) SetLogging(flag bool) *HTTPClientBuilder { + b.logging = flag return b } @@ -133,8 +143,6 @@ func (b *HTTPClientBuilder) FromConfig(config HTTPClientConfigInterface) *HTTPCl // FromEngine fulfills mock configuration from ConfigInterface inside Engine func (b *HTTPClientBuilder) FromEngine(engine *Engine) *HTTPClientBuilder { - b.engine = engine - b.logging = engine.Config.IsDebug() return b.FromConfig(engine.Config.GetHTTPClientConfig()) } @@ -212,8 +220,8 @@ func (b *HTTPClientBuilder) buildMocks() error { // logf prints logs via Engine or via fmt.Printf func (b *HTTPClientBuilder) logf(format string, args ...interface{}) { if b.logging { - if b.engine != nil && b.engine.Logger != nil { - b.engine.Logger.Infof(format, args...) + if b.logger != nil { + b.logger.Infof(format, args...) } else { fmt.Printf(format, args...) } diff --git a/core/http_client_builder_test.go b/core/http_client_builder_test.go index c48ff71..2d56ed3 100644 --- a/core/http_client_builder_test.go +++ b/core/http_client_builder_test.go @@ -5,7 +5,9 @@ import ( "testing" "time" + "github.com/op/go-logging" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" ) @@ -25,9 +27,12 @@ func (t *HTTPClientBuilderTest) Test_SetTimeout() { assert.Equal(t.T(), 90*time.Second, t.builder.httpClient.Timeout) } -func (t *HTTPClientBuilderTest) Test_EnableLogging() { - t.builder.EnableLogging() - assert.Equal(t.T(), true, t.builder.logging) +func (t *HTTPClientBuilderTest) Test_SetLogging() { + t.builder.SetLogging(true) + assert.True(t.T(), t.builder.logging) + + t.builder.SetLogging(false) + assert.False(t.T(), t.builder.logging) } func (t *HTTPClientBuilderTest) Test_SetMockAddress() { @@ -97,7 +102,7 @@ func (t *HTTPClientBuilderTest) Test_FromEngine() { } t.builder.FromEngine(engine) - assert.NotNil(t.T(), engine, t.builder.engine) + assert.Equal(t.T(), engine.Config.GetHTTPClientConfig().GetMockAddress(), t.builder.mockAddress) } func (t *HTTPClientBuilderTest) Test_buildDialer() { @@ -114,6 +119,18 @@ func (t *HTTPClientBuilderTest) Test_buildMocks() { assert.NoError(t.T(), t.builder.buildMocks()) } +func (t *HTTPClientBuilderTest) Test_WithLogger() { + logger := NewLogger("telegram", logging.ERROR, DefaultLogFormatter()) + builder := NewHTTPClientBuilder() + require.Nil(t.T(), builder.logger) + + builder.WithLogger(nil) + assert.Nil(t.T(), builder.logger) + + builder.WithLogger(logger) + assert.NotNil(t.T(), builder.logger) +} + func (t *HTTPClientBuilderTest) Test_logf() { defer func() { assert.Nil(t.T(), recover()) diff --git a/core/logger.go b/core/logger.go index 4a1398d..7c16627 100644 --- a/core/logger.go +++ b/core/logger.go @@ -8,7 +8,7 @@ import ( // NewLogger will create new logger with specified formatter. // Usage: -// logger := NewLogger(config, DefaultLogFormatter()) +// logger := NewLogger("telegram", logging.ERROR, DefaultLogFormatter()) func NewLogger(transportCode string, logLevel logging.Level, logFormat logging.Formatter) *logging.Logger { logger := logging.MustGetLogger(transportCode) logBackend := logging.NewLogBackend(os.Stdout, "", 0)