forked from yext/edward
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtail.go
178 lines (156 loc) · 4 KB
/
tail.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package main
import (
"bufio"
"fmt"
"os"
"sort"
"strings"
"github.com/fatih/color"
"github.com/hpcloud/tail"
"github.com/pkg/errors"
"github.com/urfave/cli"
"github.com/yext/edward/config"
"github.com/yext/edward/runner"
"github.com/yext/edward/services"
)
func tailFromFlag(c *cli.Context) error {
fmt.Println("=== Logs ===")
return errors.WithStack(doLog(c))
}
func doLog(c *cli.Context) error {
if len(c.Args()) == 0 {
return errors.New("At least one service or group must be specified")
}
sgs, err := config.GetServicesOrGroups(c.Args())
if err != nil {
return errors.WithStack(err)
}
var logChannel = make(chan runner.LogLine)
var lines []runner.LogLine
for _, sg := range sgs {
switch v := sg.(type) {
case *services.ServiceConfig:
newLines, err := followServiceLog(v, logChannel)
if err != nil {
return err
}
lines = append(lines, newLines...)
case *services.ServiceGroupConfig:
newLines, err := followGroupLog(v, logChannel)
if err != nil {
return err
}
lines = append(lines, newLines...)
}
}
// Sort initial lines
sort.Sort(byTime(lines))
for _, line := range lines {
printMessage(line, services.CountServices(sgs) > 1)
}
for logMessage := range logChannel {
printMessage(logMessage, services.CountServices(sgs) > 1)
}
return nil
}
type byTime []runner.LogLine
func (a byTime) Len() int { return len(a) }
func (a byTime) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byTime) Less(i, j int) bool { return a[i].Time.Before(a[j].Time) }
func printMessage(logMessage runner.LogLine, multiple bool) {
message := strings.TrimSpace(logMessage.Message)
if len(message) == 0 {
return
}
if multiple {
print("[")
color.Set(color.FgHiYellow)
print(logMessage.Name)
if logMessage.Stream == "messages" {
print(" (edward)")
}
color.Unset()
print("]: ")
}
if logMessage.Stream == "stderr" {
color.Set(color.FgRed)
}
if logMessage.Stream == "messages" {
color.Set(color.FgYellow)
}
fmt.Printf("%v\n", strings.TrimSpace(message))
color.Unset()
}
func followGroupLog(group *services.ServiceGroupConfig, logChannel chan runner.LogLine) ([]runner.LogLine, error) {
var lines []runner.LogLine
for _, group := range group.Groups {
newLines, err := followGroupLog(group, logChannel)
lines = append(lines, newLines...)
if err != nil {
return nil, err
}
}
for _, service := range group.Services {
newLines, err := followServiceLog(service, logChannel)
lines = append(lines, newLines...)
if err != nil {
return nil, err
}
}
return lines, nil
}
func followServiceLog(service *services.ServiceConfig, logChannel chan runner.LogLine) ([]runner.LogLine, error) {
// Skip services that don't have a launch command
if service.Commands.Launch == "" {
return nil, nil
}
runLog := service.GetRunLog()
logFile, err := os.Open(runLog)
defer logFile.Close()
if err != nil {
return nil, errors.WithStack(err)
}
var initialLines []runner.LogLine
// create a new scanner and read the file line by line
scanner := bufio.NewScanner(logFile)
var lineCount int
for scanner.Scan() {
text := scanner.Text()
lineCount++
var line runner.LogLine
line, err = runner.ParseLogLine(text)
if err != nil {
return nil, errors.WithStack(err)
}
initialLines = append(initialLines, line)
}
// check for errors
if err = scanner.Err(); err != nil {
return nil, errors.WithStack(err)
}
go doFollowServiceLog(service, lineCount, logChannel)
return initialLines, nil
}
func doFollowServiceLog(service *services.ServiceConfig, skipLines int, logChannel chan runner.LogLine) error {
runLog := service.GetRunLog()
t, err := tail.TailFile(runLog, tail.Config{
Follow: true,
Logger: tail.DiscardingLogger,
})
if err != nil {
return errors.WithStack(err)
}
var linesSkipped int
for line := range t.Lines {
if linesSkipped < skipLines {
linesSkipped++
continue
}
lineData, err := runner.ParseLogLine(line.Text)
if err != nil {
return errors.WithStack(err)
}
logChannel <- lineData
}
return nil
}