TUN-3140: Add timestamps to terminal log entries

This commit is contained in:
Rachel Williams 2020-06-30 10:53:19 -07:00
parent dbe3516204
commit 932e383051
2 changed files with 10 additions and 7 deletions

View File

@ -114,10 +114,12 @@ func New(opts ...Option) (Service, error) {
} }
if !config.terminalOutputDisabled { if !config.terminalOutputDisabled {
terminalFormatter := NewTerminalFormatter(time.RFC3339)
if len(config.supportedTerminalLevels) == 0 { if len(config.supportedTerminalLevels) == 0 {
l.Add(os.Stderr, NewTerminalFormatter(""), InfoLevel, ErrorLevel, FatalLevel) l.Add(os.Stderr, terminalFormatter, InfoLevel, ErrorLevel, FatalLevel)
} else { } else {
l.Add(os.Stderr, NewTerminalFormatter(""), config.supportedTerminalLevels...) l.Add(os.Stderr, terminalFormatter, config.supportedTerminalLevels...)
} }
} }
return l, nil return l, nil

View File

@ -76,17 +76,18 @@ func NewTerminalFormatter(format string) Formatter {
// Timestamp returns the log level with a matching color to the log type // Timestamp returns the log level with a matching color to the log type
func (f *TerminalFormatter) Timestamp(l Level, d time.Time) string { func (f *TerminalFormatter) Timestamp(l Level, d time.Time) string {
t := "" t := ""
dateStr := "[" + d.Format(f.format) + "] "
switch l { switch l {
case InfoLevel: case InfoLevel:
t = f.output("[INFO] ", skittles.Cyan) t = f.output("INFO", skittles.Cyan)
case ErrorLevel: case ErrorLevel:
t = f.output("[ERROR] ", skittles.Red) t = f.output("ERROR", skittles.Red)
case DebugLevel: case DebugLevel:
t = f.output("[DEBUG] ", skittles.Yellow) t = f.output("DEBUG", skittles.Yellow)
case FatalLevel: 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 // Content just writes the log line straight to the sources