feat: add debug logger
This commit is contained in:
parent
e4d5a1be76
commit
61ec427a30
36
log/default.go
Normal file
36
log/default.go
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package log
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
"os"
|
||||||
|
"sync"
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
nilLogger = func(prefix, timestamp, msg string, data ...any) {}
|
||||||
|
normalLogger = func(prefix, timestamp, msg string, data ...any) {
|
||||||
|
fmt.Printf(prefix+"| "+timestamp+" | "+msg+"\n", data...)
|
||||||
|
}
|
||||||
|
|
||||||
|
defaultLogger = &logger{
|
||||||
|
Mutex: sync.Mutex{},
|
||||||
|
timeFormat: "2006-01-02T15:04:05",
|
||||||
|
writer: os.Stdout,
|
||||||
|
level: LogLevelInfo,
|
||||||
|
debug: nilLogger,
|
||||||
|
info: normalLogger,
|
||||||
|
warn: normalLogger,
|
||||||
|
error: normalLogger,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetTimeFormat(format string) {
|
||||||
|
defaultLogger.SetTimeFormat(format)
|
||||||
|
}
|
||||||
|
|
||||||
|
func SetLogLevel(level LogLevel) {
|
||||||
|
defaultLogger.SetLogLevel(level)
|
||||||
|
}
|
103
log/log.go
103
log/log.go
@ -1,52 +1,103 @@
|
|||||||
package log
|
package log
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"fmt"
|
|
||||||
"github.com/fatih/color"
|
"github.com/fatih/color"
|
||||||
|
"io"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
type LogLevel uint32
|
||||||
|
|
||||||
|
const (
|
||||||
|
LogLevelDebug = iota
|
||||||
|
LogLevelInfo
|
||||||
|
LogLevelWarn
|
||||||
|
LogLevelError
|
||||||
|
)
|
||||||
|
|
||||||
|
type logger struct {
|
||||||
|
sync.Mutex
|
||||||
|
timeFormat string
|
||||||
|
writer io.Writer
|
||||||
|
level LogLevel
|
||||||
|
debug func(prefix, timestamp, msg string, data ...any)
|
||||||
|
info func(prefix, timestamp, msg string, data ...any)
|
||||||
|
warn func(prefix, timestamp, msg string, data ...any)
|
||||||
|
error func(prefix, timestamp, msg string, data ...any)
|
||||||
|
}
|
||||||
|
|
||||||
var (
|
var (
|
||||||
red = color.New(color.FgRed)
|
red = color.New(color.FgRed)
|
||||||
green = color.New(color.FgGreen)
|
green = color.New(color.FgGreen)
|
||||||
yellow = color.New(color.FgYellow)
|
yellow = color.New(color.FgYellow)
|
||||||
|
white = color.New(color.FgWhite)
|
||||||
locker = &sync.Mutex{}
|
|
||||||
|
|
||||||
timeFormat = "06-01-02T15:04:05"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func SetTimeFormat(format string) {
|
func (l *logger) SetTimeFormat(format string) {
|
||||||
locker.Lock()
|
l.Lock()
|
||||||
defer locker.Unlock()
|
defer l.Unlock()
|
||||||
|
l.timeFormat = format
|
||||||
timeFormat = format
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (l *logger) SetLogLevel(level LogLevel) {
|
||||||
|
l.Lock()
|
||||||
|
defer l.Unlock()
|
||||||
|
|
||||||
|
if level > LogLevelDebug {
|
||||||
|
l.debug = nilLogger
|
||||||
|
} else {
|
||||||
|
l.debug = normalLogger
|
||||||
|
}
|
||||||
|
|
||||||
|
if level > LogLevelInfo {
|
||||||
|
l.info = nilLogger
|
||||||
|
} else {
|
||||||
|
l.info = normalLogger
|
||||||
|
}
|
||||||
|
|
||||||
|
if level > LogLevelWarn {
|
||||||
|
l.warn = nilLogger
|
||||||
|
} else {
|
||||||
|
l.warn = normalLogger
|
||||||
|
}
|
||||||
|
|
||||||
|
if level > LogLevelError {
|
||||||
|
l.error = nilLogger
|
||||||
|
} else {
|
||||||
|
l.error = normalLogger
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *logger) Debug(msg string, data ...any) {
|
||||||
|
l.debug(white.Sprint("Debug "), time.Now().Format(l.timeFormat), msg, data...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *logger) Info(msg string, data ...any) {
|
||||||
|
l.info(green.Sprint("Info "), time.Now().Format(l.timeFormat), msg, data...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *logger) Warn(msg string, data ...any) {
|
||||||
|
l.warn(yellow.Sprint("Warn "), time.Now().Format(l.timeFormat), msg, data...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (l *logger) Error(msg string, data ...any) {
|
||||||
|
l.error(red.Sprint("Error "), time.Now().Format(l.timeFormat), msg, data...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func Debug(msg string, data ...any) {
|
||||||
|
defaultLogger.Debug(msg, data...)
|
||||||
|
}
|
||||||
func Info(msg string, data ...any) {
|
func Info(msg string, data ...any) {
|
||||||
buf := &bytes.Buffer{}
|
defaultLogger.Info(msg, data...)
|
||||||
_, _ = green.Fprint(buf, "Info ")
|
|
||||||
_, _ = fmt.Fprintf(buf, "| %s | ", time.Now().Format(timeFormat))
|
|
||||||
_, _ = fmt.Fprintf(buf, msg, data...)
|
|
||||||
fmt.Println(buf.String())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Warn(msg string, data ...any) {
|
func Warn(msg string, data ...any) {
|
||||||
buf := &bytes.Buffer{}
|
defaultLogger.Warn(msg, data...)
|
||||||
_, _ = yellow.Fprint(buf, "Warn ")
|
|
||||||
_, _ = fmt.Fprintf(buf, "| %s | ", time.Now().Format(timeFormat))
|
|
||||||
_, _ = fmt.Fprintf(buf, msg, data...)
|
|
||||||
fmt.Println(buf.String())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func Error(msg string, data ...any) {
|
func Error(msg string, data ...any) {
|
||||||
buf := &bytes.Buffer{}
|
defaultLogger.Error(msg, data...)
|
||||||
_, _ = red.Fprint(buf, "Error ")
|
|
||||||
_, _ = fmt.Fprintf(buf, "| %s | ", time.Now().Format(timeFormat))
|
|
||||||
_, _ = fmt.Fprintf(buf, msg, data...)
|
|
||||||
fmt.Println(buf.String())
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type WroteLogger interface {
|
type WroteLogger interface {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user