初步完成&测试可用

This commit is contained in:
Yun
2024-01-23 00:12:08 +08:00
parent fc485d8670
commit 10ecbce64f
6 changed files with 148 additions and 78 deletions
+15 -5
View File
@@ -10,23 +10,33 @@ import (
"time"
)
func (l *Logger) logger(ctx context.Context, action string, v ...any) {
func (l *Logger) logger(ctx context.Context, event string, v ...any) {
pc, file, line, _ := runtime.Caller(2)
// fmt.Println("runtime.Caller", pc, file, line, ok)
basePath, _ := filepath.Abs("./")
basePath = strings.ReplaceAll(basePath, "\\", "/")
// fmt.Println("basePath", basePath)
file = strings.TrimPrefix(file, basePath)
funcName := runtime.FuncForPC(pc).Name()
funcName = filepath.Ext(funcName)
funcName = strings.TrimPrefix(funcName, ".")
by, _ := json.Marshal(v)
nowTime := time.Now().Local().Format("20060102 15:04:05.000000")
nowTime := time.Now().Local().Format("2006-01-02 15:04:05.000000")
traceId, _ := ctx.Value("trace_id").(string)
traceId, _ := ctx.Value(l.option.traceField).(string)
writeStr := "[" + action + "]" + nowTime + " " + file + ":" + fmt.Sprintf("%d", line) + " " + funcName + " gid:" + getGID() + " " + traceId + " @data@: " + string(by) + "\n\n"
writeStr := "[" + event + "]" + nowTime + " " + file + ":" + fmt.Sprintf("%d", line) + " " + funcName + " gid:" + getGID() + " " + traceId + " @data@: " + string(by) + "\n\n"
l.Write([]byte(writeStr))
l.write(event, []byte(writeStr))
if l.option.errorToInfo && event == "error" {
l.write("info", []byte(writeStr))
}
// log.Println("" + string(by))
}