forked from go-lark/lark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logger.go
79 lines (67 loc) · 1.24 KB
/
logger.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
package lark
import (
"context"
"io"
"log"
"os"
)
// LogLevel defs
type LogLevel int
// LogLevels
const (
LogLevelTrace = iota + 1
LogLevelDebug
LogLevelInfo
LogLevelWarn
LogLevelError
)
// LogWrapper interface
type LogWrapper interface {
// for log print
Log(context.Context, LogLevel, string)
// for test redirection
SetOutput(io.Writer)
}
// String .
func (ll LogLevel) String() string {
switch ll {
case LogLevelTrace:
return "TRACE"
case LogLevelDebug:
return "DEBUG"
case LogLevelInfo:
return "INFO"
case LogLevelWarn:
return "WARN"
case LogLevelError:
return "ERROR"
}
return ""
}
type stdLogger struct {
*log.Logger
}
func (sl stdLogger) Log(_ context.Context, level LogLevel, msg string) {
sl.Printf("[%s] %s\n", level, msg)
}
const logPrefix = "[go-lark] "
func initDefaultLogger() LogWrapper {
// create a default std logger
logger := stdLogger{
log.New(os.Stderr, logPrefix, log.LstdFlags),
}
return logger
}
// SetLogger set a new logger
func (bot *Bot) SetLogger(logger LogWrapper) {
bot.logger = logger
}
// Logger returns current logger
func (bot Bot) Logger() LogWrapper {
return bot.logger
}
// WithContext .
func (bot *Bot) WithContext(ctx context.Context) *Bot {
bot.ctx = ctx
return bot
}