forked from turnerlabs/fargate
/
service_logs.go
82 lines (66 loc) · 3.3 KB
/
service_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
package cmd
import (
"fmt"
"github.com/spf13/cobra"
)
var (
flagServiceLogsFilter string
flagServiceLogsEndTime string
flagServiceLogsStartTime string
flagServiceLogsFollow bool
flagServiceLogsTasks []string
flagServiceLogsTime bool
flagServiceLogsNoLogStreamPrefix bool
)
var serviceLogsCmd = &cobra.Command{
Use: "logs",
Short: "Show logs from tasks in a service",
Long: `Show logs from tasks in a service
Return either a specific segment of service logs or tail logs in real-time
using the --follow option. Logs are prefixed by their log stream name which is
in the format of "fargate/\<service-name>/\<task-id>."
Follow will continue to run and return logs until interrupted by Control-C. If
--follow is passed --end cannot be specified.
Logs can be returned for specific tasks within a service by passing a task ID
via the --task flag. Pass --task with a task ID multiple times in order to
retrieve logs from multiple specific tasks.
A specific window of logs can be requested by passing --start and --end options
with a time expression. The time expression can be either a duration or a
timestamp:
- Duration (e.g. -1h [one hour ago], -1h10m30s [one hour, ten minutes, and
thirty seconds ago], 2h [two hours from now])
- Timestamp with optional timezone in the format of YYYY-MM-DD HH:MM:SS [TZ];
timezone will default to UTC if omitted (e.g. 2017-12-22 15:10:03 EST)
You can filter logs for specific term by passing a filter expression via the
--filter flag. Pass a single term to search for that term, pass multiple terms
to search for log messages that include all terms.
--time includes the log timestamp in the output
--no-prefix excludes the log stream prefix from the output
`,
PreRun: func(cmd *cobra.Command, args []string) {
},
Run: func(cmd *cobra.Command, args []string) {
operation := &GetLogsOperation{
LogGroupName: fmt.Sprintf(serviceLogGroupFormat, getServiceName()),
Filter: flagServiceLogsFilter,
Follow: flagServiceLogsFollow,
Namespace: getServiceName(),
IncludeTime: flagServiceLogsTime,
NoLogStreamPrefix: flagServiceLogsNoLogStreamPrefix,
}
operation.AddTasks(flagServiceLogsTasks)
operation.AddStartTime(flagServiceLogsStartTime)
operation.AddEndTime(flagServiceLogsEndTime)
GetLogs(operation)
},
}
func init() {
serviceCmd.AddCommand(serviceLogsCmd)
serviceLogsCmd.Flags().BoolVarP(&flagServiceLogsFollow, "follow", "f", false, "Poll logs and continuously print new events")
serviceLogsCmd.Flags().StringVar(&flagServiceLogsFilter, "filter", "", "Filter pattern to apply")
serviceLogsCmd.Flags().StringVar(&flagServiceLogsStartTime, "start", "", "Earliest time to return logs (e.g. -1h, 2018-01-01 09:36:00 EST")
serviceLogsCmd.Flags().StringVar(&flagServiceLogsEndTime, "end", "", "Latest time to return logs (e.g. 3y, 2021-01-20 12:00:00 EST")
serviceLogsCmd.Flags().StringSliceVarP(&flagServiceLogsTasks, "task", "t", []string{}, "Show logs from specific task (can be specified multiple times)")
serviceLogsCmd.PersistentFlags().BoolVarP(&flagServiceLogsTime, "time", "T", false, "append time to logs")
serviceLogsCmd.PersistentFlags().BoolVarP(&flagServiceLogsNoLogStreamPrefix, "no-prefix", "", false, "don't include log stream prefix in output")
}