From 7dc3eeae9da3a520b91b2accac908591d56aa579 Mon Sep 17 00:00:00 2001 From: ScuroNeko Date: Mon, 29 Sep 2025 11:05:52 +0300 Subject: [PATCH] logger --- database/mdb/logs.go | 20 +++++++++++++++++++- main.go | 1 + main.log | 30 ++++++++++++++++++++++++++++++ plugins/logs.go | 24 +++++++++++++++++++++++- requests.log | 26 ++++++++++++++++++++++++++ 5 files changed, 99 insertions(+), 2 deletions(-) diff --git a/database/mdb/logs.go b/database/mdb/logs.go index 00c8fb0..5c7421d 100644 --- a/database/mdb/logs.go +++ b/database/mdb/logs.go @@ -3,9 +3,27 @@ package mdb import ( "context" "kurumibot/laniakea" + "os" "time" ) +type ConsoleLogEntry struct { + Level string `bson:"level"` + Prefix string `bson:"prefix"` + Traceback string `bson:"traceback"` + Message string `bson:"message"` + Time time.Time `bson:"time"` + TimeStamp int64 `bson:"time_stamp"` +} + +func WriteConsoleLog(db *laniakea.DatabaseContext, e *ConsoleLogEntry) error { + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + col := db.MongoDB.Database(os.Getenv("MONGO_NAME")).Collection("logs") + _, err := col.InsertOne(ctx, e) + return err +} + type MessageLogEntry struct { MessageID int `bson:"messageId"` SenderID int `bson:"senderId"` @@ -17,7 +35,7 @@ type MessageLogEntry struct { func WriteMessageLog(db *laniakea.DatabaseContext, e *MessageLogEntry) error { ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) defer cancel() - collection := db.MongoDB.Database("kurumi").Collection("msg_logs") + collection := db.MongoDB.Database(os.Getenv("MONGO_NAME")).Collection("msg_logs") _, err := collection.InsertOne(ctx, e) return err } diff --git a/main.go b/main.go index 03a1557..350c5a8 100644 --- a/main.go +++ b/main.go @@ -32,6 +32,7 @@ func main() { } bot = bot.ErrorTemplate("Во время выполнения команды произошла ошибка!\nСообщите об этом разработчику!\n\n%s") bot = bot.InitDatabaseContext(dbCtx) + bot.AddDatabaseLogger(plugins.DatabaseLogger) bot.AddMiddleware(plugins.InitLogMiddleware()) diff --git a/main.log b/main.log index 3e45c3f..6221a77 100644 --- a/main.log +++ b/main.log @@ -24,3 +24,33 @@ [LOG] [DEBUG] [29.09.2025 09:13:53] plugins with name "Waifus" was registered [LOG] [DEBUG] [29.09.2025 09:13:53] plugins with name "Admin" was registered [LOG] [INFO] [29.09.2025 09:13:53] Bot running. Press CTRL+C to exit. +[LOG] [DEBUG] [29.09.2025 10:41:45] middleware with name "LogMiddleware" was registered +[LOG] [DEBUG] [29.09.2025 10:41:45] plugins with name "Economy" was registered +[LOG] [DEBUG] [29.09.2025 10:41:45] plugins with name "Waifus" was registered +[LOG] [DEBUG] [29.09.2025 10:41:45] plugins with name "Admin" was registered +[LOG] [INFO] [29.09.2025 10:41:45] Bot running. Press CTRL+C to exit. +[LOG] [DEBUG] [29.09.2025 10:42:50] middleware with name "LogMiddleware" was registered +[LOG] [DEBUG] [29.09.2025 10:42:50] plugins with name "Economy" was registered +[LOG] [DEBUG] [29.09.2025 10:42:50] plugins with name "Waifus" was registered +[LOG] [DEBUG] [29.09.2025 10:42:50] plugins with name "Admin" was registered +[LOG] [INFO] [29.09.2025 10:42:50] Bot running. Press CTRL+C to exit. +[LOG] [DEBUG] [bot.go:AddMiddleware:182] [29.09.2025 10:43:38] middleware with name "LogMiddleware" was registered +[LOG] [DEBUG] [bot.go:AddPlugins:165] [29.09.2025 10:43:38] plugins with name "Economy" was registered +[LOG] [DEBUG] [bot.go:AddPlugins:165] [29.09.2025 10:43:38] plugins with name "Waifus" was registered +[LOG] [DEBUG] [bot.go:AddPlugins:165] [29.09.2025 10:43:38] plugins with name "Admin" was registered +[LOG] [INFO] [bot.go:Run:198] [29.09.2025 10:43:38] Bot running. Press CTRL+C to exit. +[LOG] [DEBUG] [bot.go:AddMiddleware:182] [29.09.2025 10:44:00] middleware with name "LogMiddleware" was registered +[LOG] [DEBUG] [bot.go:AddPlugins:165] [29.09.2025 10:44:00] plugins with name "Economy" was registered +[LOG] [DEBUG] [bot.go:AddPlugins:165] [29.09.2025 10:44:00] plugins with name "Waifus" was registered +[LOG] [DEBUG] [bot.go:AddPlugins:165] [29.09.2025 10:44:00] plugins with name "Admin" was registered +[LOG] [INFO] [bot.go:Run:198] [29.09.2025 10:44:00] Bot running. Press CTRL+C to exit. +[LOG] [DEBUG] [bot.go:AddMiddleware:182] [29.09.2025 10:51:46] middleware with name "LogMiddleware" was registered +[LOG] [DEBUG] [bot.go:AddPlugins:165] [29.09.2025 10:51:46] plugins with name "Economy" was registered +[LOG] [DEBUG] [bot.go:AddPlugins:165] [29.09.2025 10:51:46] plugins with name "Waifus" was registered +[LOG] [DEBUG] [bot.go:AddPlugins:165] [29.09.2025 10:51:46] plugins with name "Admin" was registered +[LOG] [INFO] [bot.go:Run:198] [29.09.2025 10:51:46] Bot running. Press CTRL+C to exit. +[LOG] [DEBUG] [bot.go:AddMiddleware:186] [29.09.2025 10:52:42] middleware with name "LogMiddleware" was registered +[LOG] [DEBUG] [bot.go:AddPlugins:169] [29.09.2025 10:52:42] plugins with name "Economy" was registered +[LOG] [DEBUG] [bot.go:AddPlugins:169] [29.09.2025 10:52:42] plugins with name "Waifus" was registered +[LOG] [DEBUG] [bot.go:AddPlugins:169] [29.09.2025 10:52:42] plugins with name "Admin" was registered +[LOG] [INFO] [bot.go:Run:202] [29.09.2025 10:52:42] Bot running. Press CTRL+C to exit. diff --git a/plugins/logs.go b/plugins/logs.go index 5787b27..f416199 100644 --- a/plugins/logs.go +++ b/plugins/logs.go @@ -4,6 +4,7 @@ import ( "fmt" "kurumibot/database/mdb" "kurumibot/laniakea" + "strings" "time" ) @@ -23,5 +24,26 @@ func logMiddleware(ctx *laniakea.MsgContext, db *laniakea.DatabaseContext) { TimeStamp: time.Now().Unix(), } err := mdb.WriteMessageLog(db, entry) - fmt.Println(err) + if err != nil { + fmt.Println(err) + } +} +func DatabaseLogger(db *laniakea.DatabaseContext) laniakea.LoggerWriter { + return func(level laniakea.LogLevel, prefix, traceback string, m []any) { + t := time.Now() + entry := &mdb.ConsoleLogEntry{ + Level: level.GetName(), + Prefix: prefix, + Traceback: traceback, + Message: strings.Join(laniakea.Map(m, func(el any) string { + return fmt.Sprintf("%v", el) + }), "\n"), + Time: t, + TimeStamp: t.Unix(), + } + err := mdb.WriteConsoleLog(db, entry) + if err != nil { + fmt.Println(err) + } + } } diff --git a/requests.log b/requests.log index 47c4725..9d9f931 100644 --- a/requests.log +++ b/requests.log @@ -28,3 +28,29 @@ [REQUESTS] [DEBUG] [29.09.2025 09:13:58] UPDATE {"message":{"chat":{"first_name":"scuroneko","id":314834933,"type":"private","username":"scuroneko"},"date":1759126435,"entities":[{"length":8,"offset":0,"type":"bot_command"}],"from":{"first_name":"scuroneko","id":314834933,"is_bot":false,"language_code":"ru","username":"scuroneko"},"message_id":590,"text":"/profile"},"update_id":475625438} [REQUESTS] [DEBUG] [29.09.2025 09:13:58] POST https://api.telegram.org/bot/getUpdates {"allowed_updates":[],"offset":475625439,"timeout":30} [REQUESTS] [DEBUG] [29.09.2025 09:13:58] POST https://api.telegram.org/bot/sendMessage {"chat_id":314834933,"parse_mode":"markdown","text":"🖤, scuroneko🖤\n🆔ID: 1\n🔰Фракция: нет\n📊Группа: ✨Пользователь✨ (ID: 1, x1.0)\n❤️Пара: нет\n💡Уровень: 1 (0 опыта, 48 опыта до повышения)\n💴Баланс: 0¥ (0 ), 0₿\n💼Работа: Уборщик в маке (ID: 1)\n🚘Авто: нет\n🏢Бизнес: нет\n👩‍🦳Горничная: нет\n🖥Майнер: нет"} +[REQUESTS] [DEBUG] [29.09.2025 10:41:45] POST https://api.telegram.org/bot/getUpdates {"allowed_updates":[],"offset":0,"timeout":30} +[REQUESTS] [DEBUG] [29.09.2025 10:42:15] POST https://api.telegram.org/bot/getUpdates {"allowed_updates":[],"offset":0,"timeout":30} +[REQUESTS] [DEBUG] [29.09.2025 10:42:45] POST https://api.telegram.org/bot/getUpdates {"allowed_updates":[],"offset":0,"timeout":30} +[REQUESTS] [DEBUG] [29.09.2025 10:42:50] POST https://api.telegram.org/bot/getUpdates {"allowed_updates":[],"offset":0,"timeout":30} +[REQUESTS] [DEBUG] [29.09.2025 10:43:20] POST https://api.telegram.org/bot/getUpdates {"allowed_updates":[],"offset":0,"timeout":30} +[REQUESTS] [DEBUG] [29.09.2025 10:43:38] POST https://api.telegram.org/bot/getUpdates {"allowed_updates":[],"offset":0,"timeout":30} +[REQUESTS] [DEBUG] [29.09.2025 10:44:00] POST https://api.telegram.org/bot/getUpdates {"allowed_updates":[],"offset":0,"timeout":30} +[REQUESTS] [DEBUG] [29.09.2025 10:44:31] POST https://api.telegram.org/bot/getUpdates {"allowed_updates":[],"offset":0,"timeout":30} +[REQUESTS] [DEBUG] [29.09.2025 10:45:01] POST https://api.telegram.org/bot/getUpdates {"allowed_updates":[],"offset":0,"timeout":30} +[REQUESTS] [DEBUG] [29.09.2025 10:45:31] POST https://api.telegram.org/bot/getUpdates {"allowed_updates":[],"offset":0,"timeout":30} +[REQUESTS] [DEBUG] [29.09.2025 10:46:01] POST https://api.telegram.org/bot/getUpdates {"allowed_updates":[],"offset":0,"timeout":30} +[REQUESTS] [DEBUG] [29.09.2025 10:46:31] POST https://api.telegram.org/bot/getUpdates {"allowed_updates":[],"offset":0,"timeout":30} +[REQUESTS] [DEBUG] [29.09.2025 10:47:01] POST https://api.telegram.org/bot/getUpdates {"allowed_updates":[],"offset":0,"timeout":30} +[REQUESTS] [DEBUG] [29.09.2025 10:47:31] POST https://api.telegram.org/bot/getUpdates {"allowed_updates":[],"offset":0,"timeout":30} +[REQUESTS] [DEBUG] [29.09.2025 10:48:01] POST https://api.telegram.org/bot/getUpdates {"allowed_updates":[],"offset":0,"timeout":30} +[REQUESTS] [DEBUG] [29.09.2025 10:48:31] POST https://api.telegram.org/bot/getUpdates {"allowed_updates":[],"offset":0,"timeout":30} +[REQUESTS] [DEBUG] [29.09.2025 10:49:01] POST https://api.telegram.org/bot/getUpdates {"allowed_updates":[],"offset":0,"timeout":30} +[REQUESTS] [DEBUG] [29.09.2025 10:49:31] POST https://api.telegram.org/bot/getUpdates {"allowed_updates":[],"offset":0,"timeout":30} +[REQUESTS] [DEBUG] [29.09.2025 10:50:01] POST https://api.telegram.org/bot/getUpdates {"allowed_updates":[],"offset":0,"timeout":30} +[REQUESTS] [DEBUG] [29.09.2025 10:50:31] POST https://api.telegram.org/bot/getUpdates {"allowed_updates":[],"offset":0,"timeout":30} +[REQUESTS] [DEBUG] [29.09.2025 10:51:01] POST https://api.telegram.org/bot/getUpdates {"allowed_updates":[],"offset":0,"timeout":30} +[REQUESTS] [DEBUG] [29.09.2025 10:51:32] POST https://api.telegram.org/bot/getUpdates {"allowed_updates":[],"offset":0,"timeout":30} +[REQUESTS] [DEBUG] [29.09.2025 10:51:46] POST https://api.telegram.org/bot/getUpdates {"allowed_updates":[],"offset":0,"timeout":30} +[REQUESTS] [DEBUG] [29.09.2025 10:52:16] POST https://api.telegram.org/bot/getUpdates {"allowed_updates":[],"offset":0,"timeout":30} +[REQUESTS] [DEBUG] [29.09.2025 10:52:42] POST https://api.telegram.org/bot/getUpdates {"allowed_updates":[],"offset":0,"timeout":30} +[REQUESTS] [DEBUG] [29.09.2025 10:53:13] POST https://api.telegram.org/bot/getUpdates {"allowed_updates":[],"offset":0,"timeout":30}