Skip to content

Commit

Permalink
Add --pretty for get and --raw for watch
Browse files Browse the repository at this point in the history
Adds the ability to toggle the display of the timestamp, and stream
name prefix, for both commands under different flags. This preserves
the current defaults.
  • Loading branch information
will-ockmore authored and TylerBrock committed Sep 24, 2018
1 parent 143b5f4 commit 0dcf9cd
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 11 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ sudo dpkg -i <the_deb_name>
- `--expand` Explode JSON objects using indenting
- `--rawString` Print JSON strings instead of escaping ("\n", ...)
- `--invert` Invert white colors to black for light color schemes
- `--raw`, or `--pretty`, for `watch` and `get` commands respectively, toggles display of the timestamp and stream name prefix.
- Filter logs using CloudWatch patterns
- `--filter foo` Filter logs for the text "foo"
Expand Down
16 changes: 12 additions & 4 deletions blade/blade.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,11 @@ func (b *Blade) GetEvents() {

handlePage := func(page *cloudwatchlogs.FilterLogEventsOutput, lastPage bool) bool {
for _, event := range page.Events {
fmt.Print(formatEvent(formatter, event))
if b.output.Pretty {
fmt.Println(formatEvent(formatter, event))
} else {
fmt.Println(*event.Message)
}
}
return !lastPage
}
Expand Down Expand Up @@ -132,7 +136,11 @@ func (b *Blade) StreamEvents() {
for _, event := range page.Events {
updateLastSeenTime(event.Timestamp)
if _, seen := seenEventIDs[*event.EventId]; !seen {
fmt.Print(formatEvent(formatter, event))
if b.output.Raw {
fmt.Println(*event.Message)
} else {
fmt.Println(formatEvent(formatter, event))
}
addSeenEventIDs(event.EventId)
}
}
Expand Down Expand Up @@ -165,9 +173,9 @@ func formatEvent(formatter *colorjson.Formatter, event *cloudwatchlogs.FilteredL
jl := map[string]interface{}{}

if err := json.Unmarshal(bytes, &jl); err != nil {
return fmt.Sprintf("[%s] (%s) %s\n", red(dateStr), white(streamStr), str)
return fmt.Sprintf("[%s] (%s) %s", red(dateStr), white(streamStr), str)
}

output, _ := formatter.Marshal(jl)
return fmt.Sprintf("[%s] (%s) %s\n", red(dateStr), white(streamStr), output)
return fmt.Sprintf("[%s] (%s) %s", red(dateStr), white(streamStr), output)
}
1 change: 1 addition & 0 deletions cmd/get.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +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.Pretty, "pretty", false, "print timestamp and stream name prefix")
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")
Expand Down
2 changes: 2 additions & 0 deletions cmd/watch.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import (
)

var watchConfig config.Configuration

var watchOutputConfig config.OutputConfiguration

var watchCommand = &cobra.Command{
Expand Down Expand Up @@ -42,6 +43,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(&watchOutputConfig.Raw, "raw", false, "print raw log event without timestamp or stream prefix")
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")
Expand Down
13 changes: 6 additions & 7 deletions config/output.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,12 @@ import (
)

type OutputConfiguration struct {
Expand bool
Raw bool
RawString bool
HideStreamName bool
HideDate bool
Invert bool
NoColor bool
Raw bool
Pretty bool
Expand bool
Invert bool
RawString bool
NoColor bool
}

func (c *OutputConfiguration) Formatter() *colorjson.Formatter {
Expand Down

0 comments on commit 0dcf9cd

Please sign in to comment.