go项目脚手架
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
sorbet/pkg/log/util.go

77 lines
1.5 KiB

package log
import (
"log/slog"
"time"
)
type mutableLevel struct {
l *logger
}
func (l *mutableLevel) Level() slog.Level {
return l.l.Level().slog().Level()
}
type mutablePersistWriter struct {
l *logger
}
func (l *mutablePersistWriter) Write(b []byte) (int, error) {
l.l.mu.Lock()
defer l.l.mu.Unlock()
return l.l.persistWriter.Write(b)
}
type mutableWriter struct {
l *logger
}
func (l *mutableWriter) Write(b []byte) (int, error) {
l.l.mu.Lock()
defer l.l.mu.Unlock()
return l.l.writer.Write(b)
}
func bool2int32(v bool) int32 {
if v {
return 1
} else {
return 0
}
}
func createAttrReplacer(l *logger) func([]string, Attr) Attr {
return func(_ []string, a Attr) Attr {
if a.Key == slog.LevelKey {
level := a.Value.Any().(slog.Level)
levelLabel := parseSlogLevel(level).String()
a.Value = slog.StringValue(levelLabel)
} else if a.Key == slog.TimeKey {
t := a.Value.Any().(time.Time)
a.Value = slog.TimeValue(t.In(l.Timezone()))
} else if a.Key == slog.SourceKey {
s := a.Value.Any().(*slog.Source)
var as []Attr
if s.Function != "" {
as = append(as, String("func", s.Function))
}
if s.File != "" {
as = append(as, String("file", s.File))
}
if s.Line != 0 {
as = append(as, Int("line", s.Line))
}
a.Value = slog.GroupValue(as...)
} else if a.Key == rawLevelKey || a.Key == rawTimeKey {
// TODO(hupeh): 在 JSONHandler 中替换这两个值
a.Key = ""
a.Value = slog.AnyValue(nil)
}
return a
}
}
func identify(s string) string {
return s
}