forked from monzo/orchestra
/
logs.go
55 lines (47 loc) · 1.19 KB
/
logs.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package commands
import (
"fmt"
"strings"
"sync"
"github.com/ActiveState/tail"
log "github.com/cihub/seelog"
"github.com/codegangsta/cli"
"github.com/vinceprignano/orchestra/services"
"github.com/wsxiaoys/terminal"
)
var LogsCommand = &cli.Command{
Name: "logs",
Usage: "Aggregate services logs",
Action: BeforeAfterWrapper(LogsAction),
BashComplete: ServicesBashComplete,
}
var logReceiver chan string
func init() {
logReceiver = make(chan string)
}
func LogsAction(c *cli.Context) {
go ConsumeLogs()
wg := &sync.WaitGroup{}
for _, service := range FilterServices(c) {
wg.Add(1)
go TailServiceLog(service, wg)
}
wg.Wait()
close(logReceiver)
}
func ConsumeLogs() {
for log := range logReceiver {
terminal.Stdout.Colorf(log)
}
}
func TailServiceLog(service *services.Service, wg *sync.WaitGroup) {
spacingLength := services.MaxServiceNameLength + 2 - len(service.Name)
t, err := tail.TailFile(service.LogFilePath, tail.Config{Follow: true})
if err != nil {
log.Error(err.Error())
}
for line := range t.Lines {
logReceiver <- fmt.Sprintf("@{%s}%s@{|}%s| %s\n", service.Color, service.Name, strings.Repeat(" ", spacingLength), line.Text)
}
wg.Done()
}