Skip to content

Commit 588d4b0

Browse files
committed
Update xrpc gateway logger
1 parent 01c7eb6 commit 588d4b0

1 file changed

Lines changed: 15 additions & 2 deletions

File tree

rpcserver.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"google.golang.org/grpc/credentials"
1010
"google.golang.org/grpc/credentials/insecure"
1111
"google.golang.org/grpc/keepalive"
12+
"google.golang.org/grpc/status"
1213
"google.golang.org/protobuf/encoding/protojson"
1314
"net"
1415
"net/http"
@@ -148,13 +149,25 @@ func (t *RpcServer) Serve() error {
148149
DiscardUnknown: true,
149150
}
150151
}
151-
mux := runtime.NewServeMux(
152+
muxOpts := []runtime.ServeMuxOption{
152153
// Format for using proto names in json https://grpc-ecosystem.github.io/grpc-gateway/docs/mapping/customizing_your_gateway/#using-proto-names-in-json
153154
runtime.WithMarshalerOption(runtime.MIMEWildcard, &runtime.JSONPb{
154155
MarshalOptions: *marshalOptions,
155156
UnmarshalOptions: *unmarshalOptions,
156157
}),
157-
)
158+
}
159+
if t.Logger != nil {
160+
customHTTPError := func(ctx context.Context, mux *runtime.ServeMux, marshaler runtime.Marshaler, w http.ResponseWriter, r *http.Request, err error) {
161+
defer runtime.DefaultHTTPErrorHandler(ctx, mux, marshaler, w, r, err)
162+
if err == nil {
163+
return
164+
}
165+
st := status.Convert(err)
166+
t.Logger.Log(ctx, logging.LevelInfo, "gateway bad request", st.Code(), st.Message(), st.Details())
167+
}
168+
muxOpts = append(muxOpts, runtime.WithErrorHandler(customHTTPError))
169+
}
170+
mux := runtime.NewServeMux(muxOpts...)
158171
t.Gateway.Registrar(mux, conn)
159172
gateway := &http.Server{
160173
Addr: t.Gateway.Addr,

0 commit comments

Comments
 (0)