-
Notifications
You must be signed in to change notification settings - Fork 22
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
🛠️ new logger/zerologadapter
for retryable requests
#4903
Merged
Merged
Changes from 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
// Copyright (c) Mondoo, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package zerologadapter | ||
|
||
import "github.com/rs/zerolog" | ||
|
||
// New returns a new adapter for the zerolog logger to the LeveledLogger interface. This struct is | ||
// mainly used in conjunction with a retryable http client to convert all retry logs to debug logs. | ||
// | ||
// NOTE that all messages will go to debug level. | ||
// | ||
// e.g. | ||
// ```go | ||
// retryClient := retryablehttp.NewClient() | ||
// retryClient.Logger = zerologadapter.New(log.Logger) | ||
// ``` | ||
func New(logger zerolog.Logger) *Adapter { | ||
return &Adapter{logger} | ||
} | ||
|
||
type Adapter struct { | ||
logger zerolog.Logger | ||
} | ||
|
||
func (z *Adapter) Msg(msg string, keysAndValues ...interface{}) { | ||
z.logger.Debug().Fields(convertToFields(keysAndValues...)).Msg(msg) | ||
} | ||
|
||
func (z *Adapter) Error(msg string, keysAndValues ...interface{}) { | ||
z.logger.Debug().Fields(convertToFields(keysAndValues...)).Msg(msg) | ||
} | ||
|
||
func (z *Adapter) Info(msg string, keysAndValues ...interface{}) { | ||
z.logger.Debug().Fields(convertToFields(keysAndValues...)).Msg(msg) | ||
} | ||
|
||
func (z *Adapter) Debug(msg string, keysAndValues ...interface{}) { | ||
z.logger.Debug().Fields(convertToFields(keysAndValues...)).Msg(msg) | ||
} | ||
|
||
func (z *Adapter) Warn(msg string, keysAndValues ...interface{}) { | ||
z.logger.Debug().Fields(convertToFields(keysAndValues...)).Msg(msg) | ||
} | ||
|
||
func convertToFields(keysAndValues ...interface{}) map[string]interface{} { | ||
fields := make(map[string]interface{}) | ||
for i := 0; i < len(keysAndValues); i += 2 { | ||
if i+1 < len(keysAndValues) { | ||
keyString, ok := keysAndValues[i].(string) | ||
if ok { // safelty first, eventhough we always expect a string | ||
afiune marked this conversation as resolved.
Show resolved
Hide resolved
|
||
fields[keyString] = keysAndValues[i+1] | ||
} | ||
} | ||
} | ||
return fields | ||
} |
61 changes: 61 additions & 0 deletions
61
providers-sdk/v1/util/zerologadapter/adapter_internal_test.go
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,61 @@ | ||
// Copyright (c) Mondoo, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package zerologadapter | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestConvertToFields(t *testing.T) { | ||
t.Run("Valid key-value pairs", func(t *testing.T) { | ||
input := []interface{}{"key1", "value1", "key2", 42, "key3", true} | ||
expected := map[string]interface{}{ | ||
"key1": "value1", | ||
"key2": 42, | ||
"key3": true, | ||
} | ||
|
||
result := convertToFields(input...) | ||
assert.Equal(t, expected, result) | ||
}) | ||
|
||
t.Run("Odd number of elements", func(t *testing.T) { | ||
input := []interface{}{"key1", "value1", "key2"} | ||
expected := map[string]interface{}{ | ||
"key1": "value1", | ||
} | ||
|
||
result := convertToFields(input...) | ||
assert.Equal(t, expected, result) | ||
}) | ||
|
||
t.Run("Non-string keys are ignored", func(t *testing.T) { | ||
input := []interface{}{123, "value1", "key2", 42, 3.14, "value3", "key3", true} | ||
expected := map[string]interface{}{ | ||
"key2": 42, | ||
"key3": true, | ||
} | ||
|
||
result := convertToFields(input...) | ||
assert.Equal(t, expected, result) | ||
}) | ||
|
||
t.Run("Empty input", func(t *testing.T) { | ||
input := []interface{}{} | ||
expected := map[string]interface{}{} | ||
|
||
result := convertToFields(input...) | ||
assert.Equal(t, expected, result) | ||
}) | ||
|
||
t.Run("Nil input", func(t *testing.T) { | ||
var input []interface{} | ||
expected := map[string]interface{}{} | ||
|
||
result := convertToFields(input...) | ||
assert.Equal(t, expected, result) | ||
}) | ||
} |
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,84 @@ | ||
// Copyright (c) Mondoo, Inc. | ||
// SPDX-License-Identifier: BUSL-1.1 | ||
|
||
package zerologadapter_test | ||
|
||
import ( | ||
"bytes" | ||
"testing" | ||
|
||
subject "go.mondoo.com/cnquery/v11/providers-sdk/v1/util/zerologadapter" | ||
|
||
"github.com/rs/zerolog" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewAdapter(t *testing.T) { | ||
var logOutput bytes.Buffer | ||
logger := zerolog.New(&logOutput).Level(zerolog.DebugLevel) | ||
adapter := subject.New(logger) | ||
|
||
t.Run("Msg method logs correctly", func(t *testing.T) { | ||
logOutput.Reset() | ||
adapter.Msg("Test message", "key1", "value1", "key2", 42) | ||
|
||
expectedLog := `{"level":"debug","key1":"value1","key2":42,"message":"Test message"}` | ||
assert.JSONEq(t, expectedLog, logOutput.String()) | ||
}) | ||
|
||
t.Run("Error method logs correctly", func(t *testing.T) { | ||
logOutput.Reset() | ||
adapter.Error("Error occurred", "error_code", 500) | ||
|
||
expectedLog := `{"level":"debug","error_code":500,"message":"Error occurred"}` | ||
assert.JSONEq(t, expectedLog, logOutput.String()) | ||
}) | ||
|
||
t.Run("Info method logs correctly", func(t *testing.T) { | ||
logOutput.Reset() | ||
adapter.Info("Info message", "key", "value") | ||
|
||
expectedLog := `{"level":"debug","key":"value","message":"Info message"}` | ||
assert.JSONEq(t, expectedLog, logOutput.String()) | ||
}) | ||
|
||
t.Run("Debug method logs correctly", func(t *testing.T) { | ||
logOutput.Reset() | ||
adapter.Debug("Debugging issue", "context", "test") | ||
|
||
expectedLog := `{"level":"debug","context":"test","message":"Debugging issue"}` | ||
assert.JSONEq(t, expectedLog, logOutput.String()) | ||
}) | ||
|
||
t.Run("Warn method logs correctly", func(t *testing.T) { | ||
logOutput.Reset() | ||
adapter.Warn("Warning issued", "warning_level", "high") | ||
|
||
expectedLog := `{"level":"debug","warning_level":"high","message":"Warning issued"}` | ||
assert.JSONEq(t, expectedLog, logOutput.String()) | ||
}) | ||
|
||
t.Run("Handles non-string keys gracefully", func(t *testing.T) { | ||
logOutput.Reset() | ||
adapter.Info("Non-string key test", 123, "value", "key2", 42) | ||
|
||
expectedLog := `{"level":"debug","key2":42,"message":"Non-string key test"}` | ||
assert.JSONEq(t, expectedLog, logOutput.String()) | ||
}) | ||
|
||
t.Run("Handles odd number of key-value pairs gracefully", func(t *testing.T) { | ||
logOutput.Reset() | ||
adapter.Debug("Odd number test", "key1", "value1", "key2") | ||
|
||
expectedLog := `{"level":"debug","key1":"value1","message":"Odd number test"}` | ||
assert.JSONEq(t, expectedLog, logOutput.String()) | ||
}) | ||
|
||
t.Run("Empty key-value pairs", func(t *testing.T) { | ||
logOutput.Reset() | ||
adapter.Warn("Empty key-value test") | ||
|
||
expectedLog := `{"level":"debug","message":"Empty key-value test"}` | ||
assert.JSONEq(t, expectedLog, logOutput.String()) | ||
}) | ||
} |
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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think this package should be in https://github.com/mondoohq/cnquery/tree/main/logger
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I put it inside the
providers-sdk/v1
to make it clear that providers can and should use it, Ill move it though.