-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathklog.go
executable file
·129 lines (109 loc) · 2.39 KB
/
klog.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
package klog
import (
"fmt"
"io"
"os"
"time"
)
const (
LevelInfo uint = iota
LevelError
LevelWarning
LevelDebug
)
const (
PrefixInfo = "INF: "
PrefixError = "ERR: "
PrefixWarning = "WRN: "
PrefixDebug = "DBG: "
PrefixFatal = "ERR: "
PrefixPanic = "PNC: "
)
type Logger struct {
Level uint
Output io.Writer
}
var DefaultLogger = &Logger{
Level: LevelError,
Output: os.Stdout,
}
func (l *Logger) out(s string) error {
s = fmt.Sprintf("%s %s", time.Now().Format(time.RFC3339), s)
if len(s) == 0 || s[len(s)-1] != '\n' {
s += "\n"
}
_, err := l.Output.Write([]byte(s))
return err
}
func Print(v ...interface{}) {
DefaultLogger.out(PrefixInfo + fmt.Sprint(v...))
}
func Printf(format string, v ...interface{}) {
DefaultLogger.out(PrefixInfo + fmt.Sprintf(format, v...))
}
func Println(v ...interface{}) {
DefaultLogger.out(PrefixInfo + fmt.Sprintln(v...))
}
func Fatal(v ...interface{}) {
DefaultLogger.out(PrefixFatal + fmt.Sprint(v...))
os.Exit(1)
}
func Fatalf(format string, v ...interface{}) {
DefaultLogger.out(PrefixFatal + fmt.Sprintf(format, v...))
os.Exit(1)
}
func Fatalln(v ...interface{}) {
DefaultLogger.out(PrefixFatal + fmt.Sprintln(v...))
os.Exit(1)
}
func Panic(v ...interface{}) {
s := fmt.Sprint(v...)
DefaultLogger.out(PrefixPanic + s)
panic(s)
}
func Panicf(format string, v ...interface{}) {
s := fmt.Sprintf(format, v...)
DefaultLogger.out(PrefixPanic + s)
panic(s)
}
func Panicln(v ...interface{}) {
s := fmt.Sprintln(v...)
DefaultLogger.out(PrefixPanic + s)
panic(s)
}
func Debug(v ...interface{}) {
if DefaultLogger.Level < LevelDebug {
return
}
DefaultLogger.out(PrefixDebug + fmt.Sprint(v...))
}
func Debugf(format string, v ...interface{}) {
if DefaultLogger.Level < LevelDebug {
return
}
DefaultLogger.out(PrefixDebug + fmt.Sprintf(format, v...))
}
func Debugln(v ...interface{}) {
if DefaultLogger.Level < LevelDebug {
return
}
DefaultLogger.out(PrefixDebug + fmt.Sprintln(v...))
}
func Warn(v ...interface{}) {
if DefaultLogger.Level < LevelWarning {
return
}
DefaultLogger.out(PrefixWarning + fmt.Sprint(v...))
}
func Warnf(format string, v ...interface{}) {
if DefaultLogger.Level < LevelWarning {
return
}
DefaultLogger.out(PrefixWarning + fmt.Sprintf(format, v...))
}
func Warnln(v ...interface{}) {
if DefaultLogger.Level < LevelWarning {
return
}
DefaultLogger.out(PrefixWarning + fmt.Sprintln(v...))
}