142 lines
2.9 KiB
Go
142 lines
2.9 KiB
Go
package laniakea
|
|
|
|
import (
|
|
"fmt"
|
|
"runtime"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/fatih/color"
|
|
)
|
|
|
|
type Logger struct {
|
|
prefix string
|
|
level LogLevel
|
|
printTraceback bool
|
|
printTime bool
|
|
}
|
|
|
|
type LogLevel struct {
|
|
n uint8
|
|
t string
|
|
c color.Attribute
|
|
}
|
|
|
|
type MethodTraceback struct {
|
|
Package string
|
|
Method string
|
|
fullPath string
|
|
signature string
|
|
filename string
|
|
line int
|
|
}
|
|
|
|
var (
|
|
INFO LogLevel = LogLevel{n: 0, t: "info", c: color.FgWhite}
|
|
WARN LogLevel = LogLevel{n: 1, t: "warn", c: color.FgHiYellow}
|
|
ERROR LogLevel = LogLevel{n: 2, t: "error", c: color.FgHiRed}
|
|
FATAL LogLevel = LogLevel{n: 3, t: "fatal", c: color.FgRed}
|
|
DEBUG LogLevel = LogLevel{n: 4, t: "debug", c: color.FgGreen}
|
|
)
|
|
|
|
func CreateLogger() *Logger {
|
|
return &Logger{
|
|
prefix: "LOG",
|
|
level: FATAL,
|
|
printTraceback: false,
|
|
printTime: true,
|
|
}
|
|
}
|
|
|
|
func (l *Logger) Prefix(prefix string) {
|
|
l.prefix = prefix
|
|
}
|
|
func (l *Logger) Level(level LogLevel) {
|
|
l.level = level
|
|
}
|
|
func (l *Logger) PrintTraceback(b bool) {
|
|
l.printTraceback = b
|
|
}
|
|
|
|
func (l *Logger) Info(m ...any) {
|
|
l.print(INFO, m)
|
|
}
|
|
|
|
func (l *Logger) Warn(m ...any) {
|
|
l.print(WARN, m)
|
|
}
|
|
|
|
func (l *Logger) Error(m ...any) {
|
|
l.print(ERROR, m)
|
|
}
|
|
|
|
func (l *Logger) Fatal(m ...any) {
|
|
l.print(FATAL, m)
|
|
panic(m)
|
|
}
|
|
|
|
func (l *Logger) Debug(m ...any) {
|
|
l.print(DEBUG, m)
|
|
}
|
|
|
|
func (l *Logger) formatTime(t time.Time) string {
|
|
return fmt.Sprintf("%02d.%02d.%02d %02d:%02d:%02d", t.Day(), t.Month(), t.Year(), t.Hour(), t.Minute(), t.Second())
|
|
}
|
|
|
|
func (l *Logger) getTraceback() *MethodTraceback {
|
|
caller, _, _, _ := runtime.Caller(4)
|
|
details := runtime.FuncForPC(caller)
|
|
signature := details.Name()
|
|
path, line := details.FileLine(caller)
|
|
splitPath := strings.Split(path, "/")
|
|
|
|
splitSignature := strings.Split(signature, ".")
|
|
pkg, method := splitSignature[0], splitSignature[len(splitSignature)-1]
|
|
|
|
tb := &MethodTraceback{
|
|
filename: splitPath[len(splitPath)-1],
|
|
fullPath: path,
|
|
line: line,
|
|
signature: signature,
|
|
Package: pkg,
|
|
Method: method,
|
|
}
|
|
|
|
return tb
|
|
}
|
|
func (l *Logger) formatTraceback(mt *MethodTraceback) string {
|
|
return fmt.Sprintf("%s:%s:%d", mt.filename, mt.Method, mt.line)
|
|
}
|
|
|
|
func (l *Logger) buildString(level LogLevel, m []any) string {
|
|
args := []string{
|
|
fmt.Sprintf("[%s]", l.prefix),
|
|
fmt.Sprintf("[%s]", strings.ToUpper(level.t)),
|
|
}
|
|
|
|
if l.printTraceback {
|
|
args = append(args, fmt.Sprintf("[%s]", l.formatTraceback(l.getTraceback())))
|
|
}
|
|
|
|
if l.printTime {
|
|
args = append(args, fmt.Sprintf("[%s]", l.formatTime(time.Now())))
|
|
}
|
|
|
|
msg := Map(m, func(el any) string {
|
|
return fmt.Sprintf("%v", el)
|
|
})
|
|
|
|
return fmt.Sprintf("%s %v", strings.Join(args, " "), strings.Join(msg, " "))
|
|
}
|
|
|
|
func (l *Logger) print(level LogLevel, m []any) {
|
|
if l.level.n < level.n {
|
|
return
|
|
}
|
|
if level == INFO {
|
|
color.New(level.c).Println(l.buildString(level, m))
|
|
} else {
|
|
color.New(level.c).Print(l.buildString(level, m))
|
|
}
|
|
}
|