github.com/kolanos/fargate@v0.2.3/cmd/service_logs.go (about) 1 package cmd 2 3 import ( 4 "fmt" 5 6 "github.com/spf13/cobra" 7 ) 8 9 var ( 10 flagServiceLogsFilter string 11 flagServiceLogsEndTime string 12 flagServiceLogsStartTime string 13 flagServiceLogsFollow bool 14 flagServiceLogsTasks []string 15 ) 16 17 var serviceLogsCmd = &cobra.Command{ 18 Use: "logs <service-name>", 19 Short: "Show logs from tasks in a service", 20 Long: `Show logs from tasks in a service 21 22 Return either a specific segment of service logs or tail logs in real-time 23 using the --follow option. Logs are prefixed by their log stream name which is 24 in the format of "fargate/\<service-name>/\<task-id>." 25 26 Follow will continue to run and return logs until interrupted by Control-C. If 27 --follow is passed --end cannot be specified. 28 29 Logs can be returned for specific tasks within a service by passing a task ID 30 via the --task flag. Pass --task with a task ID multiple times in order to 31 retrieve logs from multiple specific tasks. 32 33 A specific window of logs can be requested by passing --start and --end options 34 with a time expression. The time expression can be either a duration or a 35 timestamp: 36 37 - Duration (e.g. -1h [one hour ago], -1h10m30s [one hour, ten minutes, and 38 thirty seconds ago], 2h [two hours from now]) 39 - Timestamp with optional timezone in the format of YYYY-MM-DD HH:MM:SS [TZ]; 40 timezone will default to UTC if omitted (e.g. 2017-12-22 15:10:03 EST) 41 42 You can filter logs for specific term by passing a filter expression via the 43 --filter flag. Pass a single term to search for that term, pass multiple terms 44 to search for log messages that include all terms.`, 45 Args: cobra.ExactArgs(1), 46 PreRun: func(cmd *cobra.Command, args []string) { 47 }, 48 Run: func(cmd *cobra.Command, args []string) { 49 operation := &GetLogsOperation{ 50 LogGroupName: fmt.Sprintf(serviceLogGroupFormat, args[0]), 51 Filter: flagServiceLogsFilter, 52 Follow: flagServiceLogsFollow, 53 Namespace: args[0], 54 } 55 56 operation.AddTasks(flagServiceLogsTasks) 57 operation.AddStartTime(flagServiceLogsStartTime) 58 operation.AddEndTime(flagServiceLogsEndTime) 59 60 GetLogs(operation) 61 }, 62 } 63 64 func init() { 65 serviceCmd.AddCommand(serviceLogsCmd) 66 67 serviceLogsCmd.Flags().BoolVarP(&flagServiceLogsFollow, "follow", "f", false, "Poll logs and continuously print new events") 68 serviceLogsCmd.Flags().StringVar(&flagServiceLogsFilter, "filter", "", "Filter pattern to apply") 69 serviceLogsCmd.Flags().StringVar(&flagServiceLogsStartTime, "start", "", "Earliest time to return logs (e.g. -1h, 2018-01-01 09:36:00 EST") 70 serviceLogsCmd.Flags().StringVar(&flagServiceLogsEndTime, "end", "", "Latest time to return logs (e.g. 3y, 2021-01-20 12:00:00 EST") 71 serviceLogsCmd.Flags().StringSliceVarP(&flagServiceLogsTasks, "task", "t", []string{}, "Show logs from specific task (can be specified multiple times)") 72 }