madeit so that for flog.Error(), flog.Errorf(), flog.Errorln() that anything that fits an error that is passed at a parameter gets collected into the "~errors" key in the context. the thing under the "~errors" key is a []error.

master
Charles Iliya Krempeaux 2016-07-27 14:51:04 -07:00
parent 1a63335498
commit 2e14e0b46d
2 changed files with 239 additions and 3 deletions

View File

@ -17,14 +17,18 @@ func (flogger *internalFlogger) Error(v ...interface{}) {
msg := fmt.Sprint(v...) msg := fmt.Sprint(v...)
flogger.route(msg, errorContext) errCtx := newErrorContext(errorContext, v...)
flogger.route(msg, errCtx)
} }
func (flogger *internalFlogger) Errorf(format string, v ...interface{}) { func (flogger *internalFlogger) Errorf(format string, v ...interface{}) {
msg := fmt.Sprintf(format, v...) msg := fmt.Sprintf(format, v...)
flogger.route(msg, errorContext) errCtx := newErrorContext(errorContext, v...)
flogger.route(msg, errCtx)
} }
func (flogger *internalFlogger) Errorfe(err error, format string, v ...interface{}) { func (flogger *internalFlogger) Errorfe(err error, format string, v ...interface{}) {
@ -44,5 +48,39 @@ func (flogger *internalFlogger) Errorln(v ...interface{}) {
msg := fmt.Sprintln(v...) msg := fmt.Sprintln(v...)
flogger.route(msg, errorContext) errCtx := newErrorContext(errorContext, v...)
flogger.route(msg, errCtx)
}
func newErrorContext(baseContext map[string]interface{}, v ...interface{}) map[string]interface{} {
// Collect any errors.
errs := []error{}
for _, datum := range v {
if err, ok := datum.(error); ok {
errs = append(errs, err)
}
}
if 0 == len(errs) {
return baseContext
}
// Copy the base context.
context := map[string]interface{}{}
for k,v := range baseContext {
context[k] = v
}
// Put the collected errors in this new context.
context["~errors"] = errs
return context
} }

198
error_test.go 100644
View File

@ -0,0 +1,198 @@
package flog
import (
"errors"
"reflect"
"testing"
)
func TestNewErrorContext(t *testing.T) {
err1 := errors.New("error 1")
err2 := errors.New("error 2")
err3 := errors.New("error 3")
err4 := errors.New("error 4")
err5 := errors.New("error 5")
tests := []struct{
BaseContext map[string]interface{}
V []interface{}
Expected map[string]interface{}
}{
{
BaseContext: map[string]interface{}{
"~type":"error",
},
V: []interface{}{
// Nothing here.
},
Expected: map[string]interface{}{
"~type":"error",
},
},
{
BaseContext: map[string]interface{}{
"~type":"error",
},
V: []interface{}{
1,
},
Expected: map[string]interface{}{
"~type":"error",
},
},
{
BaseContext: map[string]interface{}{
"~type":"error",
},
V: []interface{}{
1, "two",
},
Expected: map[string]interface{}{
"~type":"error",
},
},
{
BaseContext: map[string]interface{}{
"~type":"error",
},
V: []interface{}{
1, "two", 3.0,
},
Expected: map[string]interface{}{
"~type":"error",
},
},
{
BaseContext: map[string]interface{}{
"~type":"error",
},
V: []interface{}{
1, "two", 3.0,
},
Expected: map[string]interface{}{
"~type":"error",
},
},
{
BaseContext: map[string]interface{}{
"~type":"error",
},
V: []interface{}{
err1,
},
Expected: map[string]interface{}{
"~type":"error",
"~errors": []error{
err1,
},
},
},
{
BaseContext: map[string]interface{}{
"~type":"error",
},
V: []interface{}{
err1, err2,
},
Expected: map[string]interface{}{
"~type":"error",
"~errors": []error{
err1, err2,
},
},
},
{
BaseContext: map[string]interface{}{
"~type":"error",
},
V: []interface{}{
err1, err2, err3,
},
Expected: map[string]interface{}{
"~type":"error",
"~errors": []error{
err1, err2, err3,
},
},
},
{
BaseContext: map[string]interface{}{
"~type":"error",
},
V: []interface{}{
err1, err2, err3, err4,
},
Expected: map[string]interface{}{
"~type":"error",
"~errors": []error{
err1, err2, err3, err4,
},
},
},
{
BaseContext: map[string]interface{}{
"~type":"error",
},
V: []interface{}{
err1, err2, err3, err4, err5,
},
Expected: map[string]interface{}{
"~type":"error",
"~errors": []error{
err1, err2, err3, err4, err5,
},
},
},
{
BaseContext: map[string]interface{}{
"~type":"error",
},
V: []interface{}{
1, err1, "two", err2, 3.0, err3, '4',
},
Expected: map[string]interface{}{
"~type":"error",
"~errors": []error{
err1, err2, err3,
},
},
},
}
for testNumber, test := range tests {
actualContext := newErrorContext(test.BaseContext, test.V...)
if actual := actualContext; nil == actual {
t.Errorf("For test #%d, did not expected nil, but actually got %v", testNumber, actual)
continue
}
if expected, actual := len(test.Expected), len(actualContext); expected != actual {
t.Errorf("For test #%d, expected %d, but actually got %d.", testNumber, expected, actual)
continue
}
if expected, actual := test.Expected, actualContext; !reflect.DeepEqual(expected, actual) {
t.Errorf("For test #%d, expected...\n%#v\nbut actually got...\n%#v", testNumber, expected, actual)
continue
}
}
}