-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from digitalocean/dkomsa/use-proper-logger-for…
…-http-client Use proper logger for http client
- Loading branch information
Showing
4 changed files
with
105 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package netbox | ||
|
||
import ( | ||
retryablehttp "github.com/hashicorp/go-retryablehttp" | ||
"go.uber.org/zap" | ||
) | ||
|
||
// retryableHTTPLogger is a wrapper for zap logger that implements retyablehttp.LeveledLogger | ||
// interface and therefore can be passed to a retryablehttp client | ||
type retryableHTTPLogger struct { | ||
logger *zap.Logger | ||
} | ||
|
||
func newRetryableHTTPLogger(logger *zap.Logger) retryablehttp.LeveledLogger { | ||
return &retryableHTTPLogger{logger: logger} | ||
} | ||
|
||
func (l *retryableHTTPLogger) Error(msg string, keysAndValues ...interface{}) { | ||
l.logger.Error(msg, fieldsFromKeysAndValues(keysAndValues)...) | ||
} | ||
|
||
func (l *retryableHTTPLogger) Info(msg string, keysAndValues ...interface{}) { | ||
l.logger.Info(msg, fieldsFromKeysAndValues(keysAndValues)...) | ||
} | ||
|
||
func (l *retryableHTTPLogger) Debug(msg string, keysAndValues ...interface{}) { | ||
l.logger.Info(msg, fieldsFromKeysAndValues(keysAndValues)...) | ||
} | ||
|
||
func (l *retryableHTTPLogger) Warn(msg string, keysAndValues ...interface{}) { | ||
l.logger.Info(msg, fieldsFromKeysAndValues(keysAndValues)...) | ||
} | ||
|
||
func fieldsFromKeysAndValues(keysAndValues []interface{}) []zap.Field { | ||
var fields []zap.Field | ||
for i := 1; i < len(keysAndValues); i += 2 { | ||
key := keysAndValues[i-1] | ||
value := keysAndValues[i] | ||
if keyStr, ok := key.(string); ok { | ||
fields = append(fields, zap.Any(keyStr, value)) | ||
} | ||
// ignore malformed key-value pair | ||
} | ||
return fields | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package netbox | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/google/go-cmp/cmp" | ||
"go.uber.org/zap" | ||
) | ||
|
||
func TestFieldsFromKeysAndValues(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
keysAndValues []interface{} | ||
expectedFields []zap.Field | ||
}{{ | ||
name: "empty", | ||
}, { | ||
name: "simple string pair", | ||
keysAndValues: []interface{}{"foo", "bar"}, | ||
expectedFields: []zap.Field{zap.Any("foo", "bar")}, | ||
}, { | ||
name: "multiple pairs", | ||
keysAndValues: []interface{}{"foo", 1, "bar", true}, | ||
expectedFields: []zap.Field{zap.Any("foo", 1), zap.Any("bar", true)}, | ||
}, { | ||
name: "key without value", | ||
keysAndValues: []interface{}{"foo", "bar", "baz"}, | ||
expectedFields: []zap.Field{zap.Any("foo", "bar")}, | ||
}, { | ||
name: "key is not a string", | ||
keysAndValues: []interface{}{100, "bar"}, | ||
}} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
fields := fieldsFromKeysAndValues(test.keysAndValues) | ||
|
||
if diff := cmp.Diff(test.expectedFields, fields); diff != "" { | ||
t.Errorf("\n (-want, +got)\n%s", diff) | ||
} | ||
}) | ||
} | ||
} |