Skip to content

Commit

Permalink
Logwitherr (#131)
Browse files Browse the repository at this point in the history
* update vendor deps for assertions, gls, and goconvey for go 1.12 support

* make add log functions for logging if err with log keys
  • Loading branch information
charless-splunk authored Mar 18, 2019
1 parent 24fac12 commit ed6b82f
Show file tree
Hide file tree
Showing 45 changed files with 426 additions and 9,771 deletions.
26 changes: 8 additions & 18 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 5 additions & 9 deletions Gopkg.toml
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,6 @@
[[constraint]]
name = "gopkg.in/logfmt.v0"
version = "v0.1.0"
#
#[[constraint]]
# name = "github.com/jtolds/gls"
# version = "v4.2.0"

[[constraint]]
name = "github.com/golang/protobuf"
Expand All @@ -76,16 +72,16 @@

[[constraint]]
name = "github.com/smartystreets/goconvey"
revision = "cc5d85f4f15bd2163abf0fe6e6753356dde72aa2"

#[[constraint]]
# name = "github.com/smartystreets/assertions"
# revision = "2d74a419fbe076f8c76ed8b1c7c633a676c5dc63"
revision = "200a235640ff2643e3126834b67f3e93df76640a"

[[constraint]]
name = "github.com/stretchr/testify"
revision = "c5d7a69bf8a2c9c374798160849c071093e41dd1"

[[override]]
name = "github.com/smartystreets/assertions"
revision = "980c5ac6f3acb6ce1cfe13160e49188807174fbe"

[[constraint]]
branch = "master"
name = "github.com/juju/errors"
Expand Down
25 changes: 25 additions & 0 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,28 @@ func IfErr(l Logger, err error) {
l.Log(Err, err)
}
}

// IfErrAndReturn is a shorthand that will log an error if err is not nil and returns the original err
func IfErrAndReturn(l Logger, err error) error {
if err != nil {
l.Log(Err, err)
}
return err
}

// IfErrWithKeys logs an error with the supplied logger if it is not nil.
// The error will be appended to the end of the supplied keys/messages
func IfErrWithKeys(l Logger, err error, intf ...interface{}) {
if err != nil && l != nil {
l.Log(append(intf, err)...)
}
}

// IfErrWithKeysAndReturn logs an error with the supplied logger if it is not nil and then returns the original error.
// The error will be appended to the end of the supplied keys/messages
func IfErrWithKeysAndReturn(l Logger, err error, intf ...interface{}) error {
if err != nil && l != nil {
l.Log(append(intf, err)...)
}
return err
}
83 changes: 83 additions & 0 deletions log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package log
import (
"bytes"
"errors"
"fmt"
"github.com/signalfx/golib/eventcounter"
. "github.com/smartystreets/goconvey/convey"
"io"
Expand Down Expand Up @@ -230,6 +231,38 @@ func BenchmarkTenWith(b *testing.B) {
}
}

func BenchmarkIfErr(b *testing.B) {
for n := 0; n < b.N; n++ {
b := &bytes.Buffer{}
l := NewLogfmtLogger(b, Panic)
IfErr(l, fmt.Errorf("hello world"))
}
}

func BenchmarkIfErrAndReturn(b *testing.B) {
for n := 0; n < b.N; n++ {
b := &bytes.Buffer{}
l := NewLogfmtLogger(b, Panic)
IfErrAndReturn(l, fmt.Errorf("hello world"))
}
}

func BenchmarkIfErrWithKeys(b *testing.B) {
for n := 0; n < b.N; n++ {
b := &bytes.Buffer{}
l := NewLogfmtLogger(b, Panic)
IfErrWithKeys(l, fmt.Errorf("hello world"), Err, "test message")
}
}

func BenchmarkIfErrWithKeysAndReturn(b *testing.B) {
for n := 0; n < b.N; n++ {
b := &bytes.Buffer{}
l := NewLogfmtLogger(b, Panic)
IfErrWithKeysAndReturn(l, fmt.Errorf("hello world"), Err, "test message")
}
}

func TestDisabledLog(t *testing.T) {
Convey("a nil log", t, func() {
var logger Logger
Expand Down Expand Up @@ -344,6 +377,56 @@ func TestIfErr(t *testing.T) {
}
}

func TestIfErrAndReturn(t *testing.T) {
b := &bytes.Buffer{}
l := NewLogfmtLogger(b, Panic)
if err := IfErrAndReturn(l, nil); err != nil {
t.Error("Expected an nil return value")
}
if b.String() != "" {
t.Error("Expected empty string")
}
testErr := errors.New("nope")
if err := IfErrAndReturn(l, testErr); err != testErr {
t.Error("Expected error to equal ")
}
if b.String() == "" {
t.Error("Expected error result")
}
}

func TestIfErrWithKeysAndReturn(t *testing.T) {
b := &bytes.Buffer{}
l := NewLogfmtLogger(b, Panic)
if err := IfErrWithKeysAndReturn(l, nil, Err, "test message"); err != nil {
t.Error("Expected an nil return value")
}
if b.String() != "" {
t.Error("Expected empty string")
}
testErr := errors.New("nope")
if err := IfErrWithKeysAndReturn(l, testErr, Err, "test message"); err != testErr {
t.Error("Expected error to equal ")
}
if b.String() == "" {
t.Error("Expected error result")
}
}

func TestIfErrWithKeys(t *testing.T) {
b := &bytes.Buffer{}
l := NewLogfmtLogger(b, Panic)
IfErrWithKeys(l, nil, Err, "test message")
if b.String() != "" {
t.Error("Expected empty string")
}
testErr := errors.New("nope")
IfErrWithKeys(l, testErr, Err, "test message")
if b.String() == "" {
t.Error("Expected error result")
}
}

func TestChannelLoggerRace(t *testing.T) {
fullyVerifyLogger(t, func() (Logger, *bytes.Buffer) {
return NewChannelLogger(10, Discard), nil
Expand Down
34 changes: 34 additions & 0 deletions vendor/github.com/jtolds/gls/stack_tags.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit ed6b82f

Please sign in to comment.