Skip to content

Commit

Permalink
🔄 Sync from monorepo
Browse files Browse the repository at this point in the history
  • Loading branch information
mojo-machine[bot] committed Nov 23, 2023
1 parent fe1abbe commit 2dfac92
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 16 deletions.
4 changes: 3 additions & 1 deletion lib/crpc/example/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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),
Expand Down
3 changes: 2 additions & 1 deletion lib/crpc/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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
}

Expand Down
37 changes: 23 additions & 14 deletions lib/middleware/request/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
})
}
}
Expand All @@ -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
}

0 comments on commit 2dfac92

Please sign in to comment.