-
Notifications
You must be signed in to change notification settings - Fork 3.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat(logcli): quiet mode Adds a quiet mode (-q / --quiet) to suppress the debug messages to stderr * feat(logcli): output modes Adds two alternative output modes (-o / --output) - raw: emits the line as parsed - jsonl: emits the line plus all known metadata as JSONL (JSON Line) Usage: -o [default, raw, jsonl] * feat(logcli): quiet tailing mode * feat(logcli): output modes while tailing Supports the three different output modes in tail mode as well * feat(logcli): print labels in jsonl tail mode * refactor(logcli): clean up entry printing Moves the entry printing into a standardized interface, implements this three times: - default (human readable) - jsonl (for scripts) - raw ('as is')
- Loading branch information
1 parent
94c252b
commit 9d5630d
Showing
6 changed files
with
95 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package main | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"log" | ||
"strings" | ||
"time" | ||
|
||
"github.com/fatih/color" | ||
"github.com/prometheus/prometheus/pkg/labels" | ||
) | ||
|
||
// Outputs is an enum with all possible output modes | ||
var Outputs = map[string]LogOutput{ | ||
"default": &DefaultOutput{}, | ||
"jsonl": &JSONLOutput{}, | ||
"raw": &RawOutput{}, | ||
} | ||
|
||
// LogOutput is the interface any output mode must implement | ||
type LogOutput interface { | ||
Print(ts time.Time, lbls *labels.Labels, line string) | ||
} | ||
|
||
// DefaultOutput provides logs and metadata in human readable format | ||
type DefaultOutput struct { | ||
MaxLabelsLen int | ||
CommonLabels labels.Labels | ||
} | ||
|
||
// Print a log entry in a human readable format | ||
func (f DefaultOutput) Print(ts time.Time, lbls *labels.Labels, line string) { | ||
ls := subtract(*lbls, f.CommonLabels) | ||
if len(*ignoreLabelsKey) > 0 { | ||
ls = ls.MatchLabels(false, *ignoreLabelsKey...) | ||
} | ||
|
||
labels := "" | ||
if !*noLabels { | ||
labels = padLabel(ls, f.MaxLabelsLen) | ||
} | ||
fmt.Println( | ||
color.BlueString(ts.Format(time.RFC3339)), | ||
color.RedString(labels), | ||
strings.TrimSpace(line), | ||
) | ||
} | ||
|
||
// JSONLOutput prints logs and metadata as JSON Lines, suitable for scripts | ||
type JSONLOutput struct{} | ||
|
||
// Print a log entry as json line | ||
func (f JSONLOutput) Print(ts time.Time, lbls *labels.Labels, line string) { | ||
entry := map[string]interface{}{ | ||
"timestamp": ts, | ||
"labels": lbls, | ||
"line": line, | ||
} | ||
out, err := json.Marshal(entry) | ||
if err != nil { | ||
log.Fatalf("error marshalling entry: %s", err) | ||
} | ||
fmt.Println(string(out)) | ||
} | ||
|
||
// RawOutput prints logs in their original form, without any metadata | ||
type RawOutput struct{} | ||
|
||
// Print a log entry as is | ||
func (f RawOutput) Print(ts time.Time, lbls *labels.Labels, line string) { | ||
fmt.Println(line) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters