diff --git a/server/kit/kitserver_test.go b/server/kit/kitserver_test.go index d3ae30f0b..117617fcd 100644 --- a/server/kit/kitserver_test.go +++ b/server/kit/kitserver_test.go @@ -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 }) @@ -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 } diff --git a/server/kit/log.go b/server/kit/log.go index 999927f11..0c9056faf 100644 --- a/server/kit/log.go +++ b/server/kit/log.go @@ -2,6 +2,7 @@ package kit import ( "context" + "fmt" "os" "github.com/NYTimes/gizmo/observe" @@ -104,6 +105,14 @@ 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. @@ -111,6 +120,15 @@ 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. @@ -118,9 +136,27 @@ 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...)) +}