Better logging

This commit is contained in:
Alexander Kiryukhin 2022-06-30 03:07:26 +03:00
parent c39fcbc6a1
commit 058331685c
No known key found for this signature in database
GPG key ID: B0DA6283C40CB2CB
2 changed files with 8 additions and 3 deletions

1
go.sum
View file

@ -4,6 +4,7 @@ github.com/qri-io/jsonpointer v0.1.1 h1:prVZBZLL6TW5vsSB9fFHFAMBLI4b0ri5vribQlTJ
github.com/qri-io/jsonpointer v0.1.1/go.mod h1:DnJPaYgiKu56EuDp8TU5wFLdZIcAnb/uH9v37ZaMV64= github.com/qri-io/jsonpointer v0.1.1/go.mod h1:DnJPaYgiKu56EuDp8TU5wFLdZIcAnb/uH9v37ZaMV64=
github.com/qri-io/jsonschema v0.2.1 h1:NNFoKms+kut6ABPf6xiKNM5214jzxAhDBrPHCJ97Wg0= github.com/qri-io/jsonschema v0.2.1 h1:NNFoKms+kut6ABPf6xiKNM5214jzxAhDBrPHCJ97Wg0=
github.com/qri-io/jsonschema v0.2.1/go.mod h1:g7DPkiOsK1xv6T/Ao5scXRkd+yTFygcANPBaaqW+VrI= github.com/qri-io/jsonschema v0.2.1/go.mod h1:g7DPkiOsK1xv6T/Ao5scXRkd+yTFygcANPBaaqW+VrI=
github.com/sergi/go-diff v1.0.0 h1:Kpca3qRNrduNnOQeazBd0ysaKrUJiIuISHxogkT9RPQ=
github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo= github.com/sergi/go-diff v1.0.0/go.mod h1:0CfEIISq7TuYL3j771MWULgwwjU+GofnZX9QAmXWZgo=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI=

View file

@ -21,7 +21,8 @@ package middleware
import ( import (
"context" "context"
"strings" "encoding/json"
"fmt"
"time" "time"
"go.neonxp.dev/jsonrpc2/rpc" "go.neonxp.dev/jsonrpc2/rpc"
@ -33,8 +34,11 @@ func Logger(logger rpc.Logger) rpc.Middleware {
t1 := time.Now().UnixMicro() t1 := time.Now().UnixMicro()
resp := handler(ctx, req) resp := handler(ctx, req)
t2 := time.Now().UnixMicro() t2 := time.Now().UnixMicro()
args := strings.ReplaceAll(string(req.Params), "\n", "") var params any
logger.Logf("rpc call=%s, args=%s, take=%dμs", req.Method, args, (t2 - t1)) if err := json.Unmarshal(req.Params, &params); err != nil {
params = fmt.Sprintf("<invalid body: %s>", err.Error())
}
logger.Logf("rpc call=%s, args=%+v, take=%dμs", req.Method, params, (t2 - t1))
return resp return resp
} }
} }