forked from imjerrybao/apex
/
logs.go
85 lines (65 loc) · 1.77 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
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
// Package logs outputs logs from CloudWatch logs.
package logs
import (
"fmt"
"time"
"github.com/aws/aws-sdk-go/service/cloudwatchlogs"
"github.com/spf13/cobra"
"github.com/apex/apex/cmd/apex/root"
"github.com/apex/apex/logs"
)
// filter pattern.
var filter string
// follow via polling.
var follow bool
// duration of results.
var duration time.Duration
// example output.
const example = ` Print logs for all functions
$ apex logs
Follow the output
$ apex logs -f
Follow output with no historical logs
$ apex logs -f --since 0
Print logs for a single function
$ apex logs api
Print logs for functions with a specified start time, e.g. 5 minutes
$ apex logs foo bar --since 5m`
// Command config.
var Command = &cobra.Command{
Use: "logs [<name>...] [<duration>]",
Short: "Output function logs",
Example: example,
RunE: run,
}
// Initialize.
func init() {
root.Register(Command)
f := Command.Flags()
f.DurationVarP(&duration, "since", "s", 5*time.Minute, "Start time of the search")
f.StringVarP(&filter, "filter", "F", "", "Filter logs with pattern")
f.BoolVarP(&follow, "follow", "f", false, "Follow tails logs for updates")
}
// Run command.
func run(c *cobra.Command, args []string) error {
if err := root.Project.LoadFunctions(args...); err != nil {
return err
}
config := logs.Config{
Service: cloudwatchlogs.New(root.Session),
StartTime: time.Now().Add(-duration).UTC(),
PollInterval: 2 * time.Second,
Follow: follow,
FilterPattern: filter,
}
l := &logs.Logs{
Config: config,
}
for _, fn := range root.Project.Functions {
l.GroupNames = append(l.GroupNames, fn.GroupName())
}
for event := range l.Start() {
fmt.Printf("\033[34m%s\033[0m %s", event.GroupName, event.Message)
}
return l.Err()
}