wheat-cache/pkg/logx/logx.go

162 lines
3.4 KiB
Go
Raw Normal View History

2021-10-05 20:52:45 +08:00
package logx
import (
2021-10-06 14:47:58 +08:00
"context"
2021-10-05 20:52:45 +08:00
"fmt"
2021-10-06 19:14:16 +08:00
"os"
2021-10-05 20:52:45 +08:00
"runtime"
2021-10-06 19:00:48 +08:00
"strings"
2021-10-05 20:52:45 +08:00
"time"
2021-10-19 16:05:24 +08:00
2021-11-02 14:45:08 +08:00
"gitee.com/wheat-os/wheatCache/pkg/event"
middleMsg "gitee.com/wheat-os/wheatCache/pkg/middle-msg"
2021-10-05 20:52:45 +08:00
)
2021-10-06 19:00:48 +08:00
func With(ctx context.Context, p event.ProduceInterface) *upLogger {
2021-10-05 21:36:06 +08:00
return &upLogger{
2021-10-06 19:00:48 +08:00
ctx: ctx,
2021-10-06 14:47:58 +08:00
produce: p,
2021-10-05 21:36:06 +08:00
}
}
2021-10-06 19:00:48 +08:00
func (l *upLogger) Debug(format string, msg ...interface{}) {
l.Print("DEBUG", format, msg...)
2021-10-05 21:36:06 +08:00
}
2021-10-06 19:00:48 +08:00
func (l *upLogger) Info(format string, msg ...interface{}) {
l.Print("INFO", format, msg...)
2021-10-05 21:36:06 +08:00
}
2021-10-06 19:00:48 +08:00
func (l *upLogger) Warn(format string, msg ...interface{}) {
l.Print("WARN", format, msg...)
2021-10-05 21:36:06 +08:00
}
2021-10-06 19:00:48 +08:00
func (l *upLogger) Error(format string, msg ...interface{}) {
l.Print("ERROR", format, msg...)
}
2021-10-06 19:00:48 +08:00
func (l *upLogger) Panic(format string, msg ...interface{}) {
l.Print("ERROR", format, msg...)
2021-10-06 19:14:16 +08:00
os.Exit(-1)
2021-10-05 21:36:06 +08:00
}
2021-10-06 19:00:48 +08:00
func (l *upLogger) Print(level string, format string, msg ...interface{}) {
logPrint(4, level, format, msg...)
2021-10-19 16:05:24 +08:00
sendMsg := &middleMsg.LogContext{
Level: level,
Data: time.Now(),
Msg: fmt.Sprintf(format, msg...),
Route: findPlace(4),
2021-10-19 16:05:24 +08:00
}
middleMsg.SendMiddleMsg(l.ctx, l.produce, sendMsg)
2021-10-05 20:52:45 +08:00
}
func (l *upLogger) Debugln(msg ...interface{}) {
l.Print("DEBUG", "%s", format(msg...))
}
func (l *upLogger) Infoln(msg ...interface{}) {
l.Print("INFO", "%s", format(msg...))
}
func (l *upLogger) Warnln(msg ...interface{}) {
l.Print("WARN", "%s", format(msg...))
}
func (l *upLogger) Errorln(msg ...interface{}) {
l.Print("ERROR", "%s", format(msg...))
}
func (l *upLogger) Panicln(msg ...interface{}) {
l.Print("ERROR", "%s", format(msg...))
os.Exit(-1)
}
2021-10-06 19:00:48 +08:00
func Debug(format string, msg ...interface{}) {
logPrint(3, "DEBUG", format, msg...)
2021-10-05 21:36:06 +08:00
}
2021-10-06 19:00:48 +08:00
func Info(format string, msg ...interface{}) {
logPrint(3, "INFO", format, msg...)
2021-10-05 21:36:06 +08:00
}
2021-10-06 19:00:48 +08:00
func Warn(format string, msg ...interface{}) {
logPrint(3, "WARN", format, msg...)
2021-10-05 21:36:06 +08:00
}
2021-10-06 19:00:48 +08:00
func Error(format string, msg ...interface{}) {
logPrint(3, "ERROR", format, msg...)
2021-10-05 20:52:45 +08:00
}
2021-10-06 19:00:48 +08:00
func Panic(format string, msg ...interface{}) {
logPrint(3, "PANIC", format, msg...)
2021-10-06 19:14:16 +08:00
os.Exit(-1)
2021-10-06 19:00:48 +08:00
}
2021-10-05 20:52:45 +08:00
func Debugln(msg ...interface{}) {
logPrint(3, "DEBUG", "%s", format(msg...))
}
func Infoln(msg ...interface{}) {
logPrint(3, "INFO", "%s", format(msg...))
}
func Warnln(msg ...interface{}) {
logPrint(3, "WARN", "%s", format(msg...))
}
func Errorln(msg ...interface{}) {
logPrint(3, "ERROR", "%s", format(msg...))
}
func Panicln(msg ...interface{}) {
logPrint(3, "PANIC", "%s", format(msg...))
os.Exit(-1)
}
func logPrint(floor int, level string, format string, msg ...interface{}) {
place := findPlace(floor)
2021-10-05 21:36:06 +08:00
datetime := fmt.Sprintf("%s", time.Now())[0:19]
switch level {
case "DEBUG":
fmt.Printf("\033[1;37;40m")
case "INFO":
fmt.Printf("\033[1;32;40m")
case "WARN":
fmt.Printf("\033[1;33;40m")
default:
fmt.Printf("\033[1;31;40m")
}
//fmt.Println(level, datetime, fmt.Sprintf(format, msg...))
fmt.Printf("%s\t%v\t%s\n", level, datetime, fmt.Sprintf(format, msg...))
2021-10-06 19:00:48 +08:00
for _, lv := range stath {
if strings.ToUpper(lv) == strings.ToUpper(level) {
fmt.Println(place)
break
}
}
2021-10-05 21:36:06 +08:00
}
func findPlace(floor int) string {
2021-10-06 19:00:48 +08:00
2021-10-05 20:52:45 +08:00
var (
place string
i = floor
2021-10-05 20:52:45 +08:00
)
for {
_, file, line, _ := runtime.Caller(i)
if line == 0 {
break
}
i++
place = fmt.Sprintf("%s:%d\n%s", file, line, place)
}
2021-10-05 21:36:06 +08:00
return place
2021-10-05 20:52:45 +08:00
}
func format(message ...interface{}) (context string) {
for _, msg := range message {
context = fmt.Sprintf("%s\t%v", context, msg)
}
return context
}