Skip to content

Commit

Permalink
Add Func log method
Browse files Browse the repository at this point in the history
This is adds the Func log method to log using an anonymous function
only if the level is currently enabled.

The use case is for when you don't own an object and therefore can't
create your own marshaller but need to do some translation prior to
logging.

For example, this:

msg := log.Debug()
if msg.Enabled() {
  msg.Str("complicated_thing", makeBinaryThingLoggable(thing))
}
msg.Msg("Sending complicated thing")

Turns into this:

log.Debug().
  Func(func(e *Event) { e.Str("complicated_thing", makeBinaryThingLoggable(thing)) }).
  Msg("Sending complicated thing")
  • Loading branch information
marwatk committed May 19, 2021
1 parent 9e51190 commit b3b0b38
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 1 deletion.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,7 @@ Some settings can be changed and will by applied to all loggers:
### Advanced Fields

* `Err`: Takes an `error` and renders it as a string using the `zerolog.ErrorFieldName` field name.
* `Func`: Run a `func` only if the level is enabled.
* `Timestamp`: Inserts a timestamp field with `zerolog.TimestampFieldName` field name, formatted using `zerolog.TimeFieldFormat`.
* `Time`: Adds a field with time formatted with `zerolog.TimeFieldFormat`.
* `Dur`: Adds a field with `time.Duration`.
Expand Down
9 changes: 9 additions & 0 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,15 @@ func (e *Event) Object(key string, obj LogObjectMarshaler) *Event {
return e
}

// Func allows an anonymous func to run only if the event is enabled.
func (e *Event) Func(f func(e *Event)) *Event {
if e == nil {
return e
}
f(e)
return e
}

// EmbedObject marshals an object that implement the LogObjectMarshaler interface.
func (e *Event) EmbedObject(obj LogObjectMarshaler) *Event {
if e == nil {
Expand Down
4 changes: 3 additions & 1 deletion log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ func TestFields(t *testing.T) {
Bytes("bytes", []byte("bar")).
Hex("hex", []byte{0x12, 0xef}).
RawJSON("json", []byte(`{"some":"json"}`)).
Func(func(e *Event) { e.Str("func", "func_output") }).
AnErr("some_err", nil).
Err(errors.New("some error")).
Bool("bool", true).
Expand All @@ -265,7 +266,7 @@ func TestFields(t *testing.T) {
Time("time", time.Time{}).
TimeDiff("diff", now, now.Add(-10*time.Second)).
Msg("")
if got, want := decodeIfBinaryToString(out.Bytes()), `{"caller":"`+caller+`","string":"foo","stringer":"127.0.0.1","stringer_nil":null,"bytes":"bar","hex":"12ef","json":{"some":"json"},"error":"some error","bool":true,"int":1,"int8":2,"int16":3,"int32":4,"int64":5,"uint":6,"uint8":7,"uint16":8,"uint32":9,"uint64":10,"IPv4":"192.168.0.100","IPv6":"2001:db8:85a3::8a2e:370:7334","Mac":"00:14:22:01:23:45","Prefix":"192.168.0.100/24","float32":11.1234,"float64":12.321321321,"dur":1000,"time":"0001-01-01T00:00:00Z","diff":10000}`+"\n"; got != want {
if got, want := decodeIfBinaryToString(out.Bytes()), `{"caller":"`+caller+`","string":"foo","stringer":"127.0.0.1","stringer_nil":null,"bytes":"bar","hex":"12ef","json":{"some":"json"},"func":"func_output","error":"some error","bool":true,"int":1,"int8":2,"int16":3,"int32":4,"int64":5,"uint":6,"uint8":7,"uint16":8,"uint32":9,"uint64":10,"IPv4":"192.168.0.100","IPv6":"2001:db8:85a3::8a2e:370:7334","Mac":"00:14:22:01:23:45","Prefix":"192.168.0.100/24","float32":11.1234,"float64":12.321321321,"dur":1000,"time":"0001-01-01T00:00:00Z","diff":10000}`+"\n"; got != want {
t.Errorf("invalid log output:\ngot: %v\nwant: %v", got, want)
}
}
Expand Down Expand Up @@ -361,6 +362,7 @@ func TestFieldsDisabled(t *testing.T) {
Hex("hex", []byte{0x12, 0xef}).
AnErr("some_err", nil).
Err(errors.New("some error")).
Func(func(e *Event) { e.Str("func", "func_output") }).
Bool("bool", true).
Int("int", 1).
Int8("int8", 2).
Expand Down

0 comments on commit b3b0b38

Please sign in to comment.