Add granunal logging config controls.
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package transport
|
||||
|
||||
import (
|
||||
"log/slog"
|
||||
"time"
|
||||
)
|
||||
|
||||
@@ -11,6 +12,8 @@ type ConnectionConfig struct {
|
||||
WriteTimeout time.Duration
|
||||
IncomingBufferSize int
|
||||
ErrorsBufferSize int
|
||||
LoggingEnabled bool
|
||||
LogLevel *slog.Level
|
||||
Retry *RetryConfig
|
||||
}
|
||||
|
||||
@@ -40,6 +43,8 @@ func GetDefaultConnectionConfig() *ConnectionConfig {
|
||||
WriteTimeout: 30 * time.Second,
|
||||
IncomingBufferSize: 100,
|
||||
ErrorsBufferSize: 10,
|
||||
LoggingEnabled: true,
|
||||
LogLevel: nil,
|
||||
Retry: GetDefaultRetryConfig(),
|
||||
}
|
||||
}
|
||||
@@ -178,6 +183,20 @@ func WithErrorsBufferSize(value int) ConnectionOption {
|
||||
}
|
||||
}
|
||||
|
||||
func WithLoggingEnabled(value bool) ConnectionOption {
|
||||
return func(c *ConnectionConfig) error {
|
||||
c.LoggingEnabled = value
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func WithLogLevel(level slog.Level) ConnectionOption {
|
||||
return func(c *ConnectionConfig) error {
|
||||
c.LogLevel = &level
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
func WithoutRetry() ConnectionOption {
|
||||
return func(c *ConnectionConfig) error {
|
||||
c.Retry = nil
|
||||
|
||||
@@ -2,6 +2,7 @@ package transport
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"log/slog"
|
||||
"testing"
|
||||
"time"
|
||||
)
|
||||
@@ -17,6 +18,8 @@ func TestNewConnectionConfig(t *testing.T) {
|
||||
WriteTimeout: 30 * time.Second,
|
||||
IncomingBufferSize: 100,
|
||||
ErrorsBufferSize: 10,
|
||||
LoggingEnabled: true,
|
||||
LogLevel: nil,
|
||||
Retry: GetDefaultRetryConfig(),
|
||||
})
|
||||
|
||||
@@ -38,6 +41,8 @@ func TestDefaultConnectionConfig(t *testing.T) {
|
||||
WriteTimeout: 30 * time.Second,
|
||||
IncomingBufferSize: 100,
|
||||
ErrorsBufferSize: 10,
|
||||
LoggingEnabled: true,
|
||||
LogLevel: nil,
|
||||
Retry: GetDefaultRetryConfig(),
|
||||
})
|
||||
}
|
||||
@@ -61,6 +66,8 @@ func TestApplyConnectionOptions(t *testing.T) {
|
||||
conf,
|
||||
WithIncomingBufferSize(256),
|
||||
WithErrorsBufferSize(100),
|
||||
WithLoggingEnabled(false),
|
||||
WithLogLevel(slog.LevelError),
|
||||
WithRetryMaxRetries(0),
|
||||
WithRetryInitialDelay(3*time.Second),
|
||||
WithRetryJitterFactor(0.5),
|
||||
@@ -69,6 +76,8 @@ func TestApplyConnectionOptions(t *testing.T) {
|
||||
assert.NoError(t, err)
|
||||
assert.Equal(t, 256, conf.IncomingBufferSize)
|
||||
assert.Equal(t, 100, conf.ErrorsBufferSize)
|
||||
assert.False(t, conf.LoggingEnabled)
|
||||
assert.Equal(t, slog.LevelError, *conf.LogLevel)
|
||||
assert.Equal(t, 0, conf.Retry.MaxRetries)
|
||||
assert.Equal(t, 3*time.Second, conf.Retry.InitialDelay)
|
||||
assert.Equal(t, 0.5, conf.Retry.JitterFactor)
|
||||
|
||||
Reference in New Issue
Block a user