forked from go-telegram-bot-api/telegram-bot-api
-
Notifications
You must be signed in to change notification settings - Fork 52
Expand file tree
/
Copy pathlog.go
More file actions
141 lines (127 loc) · 3.75 KB
/
Copy pathlog.go
File metadata and controls
141 lines (127 loc) · 3.75 KB
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
package tgbotapi
import (
"context"
"errors"
stdlog "log"
"log/slog"
"os"
"strings"
)
// 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.
type BotLogger interface {
Println(v ...any)
Printf(format string, v ...any)
}
var log BotLogger = stdlog.New(os.Stderr, "", stdlog.LstdFlags)
// SetLogger specifies the logger that the package should use.
//
// Deprecated: Use [NewBotAPIWithOptions] with [WithLogger] instead.
// [SetLogger] will be no-operation later and removed in future.
func SetLogger(logger BotLogger) error {
if logger == nil {
return errors.New("logger is nil")
}
log = logger
return nil
}
func (bot *BotAPI) debugLoggingEnabled() bool {
return bot.Debug && !bot.loggingDisabled
}
func (bot *BotAPI) logDebug(ctx context.Context, msg string, attrs ...slog.Attr) {
if !bot.debugLoggingEnabled() {
return
}
bot.logMessage(ctx, slog.LevelDebug, msg, attrs...)
}
func (bot *BotAPI) logRequestDebug(ctx context.Context, endpoint string, debugInfo requestDebug) {
if !bot.debugLoggingEnabled() {
return
}
switch logger := bot.logger.(type) {
case BotLogger:
if debugInfo.fileCount > 0 {
logger.Printf("[DEBUG] Endpoint: %s, params: %v, with %d files", endpoint, debugInfo.params, debugInfo.fileCount)
return
}
logger.Printf("[DEBUG] Endpoint: %s, params: %v", endpoint, debugInfo.params)
case *slog.Logger:
args := make([]any, 0, 6)
args = append(args,
"endpoint", endpoint,
"params", debugInfo.params,
)
if debugInfo.fileCount > 0 {
args = append(args, "file_count", debugInfo.fileCount)
}
logger.DebugContext(ctx, "telegram request", args...)
default:
if debugInfo.fileCount > 0 {
log.Printf("[DEBUG] Endpoint: %s, params: %v, with %d files", endpoint, debugInfo.params, debugInfo.fileCount)
return
}
log.Printf("[DEBUG] Endpoint: %s, params: %v", endpoint, debugInfo.params)
}
}
func (bot *BotAPI) logResponseDebug(ctx context.Context, endpoint string, response string) {
if !bot.debugLoggingEnabled() {
return
}
switch logger := bot.logger.(type) {
case BotLogger:
logger.Printf("[DEBUG] Endpoint: %s, response: %s", endpoint, response)
case *slog.Logger:
logger.DebugContext(ctx, "telegram response",
"endpoint", endpoint,
"response", response,
)
default:
log.Printf("[DEBUG] Endpoint: %s, response: %s", endpoint, response)
}
}
func (bot *BotAPI) logUpdateError(ctx context.Context, err error) {
if bot.loggingDisabled {
return
}
switch logger := bot.logger.(type) {
case BotLogger:
logger.Printf("[DEBUG] Failed to get updates (%s), retrying in 3 seconds...", err)
case *slog.Logger:
logger.ErrorContext(ctx, "telegram get updates failed",
"error", err,
)
logger.InfoContext(ctx, "telegram get updates retry scheduled",
"delay", "3s",
)
default:
log.Printf("[DEBUG] Failed to get updates (%s), retrying in 3 seconds...", err)
}
}
func (bot *BotAPI) logMessage(ctx context.Context, level slog.Level, msg string, attrs ...slog.Attr) {
switch logger := bot.logger.(type) {
case BotLogger:
if len(attrs) == 0 {
logger.Printf("[%s] %s", level.String(), msg)
return
}
args := make([]string, 0, len(attrs))
for _, attr := range attrs {
args = append(args, attr.String())
}
logger.Printf("[%s] %s (%s)", level.String(), msg, strings.Join(args, " "))
case *slog.Logger:
logger.LogAttrs(ctx, level, msg, attrs...)
default:
if len(attrs) == 0 {
log.Printf("[%s] %s", level.String(), msg)
return
}
args := make([]string, 0, len(attrs))
for _, attr := range attrs {
args = append(args, attr.String())
}
log.Printf("[%s] %s (%s)", level.String(), msg, strings.Join(args, " "))
}
}