Skip to content

Commit

Permalink
Log vault logs instead of vault-http logs
Browse files Browse the repository at this point in the history
  • Loading branch information
bastjan committed Aug 10, 2021
1 parent 5a31238 commit 5e4fa05
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
9 changes: 5 additions & 4 deletions vault/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,10 +70,11 @@ func SetCustomClient(c VaultClient) {

func newBankVaultClient(deletionPolicy synv1alpha1.DeletionPolicy, log logr.Logger) (*BankVaultClient, error) {

client, err := vault.NewClientFromConfig(&api.Config{
Address: os.Getenv(api.EnvVaultAddress),
Logger: logAdapter{log},
}, vault.ClientRole("lieutenant-operator"))
client, err := vault.NewClientFromConfig(
&api.Config{Address: os.Getenv(api.EnvVaultAddress)},
vault.ClientRole("lieutenant-operator"),
vault.ClientLogger(logAdapter{log.WithName("vaultclient")}),
)
if err != nil {
return nil, err
}
Expand Down
35 changes: 29 additions & 6 deletions vault/log_adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,37 @@ type logAdapter struct {

var errLogAdapterLogError = errors.New("vault error log")

func (l logAdapter) Debug(msg string, keysAndValues ...interface{}) {
l.V(1).Info(msg, keysAndValues...)
func (l logAdapter) Trace(msg string, fields ...map[string]interface{}) {
l.V(2).Info(msg, flatten(fields)...)
}

func (l logAdapter) Warn(msg string, keysAndValues ...interface{}) {
l.Info("WARN: "+msg, keysAndValues...)
func (l logAdapter) Debug(msg string, fields ...map[string]interface{}) {
l.V(1).Info(msg, flatten(fields)...)
}

func (l logAdapter) Error(msg string, keysAndValues ...interface{}) {
l.Logger.Error(errLogAdapterLogError, msg, keysAndValues...)
func (l logAdapter) Info(msg string, fields ...map[string]interface{}) {
l.V(0).Info(msg, flatten(fields)...)
}

func (l logAdapter) Warn(msg string, fields ...map[string]interface{}) {
l.V(0).Info("Warn: "+msg, flatten(fields)...)
}

func (l logAdapter) Error(msg string, fields ...map[string]interface{}) {
l.V(0).Error(errLogAdapterLogError, msg, flatten(fields)...)
}

func flatten(fields []map[string]interface{}) []interface{} {
flattenedLen := 0
for _, field := range fields {
flattenedLen += len(field) * 2
}

flattened := make([]interface{}, 0, flattenedLen)
for _, field := range fields {
for name, value := range field {
flattened = append(flattened, name, value)
}
}
return flattened
}
13 changes: 13 additions & 0 deletions vault/log_adapter_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package vault

import (
"testing"

"github.com/stretchr/testify/assert"
)

func TestLogAdapterFlatten(t *testing.T) {
assert.Equal(t, flatten([]map[string]interface{}{}), []interface{}{}, "empty array")

assert.Equal(t, flatten([]map[string]interface{}{{"key1": "val"}, {}, {"key1": "val2"}}), []interface{}{"key1", "val", "key1", "val2"})
}

0 comments on commit 5e4fa05

Please sign in to comment.