From 93705ce51ebe08280485d13b615016ec0085e6ea Mon Sep 17 00:00:00 2001 From: JP Robinson Date: Tue, 16 Jul 2019 15:54:24 -0400 Subject: [PATCH] Adding log levels to kit/server.Log*Msg funcs (#220) * adding log levels to kit/server.Log*Msg funcs. resolves #13 * adding warn and debug loggers, updating docs * adding tests --- server/kit/kitserver_test.go | 34 +++++++++++++++++++++++++++++++++- server/kit/log.go | 21 +++++++++++++++++++-- 2 files changed, 52 insertions(+), 3 deletions(-) diff --git a/server/kit/kitserver_test.go b/server/kit/kitserver_test.go index 4d48e14bb..d3ae30f0b 100644 --- a/server/kit/kitserver_test.go +++ b/server/kit/kitserver_test.go @@ -13,6 +13,7 @@ import ( "github.com/go-kit/kit/endpoint" httptransport "github.com/go-kit/kit/transport/http" + "github.com/pkg/errors" ocontext "golang.org/x/net/context" "google.golang.org/grpc" @@ -52,6 +53,22 @@ func TestKitServerHTTPMiddleware(t *testing.T) { } } +func TestKitServerHTTPError(t *testing.T) { + svr := kit.NewServer(&server{}) + + r := httptest.NewRequest(http.MethodGet, "http://localhost:8080/svc/error", nil) + w := httptest.NewRecorder() + + // hit the server, expect error + svr.ServeHTTP(w, r) + + resp := w.Result() + + if resp.StatusCode != http.StatusInternalServerError { + t.Errorf("expected status code of 500, got %d", resp.StatusCode) + } +} + func TestKitServer(t *testing.T) { shutdownErrChan := make(chan error) go func() { @@ -134,7 +151,13 @@ type server struct{} func (s *server) Middleware(e endpoint.Endpoint) endpoint.Endpoint { return endpoint.Endpoint(func(ctx context.Context, r interface{}) (interface{}, error) { kit.LogMsg(ctx, "kit middleware!") - return e(ctx, r) + kit.LogDebug(ctx, "debug: kit middleware!") + res, err := e(ctx, r) + if err != nil { + kit.LogWarning(ctx, "error found in middleware") + kit.LogErrorMsg(ctx, err, "the actual error") + } + return res, err }) } @@ -158,6 +181,11 @@ func (s *server) HTTPEndpoints() map[string]map[string]kit.HTTPEndpoint { Decoder: catNameDecoder, }, }, + "/svc/error": { + "GET": { + Endpoint: s.error, + }, + }, } } @@ -197,3 +225,7 @@ func (s *server) getCatByName(ctx context.Context, _ interface{}) (interface{}, kit.Logger(ctx).Log("message", "getting ziggy") return testCat, nil } + +func (s *server) error(ctx context.Context, _ interface{}) (interface{}, error) { + return nil, errors.New("doh!") +} diff --git a/server/kit/log.go b/server/kit/log.go index 9a990df49..999927f11 100644 --- a/server/kit/log.go +++ b/server/kit/log.go @@ -6,6 +6,7 @@ import ( "github.com/NYTimes/gizmo/observe" "github.com/go-kit/kit/log" + "github.com/go-kit/kit/log/level" "github.com/go-kit/kit/transport/http" "google.golang.org/grpc/metadata" ) @@ -98,12 +99,28 @@ func AddLogKeyVals(ctx context.Context, l log.Logger) log.Logger { // LogMsg will log the given message to the server logger // with the key "message" along with all the common request headers or gRPC metadata. +// This message will have an "info" log level associated with it. func LogMsg(ctx context.Context, message string) error { - return Logger(ctx).Log("message", message) + return level.Info(Logger(ctx)).Log("message", message) +} + +// LogDebug will log the given message to the server logger +// with the key "message" along with all the common request headers or gRPC metadata. +// This message will have a "debug" log level associated with it. +func LogDebug(ctx context.Context, message string) error { + return level.Debug(Logger(ctx)).Log("message", message) +} + +// LogWarning will log the given message to the server logger +// with the key "message" along with all the common request headers or gRPC metadata. +// This message will have a "warn" log level associated with it. +func LogWarning(ctx context.Context, message string) error { + return level.Warn(Logger(ctx)).Log("message", message) } // LogErrorMsg will log the given error under the key "error", the given message under // the key "message" along with all the common request headers or gRPC metadata. +// This message will have an "error" log level associated with it. func LogErrorMsg(ctx context.Context, err error, message string) error { - return Logger(ctx).Log("error", err, "message", message) + return level.Error(Logger(ctx)).Log("error", err, "message", message) }