From b309a62d2748f7748fd11b592e43aa3da0331857 Mon Sep 17 00:00:00 2001 From: spsobole Date: Tue, 16 Apr 2024 11:09:15 -0600 Subject: [PATCH] a bit of cleanup for logging --- log/default.go | 12 +++++++-- log/log.go | 63 +++++++++++++++++++++++++++++------------------ log/log_test.go | 29 ++++++++++++++++++++-- log/printers.go | 65 +++++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 141 insertions(+), 28 deletions(-) create mode 100644 log/printers.go diff --git a/log/default.go b/log/default.go index e707712..66c03af 100644 --- a/log/default.go +++ b/log/default.go @@ -1,8 +1,12 @@ package log +var DefaultTimeFormat = "2006-01-02 15:04:05" + var Default = &Log{ - fmtTime: "2006-01-02 15:04:05", - level: LogAll, + component: "ALL", + fmtTime: DefaultTimeFormat, + level: LogAll, + printer: ColorPrinterClassic, } func Printf(s string, v ...interface{}) { @@ -40,3 +44,7 @@ func Highlightf(s string, v ...interface{}) { func Errorf(s string, v ...interface{}) { Default.Errorf(s, v...) } + +func WithName(name string) *Log { + return Default.WithName(name) +} diff --git a/log/log.go b/log/log.go index 6aff37a..5f486fe 100644 --- a/log/log.go +++ b/log/log.go @@ -28,14 +28,14 @@ const ( ) type Log struct { - fmtTime string - level uint32 + fmtTime string + level uint32 + component string + printer func(level uint32, component string, msg string) } func (l *Log) Printf(s string, v ...interface{}) { - if l.level&LogPrint == LogPrint { - l.ColorPrint(ColorGray, fmt.Sprintf(s, v...)) - } + l.emit(LogPrint, s, v...) } func (l *Log) Panicf(s string, v ...interface{}) { @@ -43,49 +43,43 @@ func (l *Log) Panicf(s string, v ...interface{}) { } func (l *Log) Fatalf(s string, v ...interface{}) { - l.ColorPrint(ColorRed, fmt.Sprintf(s, v...)) + l.emit(LogError, s, v...) os.Exit(1) } func (l *Log) Infof(s string, v ...interface{}) { - if l.level&LogInfo == LogInfo { - l.ColorPrint(ColorReset, fmt.Sprintf(s, v...)) - } + l.emit(LogInfo, s, v...) } func (l *Log) Eventf(s string, v ...interface{}) { - if l.level&LogEvent == LogEvent { - l.ColorPrint(ColorCyan, fmt.Sprintf(s, v...)) - } + l.emit(LogEvent, s, v...) } func (l *Log) Warnf(s string, v ...interface{}) { - if l.level&LogWarn == LogWarn { - l.ColorPrint(ColorYellow, fmt.Sprintf(s, v...)) - } + l.emit(LogWarn, s, v...) } func (l *Log) Debugf(s string, v ...interface{}) { - if l.level&LogDebug == LogDebug { - l.ColorPrint(ColorGray, fmt.Sprintf(s, v...)) - } + l.emit(LogDebug, s, v...) } func (l *Log) Highlightf(s string, v ...interface{}) { - if l.level&LogHighlight == LogHighlight { - l.ColorPrint(ColorGreen, fmt.Sprintf(s, v...)) - } + l.emit(LogHighlight, s, v...) } func (l *Log) Errorf(s string, v ...interface{}) { - if l.level&LogHighlight == LogHighlight { - l.ColorPrint(ColorRed, fmt.Sprintf(s, v...)) + l.emit(LogError, s, v...) +} + +func (l *Log) emit(level uint32, s string, v ...interface{}) { + if l.level&level == level { + l.printer(level, l.component, fmt.Sprintf(s, v...)) } } func (l *Log) ColorPrint(color string, s string) { now := time.Now() // get this early. - fmt.Fprintf(os.Stdout, "%s | %s%s%s", now.Format(l.fmtTime), color, s, ColorReset) + fmt.Fprintf(os.Stdout, "%s | %4s | %s%s%s", now.Format(l.fmtTime), color, l.component, s, ColorReset) } func (l *Log) SetLevel(level uint32) { @@ -95,3 +89,24 @@ func (l *Log) SetLevel(level uint32) { func (l *Log) SetTimeFormat(fmt string) { l.fmtTime = fmt } + +func (l *Log) SetPrinter(printer func(level uint32, component string, msg string)) { + l.printer = printer +} + +func (l *Log) WithName(name string) *Log { + return &Log{ + component: name, + level: l.level, + fmtTime: l.fmtTime, + } +} + +func New(name string) *Log { + return &Log{ + component: name, + level: LogAll, + fmtTime: Default.fmtTime, + printer: Default.printer, + } +} diff --git a/log/log_test.go b/log/log_test.go index 0f206ca..24879b4 100644 --- a/log/log_test.go +++ b/log/log_test.go @@ -5,6 +5,31 @@ import ( ) func TestPrintf(t *testing.T) { - Printf("This is a test\n") - Printf("Another test\n") + log := New("TESTER TOO LONG") + log.Printf("printf message") + log.Warnf("warnf message") + log.Errorf("warnf message") + log.Debugf("debugf message") + log.Highlightf("highlight message") + log.Eventf("eventf message") + log.Infof("infof message") + + log.SetPrinter(ColorPrinter) + log.Printf("printf message") + log.Warnf("warnf message") + log.Errorf("warnf message") + log.Debugf("debugf message") + log.Highlightf("highlight message") + log.Eventf("eventf message") + log.Infof("infof message") + + log.SetPrinter(BasicPrinter) + log.Printf("printf message") + log.Warnf("warnf message") + log.Errorf("warnf message") + log.Debugf("debugf message") + log.Highlightf("highlight message") + log.Eventf("eventf message") + log.Infof("infof message") + } diff --git a/log/printers.go b/log/printers.go new file mode 100644 index 0000000..2575613 --- /dev/null +++ b/log/printers.go @@ -0,0 +1,65 @@ +package log + +import ( + "fmt" + "os" + "strings" + "time" +) + +var colorMap = map[uint32]string{ + LogError: ColorRed, + LogWarn: ColorYellow, + LogEvent: ColorCyan, + LogInfo: ColorReset, + LogHighlight: ColorGreen, + LogPrint: ColorGray, + LogDebug: ColorGray, +} + +var levelMap = map[uint32]string{ + LogError: "ERROR", + LogWarn: "WARN", + LogEvent: "EVENT", + LogInfo: "INFO", + LogHighlight: "HILI", + LogPrint: "PRINT", + LogDebug: "DEBUG", +} + +func ColorPrinterClassic(level uint32, component, msg string) { + color, ok := colorMap[level] + if !ok { + color = ColorRed + } + now := time.Now() // get this early. + fmt.Fprintf(os.Stdout, "%s | %s%s%s", now.Format(DefaultTimeFormat), color, msg, ColorReset) + if !strings.HasSuffix(msg, "\n") { + fmt.Fprintf(os.Stdout, "\n") + } +} + +func ColorPrinter(level uint32, component, msg string) { + color, ok := colorMap[level] + if !ok { + color = ColorRed + } + now := time.Now() // get this early. + fmt.Fprintf(os.Stdout, "%s | %s%-5.5s%s | %s%s%s", now.Format(DefaultTimeFormat), color, component, ColorReset, color, msg, ColorReset) + if !strings.HasSuffix(msg, "\n") { + fmt.Fprintf(os.Stdout, "\n") + } + +} + +func BasicPrinter(level uint32, component, msg string) { + levelString, ok := levelMap[level] + if !ok { + levelString = "----" + } + now := time.Now() // get this early. + fmt.Fprintf(os.Stdout, "%s | %-5.5s | %-5.5s | %s", now.Format(DefaultTimeFormat), levelString, component, msg) + if !strings.HasSuffix(msg, "\n") { + fmt.Fprintf(os.Stdout, "\n") + } +}