Repository navigation
Expand file tree
/
Copy pathexported.go
More file actions
145 lines (115 loc) · 2.87 KB
/
Copy pathexported.go
File metadata and controls
145 lines (115 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
E902
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 log
import (
"fmt"
"os"
"time"
)
var (
logger *Logger
transport *Complex
lineFormatter *LineFormatter
)
func init() {
lineFormatter = &LineFormatter{
TimestampFormat: "2006-01-02 15:04:05",
}
transport = NewComplex()
logger = NewLogger(transport, false, "")
RegisterExitHandler(func() {
Close()
})
}
func SetLevel(level Level) {
transport.setLevel(level)
}
func Reset(transports ...EntryTransporter) {
transport.Reset(transports...)
}
//Close 关闭
func Close() {
if transport != nil {
transport.Close()
}
}
func SetPrefix(prefix string) {
logger.SetPrefix(prefix)
}
//WithFields 写域
func WithFields(fields Fields) Builder {
return logger.WithFields(fields)
}
// Debug logs a message at level Debug on the standard logger.
func Debug(args ...interface{}) {
logger.Debug(args...)
}
// Debug logs a message at level Debug on the standard logger.
func DebugF(format string, args ...interface{}) {
logger.Debugf(format, args...)
}
// Info logs a message at level Info on the standard logger.
func Info(args ...interface{}) {
logger.Info(args...)
}
// Warn logs a message at level Warn on the standard logger.
func Warn(args ...interface{}) {
logger.Warn(args...)
}
// Error logs a message at level Error on the standard logger.
func Error(args ...interface{}) {
logger.Error(args...)
}
func panicOut(args ...interface{}) string {
defer func() {
if e := recover(); e != nil {
Close()
}
}()
s, _ := encode(PanicLevel, args...)
_, _ = os.Stderr.Write(s)
return string(s)
}
// Panic logs a message at level Panic on the standard logger.
func Panic(args ...interface{}) {
panic(panicOut(args...))
}
// Fatal logs a message at level Fatal on the standard logger then the process will exit with status set to 1.
func Fatal(args ...interface{}) {
s, e := encode(FatalLevel, args...)
if e != nil {
return
}
_, _ = os.Stderr.Write(s)
logger.Fatal(args...)
}
// Infof logs a message at level Info on the standard logger.
func Infof(format string, args ...interface{}) {
logger.Infof(format, args...)
}
// Warnf logs a message at level Info on the standard logger.
func Warnf(format string, args ...interface{}) {
logger.Warnf(format, args...)
}
// Errorf logs a message at level Error on the standard logger.
func Errorf(format string, args ...interface{}) {
logger.Errorf(format, args...)
}
// Fatalf logs a message at level Fatal on the standard logger then the process will exit with status set to 1.
func Fatalf(format string, args ...interface{}) {
s, e := encode(FatalLevel, fmt.Sprintf(format, args...))
if e != nil {
return
}
_, _ = os.Stdout.Write(s)
logger.Fatalf(format, args...)
}
func encode(level Level, args ...interface{}) ([]byte, error) {
entry := &Entry{}
entry.Message = fmt.Sprintln(args...)
entry.Level = level
entry.Time = time.Now()
s, e := lineFormatter.Format(entry)
if e != nil {
return s, e
}
return s, e
}