From 2dfac9262e363c3013753a6fe700a3f0eb4740c8 Mon Sep 17 00:00:00 2001 From: "mojo-machine[bot]" <111131124+mojo-machine[bot]@users.noreply.github.com> Date: Thu, 23 Nov 2023 15:01:43 +0000 Subject: [PATCH] =?UTF-8?q?=F0=9F=94=84=20Sync=20from=20monorepo?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit https://github.com/wearemojo/mojo/commit/383968ea203969cbed22e28dd0854ecf1c4660b8 --- lib/crpc/example/server/server.go | 4 +++- lib/crpc/middleware.go | 3 ++- lib/middleware/request/logger.go | 37 +++++++++++++++++++------------ 3 files changed, 28 insertions(+), 16 deletions(-) diff --git a/lib/crpc/example/server/server.go b/lib/crpc/example/server/server.go index 6d4a20c..6d3d6b0 100644 --- a/lib/crpc/example/server/server.go +++ b/lib/crpc/example/server/server.go @@ -11,7 +11,9 @@ import ( "github.com/wearemojo/mojo-public-go/lib/config" "github.com/wearemojo/mojo-public-go/lib/crpc" "github.com/wearemojo/mojo-public-go/lib/crpc/example" + "github.com/wearemojo/mojo-public-go/lib/merr" "github.com/wearemojo/mojo-public-go/lib/middleware/request" + "github.com/wearemojo/mojo-public-go/lib/mlog" ) type ServerConfig struct { @@ -27,7 +29,7 @@ func (es *ExampleServer) Ping(ctx context.Context) error { } func (es *ExampleServer) Greet(ctx context.Context, req *example.GreetRequest) (*example.GreetResponse, error) { - clog.Get(ctx).Info("just an example") + mlog.Info(ctx, merr.New(ctx, "example_log", merr.M{"name": req.Name})) return &example.GreetResponse{ Greeting: fmt.Sprintf("Hello %s!", req.Name), diff --git a/lib/crpc/middleware.go b/lib/crpc/middleware.go index 39ac203..6f669e9 100644 --- a/lib/crpc/middleware.go +++ b/lib/crpc/middleware.go @@ -13,6 +13,7 @@ import ( "github.com/wearemojo/mojo-public-go/lib/clog" "github.com/wearemojo/mojo-public-go/lib/gerrors" "github.com/wearemojo/mojo-public-go/lib/merr" + "github.com/wearemojo/mojo-public-go/lib/mlog" "github.com/wearemojo/mojo-public-go/lib/slicefn" "github.com/xeipuuv/gojsonschema" ) @@ -68,7 +69,7 @@ func Validate(schema *gojsonschema.Schema) MiddlewareFunc { body, err := io.ReadAll(req.Body) if err != nil { if netErr, ok := gerrors.As[net.Error](err); ok { - clog.Get(req.Context()).WithError(netErr).Warn("network error reading request body") + mlog.Warn(ctx, merr.New(ctx, "request_body_read_network_error", nil, netErr)) return io.ErrUnexpectedEOF } diff --git a/lib/middleware/request/logger.go b/lib/middleware/request/logger.go index 4401006..fbbcd91 100644 --- a/lib/middleware/request/logger.go +++ b/lib/middleware/request/logger.go @@ -6,6 +6,8 @@ import ( "github.com/sirupsen/logrus" "github.com/wearemojo/mojo-public-go/lib/clog" + "github.com/wearemojo/mojo-public-go/lib/merr" + "github.com/wearemojo/mojo-public-go/lib/mlog" ) type responseWriter struct { @@ -87,10 +89,27 @@ func Logger(log *logrus.Entry) func(http.Handler) http.Handler { "http_response_bytes": res.Bytes, }) - logger := clog.Get(ctx) - - err := getError(logger) - logger.Log(determineLevel(err, clog.TimeoutsAsErrors(ctx)), "request") + fn := mlog.Info + err := getError(clog.Get(ctx)) + if err != nil { + switch clog.DetermineLevel(err, clog.TimeoutsAsErrors(ctx)) { + case + logrus.PanicLevel, + logrus.FatalLevel, + logrus.ErrorLevel: + fn = mlog.Error + case + logrus.WarnLevel: + fn = mlog.Warn + case + logrus.InfoLevel, + logrus.DebugLevel, + logrus.TraceLevel: + fn = mlog.Info + } + } + + fn(ctx, merr.New(ctx, "request", nil)) }) } } @@ -105,13 +124,3 @@ func getError(l *logrus.Entry) error { return nil } - -// determineLevel returns a suggested logrus Level type based whether an error is present and what type -func determineLevel(err error, timeoutsAsErrors bool) logrus.Level { - if err != nil { - return clog.DetermineLevel(err, timeoutsAsErrors) - } - - // no error, default to info level - return logrus.InfoLevel -}