Skip to content

Commit

Permalink
Merge c9e40de into 580d776
Browse files Browse the repository at this point in the history
  • Loading branch information
bombsimon committed Nov 4, 2023
2 parents 580d776 + c9e40de commit 0a236c6
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 29 deletions.
9 changes: 9 additions & 0 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@ import (
"github.com/sirupsen/logrus"
)

type customType string

func (c customType) MarshalLog() interface{} {
return "custom marshal message"
}

func main() {
logrusLog := logrus.New()
log := logrusr.New(logrusLog)
Expand Down Expand Up @@ -75,4 +81,7 @@ func main() {
)

log.Info("custom types", "my_int", myInt(1), "my_other_int", myInt(2))

myString := customType("original value")
log.Info("custom marshaller", "my_string", myString)
}
9 changes: 0 additions & 9 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,21 @@ github.com/go-logr/logr v1.2.3 h1:2DntVwHkVopvECVRSlL5PSo9eG+cAkDCuckLubN+rq0=
github.com/go-logr/logr v1.2.3/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/sirupsen/logrus v1.9.0 h1:trlNQbNUG3OdDrDil03MCb1H2o9nJ1x4/5LYw7byDE0=
github.com/sirupsen/logrus v1.9.0/go.mod h1:naHLuLoDiP4jHNo9R0sCBMtWGeIprob74mVsIT4qYEQ=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.7.1 h1:5TQK59W5E3v0r2duFAb7P95B6hEeOyEnHRa8MjYSMTY=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150 h1:xHms4gcpe1YE7A3yIllJXP16CMAGuqwO2lX1mTyyRRc=
golang.org/x/sys v0.0.0-20220422013727-9388b58f7150/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.0.0-20220715151400-c0bba94af5f8/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.2.0 h1:ljd4t30dBnAvMZaQCevtY0xLLD0A+bRZXbgLMLU1F/A=
golang.org/x/sys v0.2.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
51 changes: 31 additions & 20 deletions logrusr.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ type FormatFunc func(interface{}) interface{}
// Option is options to give when construction a logrusr logger.
type Option func(l *logrusr)

// WithFormatter will set the FormatFunc to use.
// WithFormatter will set the FormatFunc to use. If you only want to format a
// specific type that is yours, prefer using the logr.Marshaler interface
// instead. The `FormatFunc` is better suited for types that are not yours such
// as external types, maps or slices.
func WithFormatter(f FormatFunc) Option {
return func(l *logrusr) {
l.formatter = f
Expand Down Expand Up @@ -168,25 +171,33 @@ func listToLogrusFields(formatter FormatFunc, keysAndValues ...interface{}) logr
for i := 0; i < len(keysAndValues); i += 2 {
k, v := keysAndValues[i], keysAndValues[i+1]

if s, ok := k.(string); ok {
// Try to avoid marshaling known types.
switch vVal := v.(type) {
case int, int8, int16, int32, int64,
uint, uint8, uint16, uint32, uint64,
float32, float64, complex64, complex128,
string, bool:
f[s] = vVal

case []byte:
f[s] = string(vVal)

default:
if formatter != nil {
f[s] = formatter(v)
} else {
j, _ := json.Marshal(vVal)
f[s] = string(j)
}
s, ok := k.(string)
if !ok {
continue
}

if v, ok := v.(logr.Marshaler); ok {
f[s] = v.MarshalLog()
continue
}

// Try to avoid marshaling known types.
switch vVal := v.(type) {
case int, int8, int16, int32, int64,
uint, uint8, uint16, uint32, uint64,
float32, float64, complex64, complex128,
string, bool:
f[s] = vVal

case []byte:
f[s] = string(vVal)

default:
if formatter != nil {
f[s] = formatter(v)
} else {
j, _ := json.Marshal(vVal)
f[s] = string(j)
}
}
}
Expand Down
27 changes: 27 additions & 0 deletions logrusr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,17 @@ import (
"github.com/stretchr/testify/require"
)

type reverseString string

func (rs reverseString) MarshalLog() interface{} {
runes := []rune(rs)
for i, j := 0, len(runes)-1; i < j; i, j = i+1, j-1 {
runes[i], runes[j] = runes[j], runes[i]
}

return string(runes)
}

func TestLogging(t *testing.T) {
t.Parallel()

Expand Down Expand Up @@ -283,6 +294,22 @@ func TestLogging(t *testing.T) {
"caller": `~testing.go:\d+`,
},
},
{
description: "custom marshaler used",
logFunc: func(log logr.Logger) {
log.Info("reversed",
"forward", "hello, world",
"reversed", reverseString("hello, world"),
)
},
reportCaller: true,
assertions: map[string]string{
"level": "info",
"msg": "reversed",
"forward": "hello, world",
"reversed": "dlrow ,olleh",
},
},
}

for _, tc := range cases {
Expand Down

0 comments on commit 0a236c6

Please sign in to comment.