mirror of
https://github.com/cloudflare/cloudflared.git
synced 2025-07-27 17:29:58 +00:00
TUN-3470: Replace in-house logger calls with zerolog
This commit is contained in:

committed by
Adam Chalmers

parent
06404bf3e8
commit
870f5fa907
@@ -3,25 +3,25 @@ package tunnelrpc
|
||||
import (
|
||||
"context"
|
||||
|
||||
"github.com/cloudflare/cloudflared/logger"
|
||||
"github.com/rs/zerolog"
|
||||
"golang.org/x/net/trace"
|
||||
"zombiezen.com/go/capnproto2/rpc"
|
||||
)
|
||||
|
||||
// ConnLogger wraps a logrus *log.Entry for a connection.
|
||||
// ConnLogger wraps a Zerolog Logger for a connection.
|
||||
type ConnLogger struct {
|
||||
Entry logger.Service
|
||||
Log *zerolog.Logger
|
||||
}
|
||||
|
||||
func (c ConnLogger) Infof(ctx context.Context, format string, args ...interface{}) {
|
||||
c.Entry.Infof(format, args...)
|
||||
c.Log.Info().Msgf(format, args...)
|
||||
}
|
||||
|
||||
func (c ConnLogger) Errorf(ctx context.Context, format string, args ...interface{}) {
|
||||
c.Entry.Errorf(format, args...)
|
||||
c.Log.Error().Msgf(format, args...)
|
||||
}
|
||||
|
||||
func ConnLog(log logger.Service) rpc.ConnOption {
|
||||
func ConnLog(log *zerolog.Logger) rpc.ConnOption {
|
||||
return rpc.ConnLog(ConnLogger{log})
|
||||
}
|
||||
|
||||
|
@@ -5,7 +5,7 @@ import (
|
||||
"bytes"
|
||||
"context"
|
||||
|
||||
"github.com/cloudflare/cloudflared/logger"
|
||||
"github.com/rs/zerolog"
|
||||
"zombiezen.com/go/capnproto2/encoding/text"
|
||||
"zombiezen.com/go/capnproto2/rpc"
|
||||
rpccapnp "zombiezen.com/go/capnproto2/std/capnp/rpc"
|
||||
@@ -13,33 +13,33 @@ import (
|
||||
|
||||
type transport struct {
|
||||
rpc.Transport
|
||||
l logger.Service
|
||||
log *zerolog.Logger
|
||||
}
|
||||
|
||||
// NewTransportLogger creates a new logger that proxies messages to and from t and
|
||||
// logs them to l. If l is nil, then the log package's default
|
||||
// logs them to log. If log is nil, then the log package's default
|
||||
// logger is used.
|
||||
func NewTransportLogger(l logger.Service, t rpc.Transport) rpc.Transport {
|
||||
return &transport{Transport: t, l: l}
|
||||
func NewTransportLogger(log *zerolog.Logger, t rpc.Transport) rpc.Transport {
|
||||
return &transport{Transport: t, log: log}
|
||||
}
|
||||
|
||||
func (t *transport) SendMessage(ctx context.Context, msg rpccapnp.Message) error {
|
||||
t.l.Debugf("rpcconnect: tx %s", formatMsg(msg))
|
||||
t.log.Debug().Msgf("rpcconnect: tx %s", formatMsg(msg))
|
||||
return t.Transport.SendMessage(ctx, msg)
|
||||
}
|
||||
|
||||
func (t *transport) RecvMessage(ctx context.Context) (rpccapnp.Message, error) {
|
||||
msg, err := t.Transport.RecvMessage(ctx)
|
||||
if err != nil {
|
||||
t.l.Debugf("rpcconnect: rx error: %s", err)
|
||||
t.log.Debug().Msgf("rpcconnect: rx error: %s", err)
|
||||
return msg, err
|
||||
}
|
||||
t.l.Debugf("rpcconnect: rx %s", formatMsg(msg))
|
||||
t.log.Debug().Msgf("rpcconnect: rx %s", formatMsg(msg))
|
||||
return msg, nil
|
||||
}
|
||||
|
||||
func formatMsg(m rpccapnp.Message) string {
|
||||
var buf bytes.Buffer
|
||||
text.NewEncoder(&buf).Encode(0x91b79f1f808db032, m.Struct)
|
||||
_ = text.NewEncoder(&buf).Encode(0x91b79f1f808db032, m.Struct)
|
||||
return buf.String()
|
||||
}
|
||||
|
@@ -102,14 +102,14 @@ func TestWhenToRefresh(t *testing.T) {
|
||||
func TestSerializeAuthenticationResponse(t *testing.T) {
|
||||
|
||||
tests := []*AuthenticateResponse{
|
||||
&AuthenticateResponse{
|
||||
{
|
||||
Jwt: []byte("\xbd\xb2\x3d\xbc\x20\xe2\x8c\x98"),
|
||||
HoursUntilRefresh: 24,
|
||||
},
|
||||
&AuthenticateResponse{
|
||||
{
|
||||
PermanentErr: "bad auth",
|
||||
},
|
||||
&AuthenticateResponse{
|
||||
{
|
||||
RetryableErr: "bad connection",
|
||||
HoursUntilRefresh: 24,
|
||||
},
|
||||
|
Reference in New Issue
Block a user