From 159263197112ffb3dce04d19ed0642c4cd4cb29a Mon Sep 17 00:00:00 2001 From: Kevin Cotugno Date: Sun, 10 Sep 2017 10:57:35 -0700 Subject: [PATCH] Logger fixes --- logger.go | 2 +- os/logger.go | 67 ------------------------------- osutil/logger_service.go | 85 ++++++++++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 68 deletions(-) delete mode 100644 os/logger.go create mode 100644 osutil/logger_service.go diff --git a/logger.go b/logger.go index bd55060..aeb83d8 100644 --- a/logger.go +++ b/logger.go @@ -1,5 +1,5 @@ package tacitus type Logger interface { - Info(format string, ...interface{}) + Info(format string, params ...interface{}) } diff --git a/os/logger.go b/os/logger.go deleted file mode 100644 index 2491f0f..0000000 --- a/os/logger.go +++ /dev/null @@ -1,67 +0,0 @@ -package io - -import ( - "fmt" - "os" - "time" -) - -const tmpl = "%v - %v: " - -type Logger struct { - DateFormat string - Path string - Stderr bool - UTC bool - - file *os.File -} - -func NewLogger() *Logger { - l := Logger{} - - l.DateFormat = time.RFC3339Nano - l.Stderr = true - - return &l -} - -func (l *Logger) Start() error { - log, err := os.OpenFile(l.Path, os.O_APPEND | os.O_CREATE | os.O_WRONLY, 0640) - if err != nil { - return err - } - - if l.file != nil { - l.file.Close() - } - - l.file = log - log = true -} - -func (l *Logger) Stop() { - log = false - l.file.Close() -} - -func (l *Logger) Info(format string, params ...interface{}) { - format = tmpl + format - params = append([]interface{l.formattedTime, "I"}, params) - - if l.Stderr { - fmt.Fprintf(os.Stderr, format, params) - } - if l.file != nil { - fmt.Fprintf(l.file, format, params) - } -} - -func (l * Logger) formattedTime() string { - t := time.Now() - - if l.UTC { - t = t.UTC() - - return t.Format(l.DateFormat) -} diff --git a/osutil/logger_service.go b/osutil/logger_service.go new file mode 100644 index 0000000..cec0c24 --- /dev/null +++ b/osutil/logger_service.go @@ -0,0 +1,85 @@ +package osutil + +import ( + "fmt" + "os" + "time" + "strings" +) + +const tmpl = "%v - %v: " + +type LoggerService struct { + DateFormat string + Path string + Stderr bool + Utc bool + + file *os.File + log bool +} + +func NewLogger() *LoggerService { + l := Logger{} + + l.DateFormat = time.RFC3339Nano + l.Stderr = true + + return &l +} + +func (l *LoggerService) Start() error { + if l.Path != "" { + log, err := os.OpenFile(l.Path, os.O_APPEND | os.O_CREATE | os.O_WRONLY, 0640) + if err != nil { + return err + } + + if l.file != nil { + l.file.Close() + } + + l.file = log + } + + l.log = true + + return nil +} + +func (l *LoggerService) Stop() { + l.log = false + + if l.file != nil { + l.file.Close() + l.file = nil + } +} + +func (l *LoggerService) Info(format string, params ...interface{}) { + format = tmpl + format + params = append([]interface{}{l.formattedTime(), "I"}, params...) + + if l.Stderr { + fmt.Fprintf(os.Stderr, formatNewLine(format), params...) + } + if l.file != nil { + fmt.Fprintf(l.file, format, params) + } +} + +func (l * Logger) formattedTime() string { + t := time.Now() + + if l.Utc { + t = t.UTC() + } + + return t.Format(l.DateFormat) +} + +func formatNewLine(format string) string { + s := strings.Replace(format, "\n", " ", -1) + s = strings.TrimSpace(s) + return s + "\n" +}