diff --git a/blade/blade.go b/blade/blade.go index 40b9904..46f6715 100644 --- a/blade/blade.go +++ b/blade/blade.go @@ -90,11 +90,12 @@ func (b *Blade) GetLogStreams() []*cloudwatchlogs.LogStream { // GetEvents gets events from AWS given the blade configuration func (b *Blade) GetEvents() { + formatter := b.output.Formatter() input := b.config.FilterLogEventsInput() handlePage := func(page *cloudwatchlogs.FilterLogEventsOutput, lastPage bool) bool { for _, event := range page.Events { - fmt.Println(*event.Message) + fmt.Print(formatEvent(formatter, event)) } return !lastPage } @@ -131,7 +132,7 @@ func (b *Blade) StreamEvents() { for _, event := range page.Events { updateLastSeenTime(event.Timestamp) if _, seen := seenEventIDs[*event.EventId]; !seen { - printEvent(formatter, event) + fmt.Print(formatEvent(formatter, event)) addSeenEventIDs(event.EventId) } } @@ -151,8 +152,8 @@ func (b *Blade) StreamEvents() { } } -// printEvent prints a filtered CloudWatch log event using the provided formatter -func printEvent(formatter *colorjson.Formatter, event *cloudwatchlogs.FilteredLogEvent) { +// formatEvent returns a CloudWatch log event as a formatted string using the provided formatter +func formatEvent(formatter *colorjson.Formatter, event *cloudwatchlogs.FilteredLogEvent) string { red := color.New(color.FgRed).SprintFunc() white := color.New(color.FgWhite).SprintFunc() @@ -162,10 +163,11 @@ func printEvent(formatter *colorjson.Formatter, event *cloudwatchlogs.FilteredLo dateStr := date.Format(time.RFC3339) streamStr := aws.StringValue(event.LogStreamName) jl := map[string]interface{}{} + if err := json.Unmarshal(bytes, &jl); err != nil { - fmt.Printf("[%s] (%s) %s\n", red(dateStr), white(streamStr), str) - } else { - output, _ := formatter.Marshal(jl) - fmt.Printf("[%s] (%s) %s\n", red(dateStr), white(streamStr), output) + return fmt.Sprintf("[%s] (%s) %s\n", red(dateStr), white(streamStr), str) } + + output, _ := formatter.Marshal(jl) + return fmt.Sprintf("[%s] (%s) %s\n", red(dateStr), white(streamStr), output) } diff --git a/cmd/get.go b/cmd/get.go index 4ee834d..20df8ec 100644 --- a/cmd/get.go +++ b/cmd/get.go @@ -11,6 +11,7 @@ import ( ) var getConfig config.Configuration +var getOutputConfig config.OutputConfiguration var getCommand = &cobra.Command{ Use: "get ", @@ -24,7 +25,7 @@ var getCommand = &cobra.Command{ }, Run: func(cmd *cobra.Command, args []string) { getConfig.Group = args[0] - b := blade.NewBlade(&getConfig, &awsConfig, nil) + b := blade.NewBlade(&getConfig, &awsConfig, &getOutputConfig) if getConfig.Prefix != "" { streams := b.GetLogStreams() if len(streams) == 0 { @@ -57,4 +58,7 @@ Takes an absolute timestamp in RFC3339 format, or a relative time (eg. -2h). Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".`, ) getCommand.Flags().StringVar(&getConfig.Filter, "filter", "", "event filter pattern") + getCommand.Flags().BoolVar(&getOutputConfig.Expand, "expand", false, "indent JSON log messages") + getCommand.Flags().BoolVar(&getOutputConfig.Invert, "invert", false, "invert colors for light terminal themes") + getCommand.Flags().BoolVar(&getOutputConfig.RawString, "rawString", false, "print JSON strings without escaping") } diff --git a/cmd/watch.go b/cmd/watch.go index 5a929d8..02fedcc 100644 --- a/cmd/watch.go +++ b/cmd/watch.go @@ -11,7 +11,7 @@ import ( ) var watchConfig config.Configuration -var outputConfig config.OutputConfiguration +var watchOutputConfig config.OutputConfiguration var watchCommand = &cobra.Command{ Use: "watch ", @@ -25,7 +25,7 @@ var watchCommand = &cobra.Command{ }, Run: func(cmd *cobra.Command, args []string) { watchConfig.Group = args[0] - b := blade.NewBlade(&watchConfig, &awsConfig, &outputConfig) + b := blade.NewBlade(&watchConfig, &awsConfig, &watchOutputConfig) if watchConfig.Prefix != "" { streams := b.GetLogStreams() if len(streams) == 0 { @@ -42,7 +42,7 @@ var watchCommand = &cobra.Command{ func init() { watchCommand.Flags().StringVar(&watchConfig.Prefix, "prefix", "", "log stream prefix filter") watchCommand.Flags().StringVar(&watchConfig.Filter, "filter", "", "event filter pattern") - watchCommand.Flags().BoolVar(&outputConfig.Expand, "expand", false, "indent JSON log messages") - watchCommand.Flags().BoolVar(&outputConfig.Invert, "invert", false, "invert colors for light terminal themes") - watchCommand.Flags().BoolVar(&outputConfig.RawString, "rawString", false, "print JSON strings without escaping") + watchCommand.Flags().BoolVar(&watchOutputConfig.Expand, "expand", false, "indent JSON log messages") + watchCommand.Flags().BoolVar(&watchOutputConfig.Invert, "invert", false, "invert colors for light terminal themes") + watchCommand.Flags().BoolVar(&watchOutputConfig.RawString, "rawString", false, "print JSON strings without escaping") }