Skip to content

Commit 217e88d

Browse files
authored
chore(logs): add output format option to logs command (#447)
Signed-off-by: Navin Chandra <[email protected]>
1 parent 840e0dd commit 217e88d

File tree

3 files changed

+15
-2
lines changed

3 files changed

+15
-2
lines changed

cmd/log.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func init() {
3333
logCmd.Flags().StringVar(&logOptions.LogPath, "logPath", "stdout", "Output location for alerts and logs, {path|stdout|none}")
3434
logCmd.Flags().StringVar(&logOptions.LogFilter, "logFilter", "policy", "Filter for what kinds of alerts and logs to receive, {policy|system|all}")
3535
logCmd.Flags().BoolVar(&logOptions.JSON, "json", false, "Flag to print alerts and logs in the JSON format")
36+
logCmd.Flags().StringVarP(&logOptions.Output, "output", "o", "text", "Output format: text, json, or pretty-json")
3637
logCmd.Flags().StringVarP(&logOptions.Namespace, "namespace", "n", "", "k8s namespace filter")
3738
logCmd.Flags().StringVar(&logOptions.Operation, "operation", "", "Give the type of the operation (Eg:Process/File/Network)")
3839
logCmd.Flags().StringVar(&logOptions.LogType, "logType", "", "Log type you want (Eg:ContainerLog/HostLog) ")

log/log.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type Options struct {
4848
LogPath string
4949
LogFilter string
5050
JSON bool
51+
Output string
5152
Namespace string
5253
LogType string
5354
Operation string
@@ -152,7 +153,7 @@ func StartObserver(c *k8s.Client, o Options) error {
152153
return nil
153154
}
154155

155-
// create client
156+
// create client
156157
logClient, err := NewClient(gRPC, o, c.K8sClientset)
157158
if err != nil {
158159
if !o.Secure && !isDialingError(err) {

log/logClient.go

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
package log
55

66
import (
7+
"bytes"
78
"context"
89
"encoding/json"
910
"errors"
@@ -406,8 +407,18 @@ func WatchTelemetryHelper(arr []byte, t string, o Options) {
406407
o.EventChan <- EventInfo{Data: arr, Type: t}
407408
}
408409

409-
if o.JSON {
410+
if o.JSON || o.Output == "json" {
410411
str = fmt.Sprintf("%s\n", string(arr))
412+
} else if o.Output == "pretty-json" {
413+
414+
var prettyJSON bytes.Buffer
415+
err = json.Indent(&prettyJSON, arr, "", " ")
416+
417+
if err != nil {
418+
fmt.Fprintf(os.Stderr, "Failed to prettify JSON (%s)\n", err.Error())
419+
}
420+
str = fmt.Sprintf("%s\n", prettyJSON.String())
421+
411422
} else {
412423

413424
if time, ok := res["UpdatedTime"]; ok {

0 commit comments

Comments
 (0)