From 47c878b9c489d103fe433733ebe268f77e57f861 Mon Sep 17 00:00:00 2001 From: Chung-Ting Huang Date: Mon, 28 Jan 2019 14:05:59 -0600 Subject: [PATCH] TUN-1419: Identify request/response headers/content length with ray ID --- cmd/cloudflared/tunnel/cmd.go | 2 +- cmd/cloudflared/tunnel/logger.go | 6 ++++++ origin/tunnel.go | 32 ++++++++++++++++++++++++-------- 3 files changed, 31 insertions(+), 9 deletions(-) diff --git a/cmd/cloudflared/tunnel/cmd.go b/cmd/cloudflared/tunnel/cmd.go index 1a975bec..61ce6c83 100644 --- a/cmd/cloudflared/tunnel/cmd.go +++ b/cmd/cloudflared/tunnel/cmd.go @@ -534,7 +534,7 @@ func tunnelFlags(shouldHide bool) []cli.Flag { altsrc.NewStringFlag(&cli.StringFlag{ Name: "loglevel", Value: "info", - Usage: "Application logging level {panic, fatal, error, warn, info, debug}", + Usage: "Application logging level {panic, fatal, error, warn, info, debug}. " + debugLevelWarning, EnvVars: []string{"TUNNEL_LOGLEVEL"}, Hidden: shouldHide, }), diff --git a/cmd/cloudflared/tunnel/logger.go b/cmd/cloudflared/tunnel/logger.go index cb28af97..cc92001c 100644 --- a/cmd/cloudflared/tunnel/logger.go +++ b/cmd/cloudflared/tunnel/logger.go @@ -13,6 +13,9 @@ import ( "github.com/pkg/errors" ) +const debugLevelWarning = "At debug level, request URL, method, protocol, content legnth and header will be logged. " + + "Response status, content length and header will also be logged in debug level." + var logger = log.CreateLogger() func configMainLogger(c *cli.Context) error { @@ -22,6 +25,9 @@ func configMainLogger(c *cli.Context) error { return errors.Wrap(err, "Unknown logging level specified") } logger.SetLevel(logLevel) + if logLevel == logrus.DebugLevel { + logger.Warn(debugLevelWarning) + } return nil } diff --git a/origin/tunnel.go b/origin/tunnel.go index 67cc30ad..084455ac 100644 --- a/origin/tunnel.go +++ b/origin/tunnel.go @@ -636,25 +636,41 @@ func (h *TunnelHandler) logError(stream *h2mux.MuxedStream, err error) { } func (h *TunnelHandler) logRequest(req *http.Request, cfRay string, lbProbe bool) { + logger := log.NewEntry(h.logger) if cfRay != "" { - h.logger.WithField("CF-RAY", cfRay).Debugf("%s %s %s", req.Method, req.URL, req.Proto) + logger = logger.WithField("CF-RAY", cfRay) + logger.Debugf("%s %s %s", req.Method, req.URL, req.Proto) } else if lbProbe { - h.logger.Debugf("Load Balancer health check %s %s %s", req.Method, req.URL, req.Proto) + logger.Debugf("Load Balancer health check %s %s %s", req.Method, req.URL, req.Proto) } else { - h.logger.Warnf("All requests should have a CF-RAY header. Please open a support ticket with Cloudflare. %s %s %s ", req.Method, req.URL, req.Proto) + logger.Warnf("All requests should have a CF-RAY header. Please open a support ticket with Cloudflare. %s %s %s ", req.Method, req.URL, req.Proto) + } + logger.Debugf("Request Headers %+v", req.Header) + + if contentLen := req.ContentLength; contentLen == -1 { + logger.Debugf("Request Content length unknown") + } else { + logger.Debugf("Request content length %d", contentLen) } - h.logger.Debugf("Request Headers %+v", req.Header) } func (h *TunnelHandler) logResponse(r *http.Response, cfRay string, lbProbe bool) { + logger := log.NewEntry(h.logger) if cfRay != "" { - h.logger.WithField("CF-RAY", cfRay).Debugf("%s", r.Status) + logger = logger.WithField("CF-RAY", cfRay) + logger.Debugf("%s", r.Status) } else if lbProbe { - h.logger.Debugf("Response to Load Balancer health check %s", r.Status) + logger.Debugf("Response to Load Balancer health check %s", r.Status) } else { - h.logger.Infof("%s", r.Status) + logger.Infof("%s", r.Status) + } + logger.Debugf("Response Headers %+v", r.Header) + + if contentLen := r.ContentLength; contentLen == -1 { + logger.Debugf("Response content length unknown") + } else { + logger.Debugf("Response content length %d", contentLen) } - h.logger.Debugf("Response Headers %+v", r.Header) } func (h *TunnelHandler) UpdateMetrics(connectionID string) {