diff --git a/field.go b/field.go index ecbb7b3b5..2ca08b752 100644 --- a/field.go +++ b/field.go @@ -21,6 +21,7 @@ package zap import ( + "encoding/base64" "fmt" "math" "time" @@ -57,6 +58,13 @@ func Skip() Field { return Field{fieldType: skipType} } +// Base64 constructs a field that encodes the given value as a +// padded base64 string. The byte slice is converted to a base64 +// string immediately. +func Base64(key string, val []byte) Field { + return String(key, base64.StdEncoding.EncodeToString(val)) +} + // Bool constructs a Field with the given key and value. func Bool(key string, val bool) Field { var ival int64 diff --git a/field_test.go b/field_test.go index 1d9924adb..e18749473 100644 --- a/field_test.go +++ b/field_test.go @@ -172,6 +172,13 @@ func TestNestField(t *testing.T) { assertCanBeReused(t, nest) } +func TestBase64Field(t *testing.T) { + assertFieldJSON(t, `"foo":"YWIxMg=="`, + Base64("foo", []byte("ab12")), + ) + assertCanBeReused(t, Base64("foo", []byte("bar"))) +} + func TestLogMarshalerFunc(t *testing.T) { assertFieldJSON(t, `"foo":{"name":"phil"}`, Marshaler("foo", LogMarshalerFunc(fakeUser{"phil"}.MarshalLog)))