This repository has been archived by the owner on Dec 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathstd.go
158 lines (129 loc) · 3.47 KB
/
std.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
146
147
148
149
150
151
152
153
154
155
156
157
158
package log
import (
"fmt"
"io"
"log"
"os"
)
// newStdLogger return a stdlib `log` logger
func newStdLogger() Logger {
return &stdLogger{Level: InfoLevel, skip: 1, fields: make(map[string]interface{})}
}
// newStdLoggerWith return a stdlib `log` logger
func newStdLoggerWith(lvl Level) Logger {
return &stdLogger{Level: lvl, skip: 1, fields: make(map[string]interface{})}
}
// newStdLoggerWithConfig return a stdlib `log` logger
func newStdLoggerWithConfig(config *LoggerConfig) Logger { //nolint:deadcode,unused //future code
l, _ := ParseLevel(config.Level)
return &stdLogger{Level: l, skip: 1, fields: make(map[string]interface{})}
}
type stdLogger struct {
Level
skip int
fields map[string]interface{}
}
// extraSkipFramesFromLogPackage used for hedzr/log package functions:
//
// log.Printf, log.Infof, ...
const extraSkipFramesFromLogPackage = 1
const skipFrames = 2 + extraSkipFramesFromLogPackage
func (s *stdLogger) AddSkip(skip int) Logger { return &stdLogger{Level: s.Level, skip: s.skip + skip} }
func (s *stdLogger) out(args ...interface{}) {
str := fmt.Sprint(args...)
_ = log.Output(skipFrames+s.skip, str)
}
func (s *stdLogger) With(key string, val interface{}) Logger {
s.fields[key] = val
return s
}
func (s *stdLogger) WithFields(fields map[string]interface{}) Logger {
for key, val := range fields {
s.fields[key] = val
}
return s
}
func (s *stdLogger) Trace(args ...interface{}) {
if s.Level >= TraceLevel {
s.out(args...)
}
}
func (s *stdLogger) Debug(args ...interface{}) {
if s.Level >= DebugLevel {
s.out(args...)
}
}
func (s *stdLogger) Info(args ...interface{}) {
if s.Level >= InfoLevel {
s.out(args...)
}
}
func (s *stdLogger) Warn(args ...interface{}) {
s.out(args...)
}
func (s *stdLogger) Error(args ...interface{}) {
s.out(args...)
}
func (s *stdLogger) Fatal(args ...interface{}) {
s.out(args...)
if InTesting() {
panic(fmt.Sprint(args...))
}
os.Exit(1)
}
func (s *stdLogger) Panic(args ...interface{}) {
str := fmt.Sprint(args...)
_ = log.Output(skipFrames+s.skip, str)
panic(str)
}
func (s *stdLogger) Print(args ...interface{}) {
s.out(args...)
}
func (s *stdLogger) Println(args ...interface{}) {
str := fmt.Sprintln(args...)
_ = log.Output(skipFrames+s.skip, str)
}
func (s *stdLogger) outf(msg string, args ...interface{}) {
str := fmt.Sprintf(msg, args...)
_ = log.Output(skipFrames+s.skip, str)
}
func (s *stdLogger) Tracef(msg string, args ...interface{}) {
if s.Level >= TraceLevel {
s.outf(msg, args...)
}
}
func (s *stdLogger) Debugf(msg string, args ...interface{}) {
if s.Level >= DebugLevel {
s.outf(msg, args...)
}
}
func (s *stdLogger) Infof(msg string, args ...interface{}) {
if s.Level >= InfoLevel {
s.outf(msg, args...)
}
}
func (s *stdLogger) Warnf(msg string, args ...interface{}) {
s.outf(msg, args...)
}
func (s *stdLogger) Errorf(msg string, args ...interface{}) {
s.outf(msg, args...)
}
func (s *stdLogger) Fatalf(msg string, args ...interface{}) {
s.outf(msg, args...)
if InTesting() {
panic(fmt.Sprintf(msg, args...))
}
os.Exit(1)
}
func (s *stdLogger) Panicf(msg string, args ...interface{}) {
str := fmt.Sprintf(msg, args...)
_ = log.Output(skipFrames+s.skip, str)
panic(str)
}
func (s *stdLogger) Printf(msg string, args ...interface{}) {
s.outf(msg, args...)
}
func (s *stdLogger) SetLevel(lvl Level) { s.Level = lvl }
func (s *stdLogger) GetLevel() Level { return s.Level }
func (s *stdLogger) SetOutput(out io.Writer) {}
func (s *stdLogger) Setup() {}