forked from keybase/go-updater
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlog.go
136 lines (113 loc) · 2.96 KB
/
log.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
package updater
import (
"context"
pkglog "log"
)
var logger = NewLogger(ErrLevel)
//var logger = NewContextLogger(InfoLevel)
// SetLogger sets logger for the package.
func SetLogger(l Logger) {
logger = l
}
// // SetContextLogger sets logger for the package.
// func SetContextLogger(l ContextLogger) {
// logger = l
// }
// Logger interface used in this package.
type Logger interface {
Debugf(format string, args ...interface{})
Infof(format string, args ...interface{})
Warningf(format string, args ...interface{})
Errorf(format string, args ...interface{})
Fatalf(format string, args ...interface{})
}
// LogLevel ...
type LogLevel int
const (
// DebugLevel ...
DebugLevel LogLevel = 3
// InfoLevel ...
InfoLevel LogLevel = 2
// WarnLevel ...
WarnLevel LogLevel = 1
// ErrLevel ...
ErrLevel LogLevel = 0
)
// NewLogger ...
func NewLogger(lev LogLevel) Logger {
return &defaultLog{Level: lev}
}
func (l LogLevel) String() string {
switch l {
case DebugLevel:
return "debug"
case InfoLevel:
return "info"
case WarnLevel:
return "warn"
case ErrLevel:
return "err"
default:
return ""
}
}
type defaultLog struct {
Level LogLevel
}
func (l defaultLog) Debugf(format string, args ...interface{}) {
if l.Level >= 3 {
pkglog.Printf("[DEBG] "+format+"\n", args...)
}
}
func (l defaultLog) Infof(format string, args ...interface{}) {
if l.Level >= 2 {
pkglog.Printf("[INFO] "+format+"\n", args...)
}
}
func (l defaultLog) Warningf(format string, args ...interface{}) {
if l.Level >= 1 {
pkglog.Printf("[WARN] "+format+"\n", args...)
}
}
func (l defaultLog) Errorf(format string, args ...interface{}) {
if l.Level >= 0 {
pkglog.Printf("[ERR] "+format+"\n", args...)
}
}
func (l defaultLog) Fatalf(format string, args ...interface{}) {
pkglog.Fatalf(format, args...)
}
// ContextLogger interface used in this package with request context.
type ContextLogger interface {
Debugf(ctx context.Context, format string, args ...interface{})
Infof(ctx context.Context, format string, args ...interface{})
Warningf(ctx context.Context, format string, args ...interface{})
Errorf(ctx context.Context, format string, args ...interface{})
}
// NewContextLogger ...
func NewContextLogger(lev LogLevel) ContextLogger {
return &defaultContextLog{Level: lev}
}
type defaultContextLog struct {
Level LogLevel
}
func (l defaultContextLog) Debugf(ctx context.Context, format string, args ...interface{}) {
if l.Level >= 3 {
pkglog.Printf("[DEBG] "+format+"\n", args...)
}
}
func (l defaultContextLog) Infof(ctx context.Context, format string, args ...interface{}) {
if l.Level >= 2 {
pkglog.Printf("[INFO] "+format+"\n", args...)
}
}
func (l defaultContextLog) Warningf(ctx context.Context, format string, args ...interface{}) {
if l.Level >= 1 {
pkglog.Printf("[WARN] "+format+"\n", args...)
}
}
func (l defaultContextLog) Errorf(ctx context.Context, format string, args ...interface{}) {
if l.Level >= 0 {
pkglog.Printf("[ERR] "+format+"\n", args...)
}
}