Skip to content

Commit

Permalink
Merge 8530bdf into 74213ad
Browse files Browse the repository at this point in the history
  • Loading branch information
jprobinson committed Sep 9, 2019
2 parents 74213ad + 8530bdf commit c662309
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
4 changes: 4 additions & 0 deletions server/kit/kitserver_test.go
Expand Up @@ -155,7 +155,10 @@ func (s *server) Middleware(e endpoint.Endpoint) endpoint.Endpoint {
res, err := e(ctx, r)
if err != nil {
kit.LogWarning(ctx, "error found in middleware")
kit.LogWarningf(ctx, "error found in middleware: %v", err)
kit.LogErrorMsg(ctx, err, "the actual error")
kit.LogErrorf(ctx, "the actual error: %v", err)
kit.LogDebugf(ctx, "debug: error in middleware: %v", err)
}
return res, err
})
Expand Down Expand Up @@ -223,6 +226,7 @@ var testCat = &Cat{Breed: "American Shorthair", Name: "Ziggy", Age: 12}
// shared business layer
func (s *server) getCatByName(ctx context.Context, _ interface{}) (interface{}, error) {
kit.Logger(ctx).Log("message", "getting ziggy")
kit.Logf(ctx, "responding with ziggy: %#v", testCat)
return testCat, nil
}

Expand Down
36 changes: 36 additions & 0 deletions server/kit/log.go
Expand Up @@ -2,6 +2,7 @@ package kit

import (
"context"
"fmt"
"os"

"github.com/NYTimes/gizmo/observe"
Expand Down Expand Up @@ -104,23 +105,58 @@ func LogMsg(ctx context.Context, message string) error {
return level.Info(Logger(ctx)).Log("message", message)
}

// Logf will format the given string with the arguments then log to the server logger
// with the key "message" along with all the common request headers or gRPC metadata.
// Arguments are handlered in the manner of fmt.Printf.
// This message will have an "info" log level associated with it.
func Logf(ctx context.Context, format string, v ...interface{}) error {
return LogMsg(ctx, fmt.Sprintf(format, v...))
}

// 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)
}

// LogDebugf will format the given string with the arguments then log to the server
// logger with the key "message" along with all the common request headers or gRPC
// metadata.
// Arguments are handlered in the manner of fmt.Printf.
// This message will have a "debug" log level associated with it.
func LogDebugf(ctx context.Context, format string, v ...interface{}) error {
return LogDebug(ctx, fmt.Sprintf(format, v...))
}

// 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)
}

// LogWarningf will the format given string with the arguments then log to the server
// logger with the key "message" along with all the common request headers or gRPC
// metadata.
// Arguments are handlered in the manner of fmt.Printf.
// This message will have a "warn" log level associated with it.
func LogWarningf(ctx context.Context, format string, v ...interface{}) error {
return LogWarning(ctx, fmt.Sprintf(format, v...))
}

// 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 level.Error(Logger(ctx)).Log("error", err, "message", message)
}

// LogErrorf will format the given string with the arguments then log to the server
// logger with the key "message" along with all the common request headers or gRPC
// metadata.
// Arguments are handlered in the manner of fmt.Printf.
// This message will have a "warn" log level associated with it.
func LogErrorf(ctx context.Context, format string, v ...interface{}) error {
return level.Error(Logger(ctx)).Log("message", fmt.Sprintf(format, v...))
}

0 comments on commit c662309

Please sign in to comment.