-
Notifications
You must be signed in to change notification settings - Fork 0
/
global.go
145 lines (116 loc) · 3.13 KB
/
global.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
package slog
import (
"context"
"fmt"
)
//
func Log(ctx context.Context, level Level, msg string, args ...any) {
DefaultCtx(ctx).log(level, ActionNone, msg, args...)
}
func LogAttrs(ctx context.Context, level Level, msg string, attrs ...Attr) {
l := DefaultCtx(ctx)
log(
l.DefaultContext(),
l.DefaultHandler(),
level,
ActionNone,
0,
msg,
func(r *Record) {
r.AddAttrs(attrs...)
},
)
}
func Debug(msg string, args ...any) {
Default().log(LevelDebug, ActionNone, msg, args...)
}
func DebugCtx(ctx context.Context, msg string, args ...any) {
DefaultCtx(ctx).log(LevelDebug, ActionNone, msg, args...)
}
func Info(msg string, args ...any) {
Default().log(LevelInfo, ActionNone, msg, args...)
}
func InfoCtx(ctx context.Context, msg string, args ...any) {
DefaultCtx(ctx).log(LevelInfo, ActionNone, msg, args...)
}
func Warn(msg string, args ...any) {
Default().log(LevelWarn, ActionNone, msg, args...)
}
func WarnCtx(ctx context.Context, msg string, args ...any) {
DefaultCtx(ctx).log(LevelWarn, ActionNone, msg, args...)
}
func Error(msg string, args ...any) {
Default().log(LevelError, ActionNone, msg, args...)
}
func ErrorCtx(ctx context.Context, msg string, args ...any) {
DefaultCtx(ctx).log(LevelError, ActionNone, msg, args...)
}
//
func Panic(obj any, args ...any) {
var msg string
var ok bool
if msg, ok = obj.(string); !ok {
msg = fmt.Sprint(obj)
}
Default().log(LevelError, ActionPanic, msg, args...)
}
func Fatal(obj any, args ...any) {
var msg string
var ok bool
if msg, ok = obj.(string); !ok {
msg = fmt.Sprint(obj)
}
Default().log(LevelError, ActionFatal, msg, args...)
}
func IfError(err error, args ...any) {
if err != nil {
Default().log(LevelError, ActionNone, ErrorMsg, append(args, E(err))...)
}
}
func IfPanic(err error, args ...any) {
if err != nil {
Default().log(LevelError, ActionPanic, ErrorMsg, append(args, E(err))...)
}
}
func IfFatal(err error, args ...any) {
if err != nil {
Default().log(LevelError, ActionFatal, ErrorMsg, append(args, E(err))...)
}
}
func OrError(fn func() error, args ...any) {
if err := fn(); err != nil {
Default().log(LevelError, ActionNone, ErrorMsg, append(args, E(err))...)
}
}
func OrPanic(fn func() error, args ...any) {
if err := fn(); err != nil {
Default().log(LevelError, ActionPanic, ErrorMsg, append(args, E(err))...)
}
}
func OrFatal(fn func() error, args ...any) {
if err := fn(); err != nil {
Default().log(LevelError, ActionFatal, ErrorMsg, append(args, E(err))...)
}
}
//
func Print(v ...any) {
Default().log(LevelInfo, ActionNone, fmt.Sprint(v...))
}
func Printf(format string, v ...any) {
Default().log(LevelInfo, ActionNone, fmt.Sprintf(format, v...))
}
func Println(v ...any) {
Default().log(LevelInfo, ActionNone, fmt.Sprintln(v...))
}
func Fatalf(format string, v ...any) {
Default().log(LevelError, ActionFatal, fmt.Sprintf(format, v...))
}
func Fatalln(v ...any) {
Default().log(LevelError, ActionFatal, fmt.Sprintln(v...))
}
func Panicf(format string, v ...any) {
Default().log(LevelError, ActionPanic, fmt.Sprintf(format, v...))
}
func Panicln(v ...any) {
Default().log(LevelError, ActionPanic, fmt.Sprintln(v...))
}