From 91508c50b54c9a0375856a17b2e774d42782b81d Mon Sep 17 00:00:00 2001 From: Frédéric Guillot Date: Sat, 8 Jun 2019 17:16:12 -0700 Subject: Add option to toggle date/time in log messages --- logger/logger.go | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) (limited to 'logger') diff --git a/logger/logger.go b/logger/logger.go index a430c0d..f329b2c 100644 --- a/logger/logger.go +++ b/logger/logger.go @@ -7,9 +7,11 @@ package logger // import "miniflux.app/logger" import ( "fmt" "os" + "time" ) var requestedLevel = InfoLevel +var displayDateTime = false // LogLevel type. type LogLevel uint32 @@ -43,6 +45,11 @@ func (level LogLevel) String() string { } } +// EnableDateTime enables date time in log messages. +func EnableDateTime() { + displayDateTime = true +} + // EnableDebug increases logging, more verbose (debug) func EnableDebug() { requestedLevel = DebugLevel @@ -79,6 +86,13 @@ func Fatal(format string, v ...interface{}) { } func formatMessage(level LogLevel, format string, v ...interface{}) { - prefix := fmt.Sprintf("[%s] ", level.String()) + var prefix string + + if displayDateTime { + prefix = fmt.Sprintf("[%s] [%s] ", time.Now().Format("2006-01-02T15:04:05"), level) + } else { + prefix = fmt.Sprintf("[%s] ", level) + } + fmt.Fprintf(os.Stderr, prefix+format+"\n", v...) } -- cgit v1.2.3