From 932e3830519c30b1268e3216e9f44051e5e3ece5 Mon Sep 17 00:00:00 2001 From: Rachel Williams Date: Tue, 30 Jun 2020 10:53:19 -0700 Subject: [PATCH] TUN-3140: Add timestamps to terminal log entries --- logger/create.go | 6 ++++-- logger/formatter.go | 11 ++++++----- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/logger/create.go b/logger/create.go index caaa5115..315435fc 100644 --- a/logger/create.go +++ b/logger/create.go @@ -114,10 +114,12 @@ func New(opts ...Option) (Service, error) { } if !config.terminalOutputDisabled { + terminalFormatter := NewTerminalFormatter(time.RFC3339) + if len(config.supportedTerminalLevels) == 0 { - l.Add(os.Stderr, NewTerminalFormatter(""), InfoLevel, ErrorLevel, FatalLevel) + l.Add(os.Stderr, terminalFormatter, InfoLevel, ErrorLevel, FatalLevel) } else { - l.Add(os.Stderr, NewTerminalFormatter(""), config.supportedTerminalLevels...) + l.Add(os.Stderr, terminalFormatter, config.supportedTerminalLevels...) } } return l, nil diff --git a/logger/formatter.go b/logger/formatter.go index 4e609af5..95c645d8 100644 --- a/logger/formatter.go +++ b/logger/formatter.go @@ -76,17 +76,18 @@ func NewTerminalFormatter(format string) Formatter { // Timestamp returns the log level with a matching color to the log type func (f *TerminalFormatter) Timestamp(l Level, d time.Time) string { t := "" + dateStr := "[" + d.Format(f.format) + "] " switch l { case InfoLevel: - t = f.output("[INFO] ", skittles.Cyan) + t = f.output("INFO", skittles.Cyan) case ErrorLevel: - t = f.output("[ERROR] ", skittles.Red) + t = f.output("ERROR", skittles.Red) case DebugLevel: - t = f.output("[DEBUG] ", skittles.Yellow) + t = f.output("DEBUG", skittles.Yellow) case FatalLevel: - t = f.output("[FATAL] ", skittles.Red) + t = f.output("FATAL", skittles.Red) } - return t + return t + dateStr } // Content just writes the log line straight to the sources