-
Notifications
You must be signed in to change notification settings - Fork 3
/
logging.go
142 lines (118 loc) · 3.5 KB
/
logging.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
137
138
139
140
141
142
package tbcomctl
import (
"context"
"encoding/json"
"fmt"
"strings"
"time"
"github.com/rusq/dlog"
tb "gopkg.in/telebot.v3"
)
const (
None = "<none>"
notAvailable = "N/A"
chatPrivate = "private"
)
var (
// lg is the package logger.
lg Logger = dlog.FromContext(context.Background()) // getting default logger
// dlg is the debug logger.
dlg Logger = blackholeLogger{}
)
// Logger is the interface for logging.
type Logger interface {
Print(v ...interface{})
Println(v ...interface{})
Printf(format string, a ...interface{})
}
// SenderInfo is the convenience function to log the sender info in the context.
func SenderInfo(c tb.Context) string {
return Userinfo(c.Sender())
}
// Userinfo returns the user info.
func Userinfo(u *tb.User) string {
if u == nil {
return None
}
return fmt.Sprintf("<[ID %d] %s (%s)>", u.ID, u.Username, Nvlstring(u.LanguageCode, notAvailable))
}
// ChatInfo returns the chat info.
func ChatInfo(ch *tb.Chat) string {
if ch == nil {
return None
}
if ch.Type == chatPrivate {
return chatPrivate + ":" + Userinfo(&tb.User{
ID: ch.ID,
FirstName: ch.FirstName,
LastName: ch.LastName,
Username: ch.Username,
})
}
var title = ""
if ch.Title != "" {
title = fmt.Sprintf(" (%q)", ch.Title)
}
return fmt.Sprintf("<[%d] %s%s>", ch.ID, ch.Type, title)
}
// Sdump dumps the structure.
func Sdump(m interface{}) string {
var buf strings.Builder
enc := json.NewEncoder(&buf)
enc.SetIndent("", " ")
if err := enc.Encode(m); err != nil {
lg.Println("failed to marshal")
}
return buf.String()
}
// SetLogger sets the current logger.
func SetLogger(l Logger) {
if l == nil {
return
}
lg = l
}
// SetDebugLogger sets the debug logger which is used to output debug messages,
// if you must. By default, debug logging is disabled.
func SetDebugLogger(l Logger) {
if l == nil {
return
}
dlg = l
}
// NoDebugLogger switches off debug messages.
func NoDebugLogger() {
dlg = blackholeLogger{}
}
// GetLogger returns current logger.
func GetLogger() Logger {
return lg
}
// NoLogging switches off default logging, if you're brave.
func NoLogging() {
lg = blackholeLogger{}
}
// blackholeLogger is the logger that outputs nothing.
type blackholeLogger struct{}
func (blackholeLogger) Print(v ...interface{}) {}
func (blackholeLogger) Println(v ...interface{}) {}
func (blackholeLogger) Printf(format string, a ...interface{}) {}
// logCallback logs callback data.
func (cc *commonCtl) logCallback(cb *tb.Callback) {
dlg.Printf("%s: callback dump: %s", Userinfo(cb.Sender), Sdump(cb))
reqID, at := cc.reg.RequestInfo(cb.Sender, cb.Message.ID)
lg.Printf("%s> %s: msg sent at %s, user response in: %s, callback data: %q", reqID, Userinfo(cb.Sender), at, time.Since(at), cb.Data)
}
// logCallback logs callback data.
func (cc *commonCtl) logCallbackMsg(m *tb.Message) {
dlg.Printf("%s: callback msg dump: %s", Userinfo(m.Sender), Sdump(m))
outboundID := cc.reg.WaitMsgID(m.Sender)
reqID, at := cc.reg.RequestInfo(m.Sender, outboundID)
lg.Printf("%s> %s: msg sent at %s, user response in: %s, message data: %q", reqID, Userinfo(m.Sender), at, time.Since(at), m.Text)
}
// logOutgoingMsg logs the outgoing message and any additional string info passed in s.
func (cc *commonCtl) logOutgoingMsg(m *tb.Message, s ...string) {
dlg.Printf("%s: message dump: %s", Userinfo(m.Sender), Sdump(m))
reqID, at := cc.reg.RequestInfo(m.Chat, m.ID)
lg.Printf("%s> msg to chat: %s, req time: %s: %s", reqID, ChatInfo(m.Chat), at, strings.Join(s, " "))
}