summaryrefslogtreecommitdiff
path: root/cmd/rr/debug/debugger.go
diff options
context:
space:
mode:
authorWolfy-J <[email protected]>2018-06-15 15:54:14 +0300
committerWolfy-J <[email protected]>2018-06-15 15:54:14 +0300
commitdfef39c64132192d13e2315364a74f1b0244791e (patch)
treeb0959bbbb1ca5a16d94646454d7f2e9340081915 /cmd/rr/debug/debugger.go
parentccd297dbe2586b3fd29854f72f795be9e818522e (diff)
readme, samples, golint, build scripts
Diffstat (limited to 'cmd/rr/debug/debugger.go')
-rw-r--r--cmd/rr/debug/debugger.go87
1 files changed, 87 insertions, 0 deletions
diff --git a/cmd/rr/debug/debugger.go b/cmd/rr/debug/debugger.go
new file mode 100644
index 00000000..0621285b
--- /dev/null
+++ b/cmd/rr/debug/debugger.go
@@ -0,0 +1,87 @@
+package debug
+
+import (
+ "github.com/sirupsen/logrus"
+ "github.com/spiral/roadrunner"
+ "github.com/spiral/roadrunner/cmd/rr/utils"
+ "github.com/spiral/roadrunner/service/http"
+)
+
+// Listener creates new debug listener.
+func Listener(logger *logrus.Logger) func(event int, ctx interface{}) {
+ return (&debugger{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{}) {
+ // http events
+ switch event {
+ case http.EventResponse:
+ log := ctx.(*http.Event)
+ s.logger.Info(utils.Sprintf("%s <white+hb>%s</reset> %s", statusColor(log.Status), log.Method, log.URI))
+ case http.EventError:
+ log := ctx.(*http.Event)
+
+ if _, ok := log.Error.(roadrunner.JobError); ok {
+ s.logger.Info(utils.Sprintf("%s <white+hb>%s</reset> %s", statusColor(log.Status), log.Method, log.URI))
+ } else {
+ s.logger.Info(utils.Sprintf(
+ "%s <white+hb>%s</reset> %s <red>%s</reset>",
+ statusColor(log.Status),
+ log.Method,
+ log.URI,
+ log.Error,
+ ))
+ }
+ }
+
+ switch event {
+ case roadrunner.EventWorkerKill:
+ w := ctx.(*roadrunner.Worker)
+ s.logger.Warning(utils.Sprintf(
+ "<white+hb>worker.%v</reset> <yellow>killed</red>",
+ *w.Pid,
+ ))
+
+ case roadrunner.EventWorkerError:
+ err := ctx.(roadrunner.WorkerError)
+ s.logger.Error(utils.Sprintf(
+ "<white+hb>worker.%v</reset> <red>%s</reset>",
+ *err.Worker.Pid,
+ err.Caused,
+ ))
+ }
+
+ // rr server events
+ switch event {
+ case roadrunner.EventServerFailure:
+ s.logger.Error(utils.Sprintf("<red>server is dead</reset>"))
+ }
+
+ // pool events
+ switch event {
+ case roadrunner.EventPoolConstruct:
+ s.logger.Debug(utils.Sprintf("<cyan>new worker pool</reset>"))
+ case roadrunner.EventPoolError:
+ s.logger.Error(utils.Sprintf("<red>%s</reset>", ctx))
+ }
+}
+
+func statusColor(status int) string {
+ if status < 300 {
+ return utils.Sprintf("<green>%v</reset>", status)
+ }
+
+ if status < 400 {
+ return utils.Sprintf("<cyan>%v</reset>", status)
+ }
+
+ if status < 500 {
+ return utils.Sprintf("<yellow>%v</reset>", status)
+ }
+
+ return utils.Sprintf("<red>%v</reset>", status)
+}