Skip to content

Commit

Permalink
chore(logs): add output format option to logs command (#447)
Browse files Browse the repository at this point in the history
Signed-off-by: Navin Chandra <[email protected]>
  • Loading branch information
navin772 authored Jul 24, 2024
1 parent 840e0dd commit 217e88d
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 2 deletions.
1 change: 1 addition & 0 deletions cmd/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) ")
Expand Down
3 changes: 2 additions & 1 deletion log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ type Options struct {
LogPath string
LogFilter string
JSON bool
Output string
Namespace string
LogType string
Operation string
Expand Down Expand Up @@ -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) {
Expand Down
13 changes: 12 additions & 1 deletion log/logClient.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package log

import (
"bytes"
"context"
"encoding/json"
"errors"
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 217e88d

Please sign in to comment.