forked from go-telegram-bot-api/telegram-bot-api
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
55 lines (46 loc) · 1.36 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
package tgbotapi
import (
"errors"
"io"
stdlog "log"
"os"
)
// BotLogger is an interface that represents the required methods to log data.
//
// Instead of requiring the standard logger, we can just specify the methods we
// use and allow users to pass anything that implements these. We use a subset
// of standard logging library APIs. A recommended library is k8s.io/klog.
type BotLogger interface {
Infoln(args ...interface{})
Infof(format string, args ...interface{})
Errorln(args ...interface{})
Errorf(format string, args ...interface{})
}
var botlog = newStdLogAdapter(os.Stderr, "", stdlog.LstdFlags)
// SetLogger specifies the logger that the package should use.
func SetLogger(logger BotLogger) error {
if logger == nil {
return errors.New("logger is nil")
}
botlog = logger
return nil
}
// Implement standard log adapter
func newStdLogAdapter(out io.Writer, prefix string, flag int) BotLogger {
return &stdlogAdapter{*stdlog.New(out, prefix, flag)}
}
type stdlogAdapter struct {
stdlog.Logger
}
func (slw *stdlogAdapter) Infoln(args ...interface{}) {
slw.Println(args...)
}
func (slw *stdlogAdapter) Infof(format string, args ...interface{}) {
slw.Printf(format, args...)
}
func (slw *stdlogAdapter) Errorln(args ...interface{}) {
slw.Println(args...)
}
func (slw *stdlogAdapter) Errorf(format string, args ...interface{}) {
slw.Printf(format, args...)
}