fcec7c93153cf1c6f4865edd75452c89e2d41ae9

Author
Maxime Borges <maxime@yaak.ai>
Committer
Maxime Borges <maxime@yaak.ai>
Date

Message

Add --no-time/-t flag

Diff

 1diff --git a/config/config.go b/config/config.go
 2index da4c70069c5499cfbaa212095ef524700bcccc77..c00e3e81e43b18777e0c7ec99d5aeb07b13de60b 100644
 3--- a/config/config.go
 4+++ b/config/config.go
 5@@ -25,6 +25,8 @@ type Config struct {
 6 	ExcludeFields []string
 7 	// Filter is a map of fields and values which are used to filter the log output
 8 	Filter map[string]string
 9+	// NoTime disables the time output
10+	NoTime bool
11 }
12 
13 type rawConfig struct {
14@@ -34,6 +36,7 @@ type rawConfig struct {
15 	Filter        string `long:"filter" short:"f" description:"Filter fields (key=value comma separated)"`
16 	NoColor       bool   `long:"no-color" short:"n" description:"Disable color output"`
17 	ForceColor    bool   `long:"force-color" short:"c" description:"Force color output, even when outputting to a pipe"`
18+	NoTime        bool   `long:"no-time" short:"t" description:"Disable time output"`
19 }
20 
21 func Parse() (*Config, error) {
22@@ -66,6 +69,9 @@ func Parse() (*Config, error) {
23 	if raw.NoColor {
24 		color.NoColor = true
25 	}
26+	if raw.NoTime {
27+		cfg.NoTime = true
28+	}
29 
30 	return &cfg, nil
31 }
32diff --git a/parser/parser.go b/parser/parser.go
33index 909d5fd0eb88ebf298832733711e81a2f830102b..a123b88abd38c7540a957c4e37b5e1fbe90ece2a 100644
34--- a/parser/parser.go
35+++ b/parser/parser.go
36@@ -37,7 +37,7 @@ func (p *Parser) Start() error {
37 			continue
38 		}
39 
40-		_, err = fmt.Fprintf(p.output, "%s\n", record.String(p.cfg.OutputFields, p.cfg.ExcludeFields))
41+		_, err = fmt.Fprintf(p.output, "%s\n", record.String(p.cfg))
42 		if err != nil {
43 			return fmt.Errorf("failed to print log to output: %w", err)
44 		}
45diff --git a/parser/record.go b/parser/record.go
46index d2c630a90fdd06872fd80ac524e7dab65792374a..33d7fd05aef98ba96960df53c55c6e22598528d4 100644
47--- a/parser/record.go
48+++ b/parser/record.go
49@@ -140,23 +140,34 @@ func (r *Record) MatchesFilter(filter map[string]string) bool {
50 }
51 
52 // String returns a formatted string representation of the Record
53-func (r *Record) String(outputFields []string, excludeFields []string) string {
54+func (r *Record) String(cfg *config.Config) string {
55 	line := ""
56 	for _, key := range r.fieldOrder {
57-		if len(outputFields) > 0 && !slices.Contains(outputFields, key) {
58+		if len(cfg.OutputFields) > 0 && !slices.Contains(cfg.OutputFields, key) {
59 			continue
60 		}
61-		if len(excludeFields) > 0 && slices.Contains(excludeFields, key) {
62+		if len(cfg.ExcludeFields) > 0 && slices.Contains(cfg.ExcludeFields, key) {
63 			continue
64 		}
65 		value := r.fields[key]
66 		key = color.HiBlueString(key)
67 		line += fmt.Sprintf("%s=%s ", key, getFormattedValue(value))
68 	}
69-	fmtString := "%s %26s %s"
70+
71+	var fmtString strings.Builder
72+	if !cfg.NoTime {
73+		fmtString.WriteString("%s ")
74+	}
75+
76 	if color.NoColor {
77-		fmtString = "%s %7s %s"
78+		fmtString.WriteString("%7s %s")
79+	} else {
80+		fmtString.WriteString("%26s %s")
81 	}
82 
83-	return fmt.Sprintf(fmtString, r.time.Format("2006-01-02 15:04:05"), getFormattedLevel(r.level), line)
84+	if cfg.NoTime {
85+		return fmt.Sprintf(fmtString.String(), getFormattedLevel(r.level), line)
86+	} else {
87+		return fmt.Sprintf(fmtString.String(), r.time.Format("2006-01-02 15:04:05"), getFormattedLevel(r.level), line)
88+	}
89 }