From 9b818bfe8c60572b69011274b166566bfff1ef66 Mon Sep 17 00:00:00 2001 From: Joe Shaw Date: Fri, 26 Jul 2024 12:01:30 -0400 Subject: [PATCH 1/2] logtail: add --timestamps flag This will print the timestamp of the request start time with each log message. --- pkg/commands/logtail/root.go | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/pkg/commands/logtail/root.go b/pkg/commands/logtail/root.go index 63a40f32f..cf5bff0ff 100644 --- a/pkg/commands/logtail/root.go +++ b/pkg/commands/logtail/root.go @@ -63,6 +63,7 @@ func NewRootCommand(parent argparser.Registerer, g *global.Data) *RootCommand { c.CmdClause.Flag("sort-buffer", "Duration of sort buffer for received logs").Default("1s").DurationVar(&c.cfg.sortBuffer) c.CmdClause.Flag("search-padding", "Time beyond from/to to consider in searches").Default("2s").DurationVar(&c.cfg.searchPadding) c.CmdClause.Flag("stream", "Output: stdout, stderr, both (default)").StringVar(&c.cfg.stream) + c.CmdClause.Flag("timestamps", "Print timestamps with logs").BoolVar(&c.cfg.printTimestamps) return &c } @@ -446,7 +447,11 @@ func (c *RootCommand) printLogs(out io.Writer, logs []Log) { filtered := filterStream(c.cfg.stream, logs) for _, l := range filtered { - fmt.Fprintln(out, l.String()) + if c.cfg.printTimestamps { + fmt.Fprintf(out, "%s | %s\n", l.RequestStartFromRaw().UTC().Format(time.RFC3339), l.String()) + } else { + fmt.Fprintln(out, l.String()) + } } } } @@ -486,6 +491,9 @@ type ( // to is when to get logs until. to int64 + // printTimestamps is whether to print timestamps with logs. + printTimestamps bool + // sortBuffer is how long to buffer logs from when the cli // receives them to when the cli prints them. It will sort // by RequestID for that buffer period. From 681638d286c91d576a6ee861811e065469348e8d Mon Sep 17 00:00:00 2001 From: Joe Shaw Date: Fri, 26 Jul 2024 14:59:38 -0400 Subject: [PATCH 2/2] address review comment --- pkg/commands/logtail/root.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pkg/commands/logtail/root.go b/pkg/commands/logtail/root.go index cf5bff0ff..e3adf7db0 100644 --- a/pkg/commands/logtail/root.go +++ b/pkg/commands/logtail/root.go @@ -448,10 +448,10 @@ func (c *RootCommand) printLogs(out io.Writer, logs []Log) { for _, l := range filtered { if c.cfg.printTimestamps { - fmt.Fprintf(out, "%s | %s\n", l.RequestStartFromRaw().UTC().Format(time.RFC3339), l.String()) - } else { - fmt.Fprintln(out, l.String()) + fmt.Fprint(out, l.RequestStartFromRaw().UTC().Format(time.RFC3339)) + fmt.Fprint(out, " | ") } + fmt.Fprintln(out, l.String()) } } }