diff options
author | Wolfy-J <[email protected]> | 2020-10-29 11:40:25 +0300 |
---|---|---|
committer | Wolfy-J <[email protected]> | 2020-10-29 11:40:25 +0300 |
commit | d7ad25092813234357855545bdef2c0f85f3e1c0 (patch) | |
tree | 617a3715418e3986ce59cfe1bc1af9af1205a5d5 | |
parent | de53c3ad12a8afb379610f87399373c4d0626ef6 (diff) |
- collect app logs into single channel
-rw-r--r-- | plugins/app/app.go | 32 |
1 files changed, 20 insertions, 12 deletions
diff --git a/plugins/app/app.go b/plugins/app/app.go index ebb42631..9301e38f 100644 --- a/plugins/app/app.go +++ b/plugins/app/app.go @@ -4,12 +4,10 @@ import ( "context" "fmt" "go.uber.org/zap" - "log" "os" "os/exec" "strings" - "github.com/fatih/color" "github.com/spiral/endure/errors" "github.com/spiral/roadrunner/v2" "github.com/spiral/roadrunner/v2/plugins/config" @@ -60,8 +58,6 @@ func (app *App) Serve() chan error { errCh <- errors.E(errors.Op("init factory"), err) } - app.log.Info("Started worker factory", zap.Any("relay", app.cfg.Relay), zap.Any("command", app.cfg.Command)) - return errCh } @@ -106,7 +102,14 @@ func (app *App) NewWorker(ctx context.Context, env Env) (roadrunner.WorkerBase, return nil, err } - return app.factory.SpawnWorkerWithContext(ctx, spawnCmd()) + w, err := app.factory.SpawnWorkerWithContext(ctx, spawnCmd()) + if err != nil { + return nil, err + } + + w.AddListener(app.collectLogs) + + return w, nil } // NewWorkerPool issues new worker pool. @@ -121,13 +124,7 @@ func (app *App) NewWorkerPool(ctx context.Context, opt roadrunner.Config, env En return nil, err } - p.AddListener(func(event interface{}) { - if we, ok := event.(roadrunner.WorkerEvent); ok { - if we.Event == roadrunner.EventWorkerLog { - log.Print(color.YellowString(string(we.Payload.([]byte)))) - } - } - }) + p.AddListener(app.collectLogs) return p, nil } @@ -167,3 +164,14 @@ func (app *App) setEnv(e Env) []string { return env } + +func (app *App) collectLogs(event interface{}) { + if we, ok := event.(roadrunner.WorkerEvent); ok { + switch we.Event { + case roadrunner.EventWorkerError: + app.log.Error(we.Payload.(error).Error(), zap.Int64("pid", we.Worker.Pid())) + case roadrunner.EventWorkerLog: + app.log.Debug(strings.TrimRight(string(we.Payload.([]byte)), " \n\t"), zap.Int64("pid", we.Worker.Pid())) + } + } +} |