47 lines
1.1 KiB
Go
47 lines
1.1 KiB
Go
package log
|
|||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"fmt"
|
||
|
|
"log/slog"
|
||
|
|
"runtime"
|
||
|
|
"strings"
|
||
|
|
"time"
|
||
|
|
)
|
||
|
|
|
||
|
|
type EntLogger struct {
|
||
|
|
*slog.Logger
|
||
|
|
}
|
||
|
|
|
||
|
|
func (l *EntLogger) DebugWithContext(ctx context.Context, v ...any) {
|
||
|
|
var pcs [1]uintptr
|
||
|
|
// skip [runtime.Callers, this function, this function's caller]
|
||
|
|
|
||
|
|
var pc uintptr
|
||
|
|
for i := 6; i < 20; i++ {
|
||
|
|
runtime.Callers(i, pcs[:])
|
||
|
|
frames := runtime.CallersFrames(pcs[:])
|
||
|
|
frame, _ := frames.Next()
|
||
|
|
//fmt.Println(i, frame.Function, frame.File, frame.Line)
|
||
|
|
if !strings.Contains(frame.File, "automatic_changer/ent") && !strings.Contains(frame.File, "entgo.io") {
|
||
|
|
pc = pcs[0]
|
||
|
|
break
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
r := slog.NewRecord(time.Now(), slog.LevelDebug, fmt.Sprint(v...), pc)
|
||
|
|
/*var attrs []slog.Attr
|
||
|
|
spanCtx := sdktrace.SpanContextFromContext(ctx)
|
||
|
|
if spanCtx.HasTraceID() {
|
||
|
|
attrs = append(attrs, slog.String("trace", spanCtx.TraceID().String()))
|
||
|
|
}
|
||
|
|
if spanCtx.HasSpanID() {
|
||
|
|
attrs = append(attrs, slog.String("span", spanCtx.SpanID().String()))
|
||
|
|
}
|
||
|
|
r.AddAttrs(attrs...)
|
||
|
|
if ctx == nil {
|
||
|
|
ctx = context.Background()
|
||
|
|
}*/
|
||
|
|
_ = l.Handler().Handle(ctx, r)
|
||
|
|
}
|