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

View File

@ -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,
}
}

View File

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

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