Files
loggerx/format.go
T

43 lines
1.0 KiB
Go
Raw Normal View History

2024-01-22 18:05:51 +08:00
package loggerx
import (
"context"
"encoding/json"
"fmt"
"path/filepath"
"runtime"
"strings"
"time"
)
2024-01-23 00:12:08 +08:00
func (l *Logger) logger(ctx context.Context, event string, v ...any) {
2024-01-22 18:05:51 +08:00
pc, file, line, _ := runtime.Caller(2)
// fmt.Println("runtime.Caller", pc, file, line, ok)
2024-01-23 00:12:08 +08:00
basePath, _ := filepath.Abs("./")
basePath = strings.ReplaceAll(basePath, "\\", "/")
// fmt.Println("basePath", basePath)
file = strings.TrimPrefix(file, basePath)
2024-01-22 18:05:51 +08:00
funcName := runtime.FuncForPC(pc).Name()
funcName = filepath.Ext(funcName)
funcName = strings.TrimPrefix(funcName, ".")
by, _ := json.Marshal(v)
2024-01-23 00:12:08 +08:00
nowTime := time.Now().Local().Format("2006-01-02 15:04:05.000000")
traceId, _ := ctx.Value(l.option.traceField).(string)
2024-01-22 18:05:51 +08:00
2024-01-23 00:12:08 +08:00
writeStr := "[" + event + "]" + nowTime + " " + file + ":" + fmt.Sprintf("%d", line) + " " + funcName + " gid:" + getGID() + " " + traceId + " @data@: " + string(by) + "\n\n"
2024-01-22 18:05:51 +08:00
2024-01-23 00:12:08 +08:00
l.write(event, []byte(writeStr))
2024-01-22 18:05:51 +08:00
2024-01-23 00:12:08 +08:00
if l.option.errorToInfo && event == "error" {
l.write("info", []byte(writeStr))
}
2024-01-22 18:05:51 +08:00
// log.Println("" + string(by))
}