a bit of cleanup for logging

This commit is contained in:
spsobole 2024-04-16 11:09:15 -06:00
parent 0bbc792882
commit b309a62d27
4 changed files with 141 additions and 28 deletions

View File

@ -1,8 +1,12 @@
package log package log
var DefaultTimeFormat = "2006-01-02 15:04:05"
var Default = &Log{ var Default = &Log{
fmtTime: "2006-01-02 15:04:05", component: "ALL",
fmtTime: DefaultTimeFormat,
level: LogAll, level: LogAll,
printer: ColorPrinterClassic,
} }
func Printf(s string, v ...interface{}) { func Printf(s string, v ...interface{}) {
@ -40,3 +44,7 @@ func Highlightf(s string, v ...interface{}) {
func Errorf(s string, v ...interface{}) { func Errorf(s string, v ...interface{}) {
Default.Errorf(s, v...) Default.Errorf(s, v...)
} }
func WithName(name string) *Log {
return Default.WithName(name)
}

View File

@ -30,12 +30,12 @@ const (
type Log struct { type Log struct {
fmtTime string fmtTime string
level uint32 level uint32
component string
printer func(level uint32, component string, msg string)
} }
func (l *Log) Printf(s string, v ...interface{}) { func (l *Log) Printf(s string, v ...interface{}) {
if l.level&LogPrint == LogPrint { l.emit(LogPrint, s, v...)
l.ColorPrint(ColorGray, fmt.Sprintf(s, v...))
}
} }
func (l *Log) Panicf(s string, v ...interface{}) { 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{}) { func (l *Log) Fatalf(s string, v ...interface{}) {
l.ColorPrint(ColorRed, fmt.Sprintf(s, v...)) l.emit(LogError, s, v...)
os.Exit(1) os.Exit(1)
} }
func (l *Log) Infof(s string, v ...interface{}) { func (l *Log) Infof(s string, v ...interface{}) {
if l.level&LogInfo == LogInfo { l.emit(LogInfo, s, v...)
l.ColorPrint(ColorReset, fmt.Sprintf(s, v...))
}
} }
func (l *Log) Eventf(s string, v ...interface{}) { func (l *Log) Eventf(s string, v ...interface{}) {
if l.level&LogEvent == LogEvent { l.emit(LogEvent, s, v...)
l.ColorPrint(ColorCyan, fmt.Sprintf(s, v...))
}
} }
func (l *Log) Warnf(s string, v ...interface{}) { func (l *Log) Warnf(s string, v ...interface{}) {
if l.level&LogWarn == LogWarn { l.emit(LogWarn, s, v...)
l.ColorPrint(ColorYellow, fmt.Sprintf(s, v...))
}
} }
func (l *Log) Debugf(s string, v ...interface{}) { func (l *Log) Debugf(s string, v ...interface{}) {
if l.level&LogDebug == LogDebug { l.emit(LogDebug, s, v...)
l.ColorPrint(ColorGray, fmt.Sprintf(s, v...))
}
} }
func (l *Log) Highlightf(s string, v ...interface{}) { func (l *Log) Highlightf(s string, v ...interface{}) {
if l.level&LogHighlight == LogHighlight { l.emit(LogHighlight, s, v...)
l.ColorPrint(ColorGreen, fmt.Sprintf(s, v...))
}
} }
func (l *Log) Errorf(s string, v ...interface{}) { func (l *Log) Errorf(s string, v ...interface{}) {
if l.level&LogHighlight == LogHighlight { l.emit(LogError, s, v...)
l.ColorPrint(ColorRed, fmt.Sprintf(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) { func (l *Log) ColorPrint(color string, s string) {
now := time.Now() // get this early. 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) { func (l *Log) SetLevel(level uint32) {
@ -95,3 +89,24 @@ func (l *Log) SetLevel(level uint32) {
func (l *Log) SetTimeFormat(fmt string) { func (l *Log) SetTimeFormat(fmt string) {
l.fmtTime = fmt 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,
}
}

View File

@ -5,6 +5,31 @@ import (
) )
func TestPrintf(t *testing.T) { func TestPrintf(t *testing.T) {
Printf("This is a test\n") log := New("TESTER TOO LONG")
Printf("Another test\n") 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")
} }

65
log/printers.go Normal file
View File

@ -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")
}
}