diff --git a/cmd/log.go b/cmd/log.go index 94e1574f..c0994c7a 100644 --- a/cmd/log.go +++ b/cmd/log.go @@ -33,6 +33,7 @@ func init() { logCmd.Flags().StringVar(&logOptions.LogPath, "logPath", "stdout", "Output location for alerts and logs, {path|stdout|none}") logCmd.Flags().StringVar(&logOptions.LogFilter, "logFilter", "policy", "Filter for what kinds of alerts and logs to receive, {policy|system|all}") logCmd.Flags().BoolVar(&logOptions.JSON, "json", false, "Flag to print alerts and logs in the JSON format") + logCmd.Flags().StringVarP(&logOptions.Output, "output", "o", "text", "Output format: text, json, or pretty-json") logCmd.Flags().StringVarP(&logOptions.Namespace, "namespace", "n", "", "k8s namespace filter") logCmd.Flags().StringVar(&logOptions.Operation, "operation", "", "Give the type of the operation (Eg:Process/File/Network)") logCmd.Flags().StringVar(&logOptions.LogType, "logType", "", "Log type you want (Eg:ContainerLog/HostLog) ") diff --git a/log/log.go b/log/log.go index 490215d9..b6167a0a 100644 --- a/log/log.go +++ b/log/log.go @@ -48,6 +48,7 @@ type Options struct { LogPath string LogFilter string JSON bool + Output string Namespace string LogType string Operation string @@ -152,7 +153,7 @@ func StartObserver(c *k8s.Client, o Options) error { return nil } - // create client + // create client logClient, err := NewClient(gRPC, o, c.K8sClientset) if err != nil { if !o.Secure && !isDialingError(err) { diff --git a/log/logClient.go b/log/logClient.go index e5235656..a6d54dc2 100644 --- a/log/logClient.go +++ b/log/logClient.go @@ -4,6 +4,7 @@ package log import ( + "bytes" "context" "encoding/json" "errors" @@ -406,8 +407,18 @@ func WatchTelemetryHelper(arr []byte, t string, o Options) { o.EventChan <- EventInfo{Data: arr, Type: t} } - if o.JSON { + if o.JSON || o.Output == "json" { str = fmt.Sprintf("%s\n", string(arr)) + } else if o.Output == "pretty-json" { + + var prettyJSON bytes.Buffer + err = json.Indent(&prettyJSON, arr, "", " ") + + if err != nil { + fmt.Fprintf(os.Stderr, "Failed to prettify JSON (%s)\n", err.Error()) + } + str = fmt.Sprintf("%s\n", prettyJSON.String()) + } else { if time, ok := res["UpdatedTime"]; ok {