diff options
author | Wolfy-J <[email protected]> | 2018-09-29 23:37:16 +0300 |
---|---|---|
committer | GitHub <[email protected]> | 2018-09-29 23:37:16 +0300 |
commit | 6122fca108c20984732c969fb1ba53cce5b3c44a (patch) | |
tree | 40835f46a5c208ea2546b76e3bd9fa05429b405a /cmd/rr/http/debug.go | |
parent | abe62c0675f839586312cff1c83d6a4cb31dd9d5 (diff) | |
parent | a04b5b33eb30944007973067ec07e9c4a2c464ab (diff) |
Merge pull request #39 from spiral/feature/1.3.0v1.2.3
Feature/1.3.0
Diffstat (limited to 'cmd/rr/http/debug.go')
-rw-r--r-- | cmd/rr/http/debug.go | 84 |
1 files changed, 77 insertions, 7 deletions
diff --git a/cmd/rr/http/debug.go b/cmd/rr/http/debug.go index f69e10a8..53980303 100644 --- a/cmd/rr/http/debug.go +++ b/cmd/rr/http/debug.go @@ -1,20 +1,90 @@ package http import ( - rr "github.com/spiral/roadrunner/cmd/rr/cmd" - + "fmt" + "github.com/sirupsen/logrus" "github.com/spf13/cobra" - "github.com/spiral/roadrunner/cmd/rr/debug" - "github.com/spiral/roadrunner/service/http" + "github.com/spiral/roadrunner" + rr "github.com/spiral/roadrunner/cmd/rr/cmd" + "github.com/spiral/roadrunner/cmd/util" + rrhttp "github.com/spiral/roadrunner/service/http" + "net/http" ) func init() { cobra.OnInitialize(func() { if rr.Debug { - svc, _ := rr.Container.Get(http.ID) - if svc, ok := svc.(*http.Service); ok { - svc.AddListener(debug.Listener(rr.Logger)) + svc, _ := rr.Container.Get(rrhttp.ID) + if svc, ok := svc.(*rrhttp.Service); ok { + svc.AddListener((&debugger{logger: rr.Logger}).listener) } } }) } + +// listener provide debug callback for system events. With colors! +type debugger struct{ logger *logrus.Logger } + +// listener listens to http events and generates nice looking output. +func (s *debugger) listener(event int, ctx interface{}) { + if util.LogEvent(s.logger, event, ctx) { + // handler by default debug package + return + } + + // http events + switch event { + case rrhttp.EventResponse: + e := ctx.(*rrhttp.ResponseEvent) + s.logger.Info(util.Sprintf( + "<cyan+h>%s</reset> %s <white+hb>%s</reset> %s", + e.Request.RemoteAddr, + statusColor(e.Response.Status), + e.Request.Method, + e.Request.URI, + )) + case rrhttp.EventError: + e := ctx.(*rrhttp.ErrorEvent) + + if _, ok := e.Error.(roadrunner.JobError); ok { + s.logger.Info(util.Sprintf( + "%s <white+hb>%s</reset> %s", + statusColor(500), + e.Request.Method, + uri(e.Request), + )) + } else { + s.logger.Info(util.Sprintf( + "%s <white+hb>%s</reset> %s <red>%s</reset>", + statusColor(500), + e.Request.Method, + uri(e.Request), + e.Error, + )) + } + } +} + +func statusColor(status int) string { + if status < 300 { + return util.Sprintf("<green>%v</reset>", status) + } + + if status < 400 { + return util.Sprintf("<cyan>%v</reset>", status) + } + + if status < 500 { + return util.Sprintf("<yellow>%v</reset>", status) + } + + return util.Sprintf("<red>%v</reset>", status) +} + +func uri(r *http.Request) string { + if r.TLS != nil { + return fmt.Sprintf("https://%s%s", r.Host, r.URL.String()) + } + + return fmt.Sprintf("http://%s%s", r.Host, r.URL.String()) +} |