-
Notifications
You must be signed in to change notification settings - Fork 25
/
databoxLogParser.go
72 lines (61 loc) · 1.43 KB
/
databoxLogParser.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package main
import (
"bufio"
"context"
"fmt"
"sync"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
libDatabox "github.com/me-box/lib-go-databox"
)
func ShowLogs() {
cli, _ := client.NewEnvClient()
libDatabox.Info("Outputting logs")
//filters := filters.NewArgs()
//filters.Add("label", "databox.type")
//services, err := d.cli.ServiceList(context.Background(), types.ServiceListOptions{Filters: filters})
services, err := cli.ServiceList(context.Background(), types.ServiceListOptions{})
libDatabox.ChkErr(err)
logChan := make(chan string)
var wg sync.WaitGroup
for _, service := range services {
ioLogReader, err := cli.ServiceLogs(context.Background(), service.ID,
types.ContainerLogsOptions{
Follow: true,
ShowStderr: true,
ShowStdout: true,
Tail: "all",
Timestamps: false,
},
)
if err != nil {
libDatabox.Err("Error reading logs for " + service.Spec.Name)
continue
}
wg.Add(1)
go func(name string) {
scanner := bufio.NewScanner(ioLogReader)
for scanner.Scan() {
message := scanner.Text()
if err := scanner.Err(); err != nil {
wg.Done()
ioLogReader.Close()
break
}
if len(message) > 8 {
message = message[8:]
}
logChan <- fmt.Sprintf("%-25s: %s", name, message)
}
}(service.Spec.Name)
}
go func() {
for {
select {
case msg := <-logChan:
fmt.Println(msg)
}
}
}()
wg.Wait()
}