laniakea/logger.go

142 lines
2.9 KiB
Go
Raw Permalink Normal View History

2024-12-16 00:52:43 +03:00
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}
)
2024-12-16 00:56:15 +03:00
func CreateLogger() *Logger {
2024-12-16 00:52:43 +03:00
return &Logger{
prefix: "LOG",
level: FATAL,
printTraceback: false,
printTime: true,
}
}
2024-12-17 09:23:53 +03:00
func (l *Logger) Prefix(prefix string) {
2024-12-16 00:52:43 +03:00
l.prefix = prefix
}
2024-12-17 09:23:53 +03:00
func (l *Logger) Level(level LogLevel) {
2024-12-16 00:52:43 +03:00
l.level = level
}
2024-12-17 09:23:53 +03:00
func (l *Logger) PrintTraceback(b bool) {
2024-12-16 00:52:43 +03:00
l.printTraceback = b
}
2024-12-17 09:23:53 +03:00
func (l *Logger) Info(m ...any) {
2024-12-16 00:52:43 +03:00
l.print(INFO, m)
}
2024-12-17 09:23:53 +03:00
func (l *Logger) Warn(m ...any) {
2024-12-16 00:52:43 +03:00
l.print(WARN, m)
}
2024-12-17 09:23:53 +03:00
func (l *Logger) Error(m ...any) {
2024-12-16 00:52:43 +03:00
l.print(ERROR, m)
}
2024-12-17 09:23:53 +03:00
func (l *Logger) Fatal(m ...any) {
2024-12-16 00:52:43 +03:00
l.print(FATAL, m)
2024-12-17 09:23:53 +03:00
panic(m)
2024-12-16 00:52:43 +03:00
}
2024-12-17 09:23:53 +03:00
func (l *Logger) Debug(m ...any) {
2024-12-16 00:52:43 +03:00
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)
}
2024-12-17 09:23:53 +03:00
func (l *Logger) buildString(level LogLevel, m []any) string {
2024-12-16 00:52:43 +03:00
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())))
}
2024-12-17 09:23:53 +03:00
msg := Map(m, func(el any) string {
return fmt.Sprintf("%v", el)
})
return fmt.Sprintf("%s %v", strings.Join(args, " "), strings.Join(msg, " "))
2024-12-16 00:52:43 +03:00
}
2024-12-17 09:23:53 +03:00
func (l *Logger) print(level LogLevel, m []any) {
2024-12-16 00:52:43 +03:00
if l.level.n < level.n {
return
}
2024-12-17 09:23:53 +03:00
if level == INFO {
color.New(level.c).Println(l.buildString(level, m))
} else {
color.New(level.c).Print(l.buildString(level, m))
}
2024-12-16 00:52:43 +03:00
}