logger
This commit is contained in:
54
bot.go
54
bot.go
@@ -217,32 +217,31 @@ func (b *Bot) Run() {
|
||||
}
|
||||
|
||||
u := queue.Dequeue()
|
||||
ctx := &MsgContext{
|
||||
Bot: b,
|
||||
Update: u,
|
||||
}
|
||||
for _, middleware := range b.middlewares {
|
||||
middleware.Execute(ctx, b.dbContext)
|
||||
}
|
||||
|
||||
for _, plugin := range b.plugins {
|
||||
if plugin.UpdateListener != nil {
|
||||
(*plugin.UpdateListener)(ctx, b.dbContext)
|
||||
}
|
||||
}
|
||||
|
||||
if u.CallbackQuery != nil {
|
||||
b.handleCallback(u)
|
||||
b.handleCallback(u, ctx)
|
||||
} else {
|
||||
b.handleMessage(u)
|
||||
b.handleMessage(u, ctx)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// {"callback_query":{"chat_instance":"6202057960757700762","data":"aboba","from":{"first_name":"scuroneko","id":314834933,"is_bot":false,"language_code":"ru","username":"scuroneko"},"id":"1352205741990111553","message":{"chat":{"first_name":"scuroneko","id":314834933,"type":"private","username":"scuroneko"},"date":1734338107,"from":{"first_name":"Kurumi","id":7718900880,"is_bot":true,"username":"kurumi_game_bot"},"message_id":19,"reply_markup":{"inline_keyboard":[[{"callback_data":"aboba","text":"Test"},{"callback_data":"another","text":"Another"}]]},"text":"Aboba"}},"update_id":350979488}
|
||||
|
||||
func (b *Bot) handleMessage(update *Update) {
|
||||
ctx := &MsgContext{
|
||||
Bot: b,
|
||||
Update: update,
|
||||
}
|
||||
|
||||
for _, middleware := range b.middlewares {
|
||||
middleware.Execute(ctx, b.dbContext)
|
||||
}
|
||||
|
||||
for _, plugin := range b.plugins {
|
||||
if plugin.UpdateListener != nil {
|
||||
(*plugin.UpdateListener)(ctx, b.dbContext)
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bot) handleMessage(update *Update, ctx *MsgContext) {
|
||||
var text string
|
||||
if update.Message == nil {
|
||||
return
|
||||
@@ -265,7 +264,6 @@ func (b *Bot) handleMessage(update *Update) {
|
||||
text = strings.TrimSpace(text[len(prefix):])
|
||||
|
||||
for _, plugin := range b.plugins {
|
||||
|
||||
// Check every command
|
||||
for cmd := range plugin.Commands {
|
||||
if !strings.HasPrefix(text, cmd) {
|
||||
@@ -280,23 +278,7 @@ func (b *Bot) handleMessage(update *Update) {
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bot) handleCallback(update *Update) {
|
||||
ctx := &MsgContext{
|
||||
Bot: b,
|
||||
Update: update,
|
||||
}
|
||||
|
||||
for _, m := range b.middlewares {
|
||||
m.Execute(ctx, b.dbContext)
|
||||
}
|
||||
|
||||
for _, plugin := range b.plugins {
|
||||
if plugin.UpdateListener != nil {
|
||||
lis := *plugin.UpdateListener
|
||||
lis(ctx, b.dbContext)
|
||||
}
|
||||
}
|
||||
|
||||
func (b *Bot) handleCallback(update *Update, ctx *MsgContext) {
|
||||
for _, plugin := range b.plugins {
|
||||
for payload := range plugin.Payloads {
|
||||
if !strings.HasPrefix(update.CallbackQuery.Data, payload) {
|
||||
|
||||
44
logger.go
44
logger.go
@@ -5,6 +5,7 @@ import (
|
||||
"os"
|
||||
"path/filepath"
|
||||
"runtime"
|
||||
"sort"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
@@ -142,6 +143,47 @@ func (l *Logger) formatTraceback(mt *MethodTraceback) string {
|
||||
return fmt.Sprintf("%s:%s:%d", mt.filename, mt.Method, mt.line)
|
||||
}
|
||||
|
||||
func (l *Logger) getFullTraceback(skip int) []*MethodTraceback {
|
||||
pc := make([]uintptr, 15)
|
||||
runtime.Callers(skip, pc)
|
||||
list := make([]*MethodTraceback, 0)
|
||||
frames := runtime.CallersFrames(pc)
|
||||
for {
|
||||
frame, more := frames.Next()
|
||||
if !more {
|
||||
break
|
||||
}
|
||||
details := runtime.FuncForPC(frame.PC)
|
||||
signature := details.Name()
|
||||
path, line := details.FileLine(frame.PC)
|
||||
splitPath := strings.Split(path, "/")
|
||||
|
||||
splitSignature := strings.Split(signature, ".")
|
||||
pkg, method := splitSignature[0], splitSignature[len(splitSignature)-1]
|
||||
|
||||
tb := &MethodTraceback{
|
||||
filename: splitPath[len(splitPath)-1],
|
||||
fullPath: path,
|
||||
line: line,
|
||||
signature: signature,
|
||||
Package: pkg,
|
||||
Method: method,
|
||||
}
|
||||
list = append(list, tb)
|
||||
}
|
||||
sort.Slice(list, func(i, j int) bool {
|
||||
return j < i
|
||||
})
|
||||
return list
|
||||
}
|
||||
func (l *Logger) formatFullTraceback(tracebacks []*MethodTraceback) string {
|
||||
formatted := make([]string, 0)
|
||||
for _, tb := range tracebacks {
|
||||
formatted = append(formatted, l.formatTraceback(tb))
|
||||
}
|
||||
return strings.Join(formatted, "->")
|
||||
}
|
||||
|
||||
func (l *Logger) buildString(level LogLevel, m []any) string {
|
||||
args := []string{
|
||||
fmt.Sprintf("[%s]", l.prefix),
|
||||
@@ -174,7 +216,7 @@ func (l *Logger) print(level LogLevel, m []any) {
|
||||
}
|
||||
|
||||
for _, writer := range l.writers {
|
||||
writer(level, l.prefix, l.formatTraceback(l.getTraceback()), m)
|
||||
writer(level, l.prefix, l.formatFullTraceback(l.getFullTraceback(4)), m)
|
||||
}
|
||||
|
||||
if l.f != nil {
|
||||
|
||||
Reference in New Issue
Block a user