logger
This commit is contained in:
44
bot.go
44
bot.go
@@ -217,22 +217,10 @@ func (b *Bot) Run() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
u := queue.Dequeue()
|
u := queue.Dequeue()
|
||||||
if u.CallbackQuery != nil {
|
|
||||||
b.handleCallback(u)
|
|
||||||
} else {
|
|
||||||
b.handleMessage(u)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// {"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{
|
ctx := &MsgContext{
|
||||||
Bot: b,
|
Bot: b,
|
||||||
Update: update,
|
Update: u,
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, middleware := range b.middlewares {
|
for _, middleware := range b.middlewares {
|
||||||
middleware.Execute(ctx, b.dbContext)
|
middleware.Execute(ctx, b.dbContext)
|
||||||
}
|
}
|
||||||
@@ -243,6 +231,17 @@ func (b *Bot) handleMessage(update *Update) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if u.CallbackQuery != nil {
|
||||||
|
b.handleCallback(u, ctx)
|
||||||
|
} else {
|
||||||
|
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) {
|
||||||
var text string
|
var text string
|
||||||
if update.Message == nil {
|
if update.Message == nil {
|
||||||
return
|
return
|
||||||
@@ -265,7 +264,6 @@ func (b *Bot) handleMessage(update *Update) {
|
|||||||
text = strings.TrimSpace(text[len(prefix):])
|
text = strings.TrimSpace(text[len(prefix):])
|
||||||
|
|
||||||
for _, plugin := range b.plugins {
|
for _, plugin := range b.plugins {
|
||||||
|
|
||||||
// Check every command
|
// Check every command
|
||||||
for cmd := range plugin.Commands {
|
for cmd := range plugin.Commands {
|
||||||
if !strings.HasPrefix(text, cmd) {
|
if !strings.HasPrefix(text, cmd) {
|
||||||
@@ -280,23 +278,7 @@ func (b *Bot) handleMessage(update *Update) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (b *Bot) handleCallback(update *Update) {
|
func (b *Bot) handleCallback(update *Update, ctx *MsgContext) {
|
||||||
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)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, plugin := range b.plugins {
|
for _, plugin := range b.plugins {
|
||||||
for payload := range plugin.Payloads {
|
for payload := range plugin.Payloads {
|
||||||
if !strings.HasPrefix(update.CallbackQuery.Data, payload) {
|
if !strings.HasPrefix(update.CallbackQuery.Data, payload) {
|
||||||
|
|||||||
44
logger.go
44
logger.go
@@ -5,6 +5,7 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
|
"sort"
|
||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -142,6 +143,47 @@ func (l *Logger) formatTraceback(mt *MethodTraceback) string {
|
|||||||
return fmt.Sprintf("%s:%s:%d", mt.filename, mt.Method, mt.line)
|
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 {
|
func (l *Logger) buildString(level LogLevel, m []any) string {
|
||||||
args := []string{
|
args := []string{
|
||||||
fmt.Sprintf("[%s]", l.prefix),
|
fmt.Sprintf("[%s]", l.prefix),
|
||||||
@@ -174,7 +216,7 @@ func (l *Logger) print(level LogLevel, m []any) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
for _, writer := range l.writers {
|
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 {
|
if l.f != nil {
|
||||||
|
|||||||
Reference in New Issue
Block a user