Skip to content

Commit

Permalink
Adding log levels to kit/server.Log*Msg funcs (#220)
Browse files Browse the repository at this point in the history
* adding log levels to kit/server.Log*Msg funcs. resolves #13

* adding warn and debug loggers, updating docs

* adding tests
  • Loading branch information
jprobinson committed Jul 16, 2019
1 parent 4dd11b5 commit 93705ce
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
34 changes: 33 additions & 1 deletion server/kit/kitserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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
})
}

Expand All @@ -158,6 +181,11 @@ func (s *server) HTTPEndpoints() map[string]map[string]kit.HTTPEndpoint {
Decoder: catNameDecoder,
},
},
"/svc/error": {
"GET": {
Endpoint: s.error,
},
},
}
}

Expand Down Expand Up @@ -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!")
}
21 changes: 19 additions & 2 deletions server/kit/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
}

0 comments on commit 93705ce

Please sign in to comment.