-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhlog.go
75 lines (68 loc) · 2.77 KB
/
hlog.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package vloghertz
import (
"context"
"fmt"
"io"
"log/slog"
"github.com/cloudwego/hertz/pkg/common/hlog"
"github.com/virzz/vlog"
)
// Logger slog impl
type HLog struct {
l *slog.Logger
cfg *vlog.Config
}
func (l *HLog) Logger() *slog.Logger { return l.l }
func (l *HLog) SetLevel(level hlog.Level) {
lvl := hLevelToSLevel(level)
l.cfg.Level.Set(lvl)
}
func (l *HLog) SetOutput(writer io.Writer) {
l.cfg.Output = writer
l.l = slog.New(slog.NewJSONHandler(writer, l.cfg.HandlerOptions))
}
func (l *HLog) log(level hlog.Level, v ...any) {
l.l.Log(context.TODO(), hLevelToSLevel(level), fmt.Sprint(v...))
}
func (l *HLog) logf(level hlog.Level, format string, kvs ...any) {
l.l.Log(context.TODO(), hLevelToSLevel(level), fmt.Sprintf(format, kvs...))
}
func (l *HLog) ctxLogf(level hlog.Level, ctx context.Context, format string, v ...any) {
l.l.Log(ctx, hLevelToSLevel(level), fmt.Sprintf(format, v...))
}
func (l *HLog) Trace(v ...any) { l.log(hlog.LevelTrace, v...) }
func (l *HLog) Debug(v ...any) { l.log(hlog.LevelDebug, v...) }
func (l *HLog) Info(v ...any) { l.log(hlog.LevelInfo, v...) }
func (l *HLog) Notice(v ...any) { l.log(hlog.LevelNotice, v...) }
func (l *HLog) Warn(v ...any) { l.log(hlog.LevelWarn, v...) }
func (l *HLog) Error(v ...any) { l.log(hlog.LevelError, v...) }
func (l *HLog) Fatal(v ...any) { l.log(hlog.LevelFatal, v...) }
func (l *HLog) Tracef(f string, v ...any) { l.logf(hlog.LevelTrace, f, v...) }
func (l *HLog) Debugf(f string, v ...any) { l.logf(hlog.LevelDebug, f, v...) }
func (l *HLog) Infof(f string, v ...any) { l.logf(hlog.LevelInfo, f, v...) }
func (l *HLog) Noticef(f string, v ...any) { l.logf(hlog.LevelNotice, f, v...) }
func (l *HLog) Warnf(f string, v ...any) { l.logf(hlog.LevelWarn, f, v...) }
func (l *HLog) Errorf(f string, v ...any) { l.logf(hlog.LevelError, f, v...) }
func (l *HLog) Fatalf(f string, v ...any) { l.logf(hlog.LevelFatal, f, v...) }
func (l *HLog) CtxTracef(ctx context.Context, f string, v ...any) {
l.ctxLogf(hlog.LevelDebug, ctx, f, v...)
}
func (l *HLog) CtxDebugf(ctx context.Context, f string, v ...any) {
l.ctxLogf(hlog.LevelDebug, ctx, f, v...)
}
func (l *HLog) CtxInfof(ctx context.Context, f string, v ...any) {
l.ctxLogf(hlog.LevelInfo, ctx, f, v...)
}
func (l *HLog) CtxNoticef(ctx context.Context, f string, v ...any) {
l.ctxLogf(hlog.LevelNotice, ctx, f, v...)
}
func (l *HLog) CtxWarnf(ctx context.Context, f string, v ...any) {
l.ctxLogf(hlog.LevelWarn, ctx, f, v...)
}
func (l *HLog) CtxErrorf(ctx context.Context, f string, v ...any) {
l.ctxLogf(hlog.LevelError, ctx, f, v...)
}
func (l *HLog) CtxFatalf(ctx context.Context, f string, v ...any) {
l.ctxLogf(hlog.LevelFatal, ctx, f, v...)
}
var _ hlog.FullLogger = (*HLog)(nil)