Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(logs): add output format option to karmor logs command #447

Merged
merged 2 commits into from
Jul 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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