Добавлено: логирование в файл
This commit is contained in:
parent
3f4742dfb5
commit
38b9aede57
6
main.go
6
main.go
|
@ -1,6 +1,7 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"os"
|
"os"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -24,6 +25,7 @@ func main() {
|
||||||
bot := new(tg.Bot)
|
bot := new(tg.Bot)
|
||||||
bot.Week = 5
|
bot.Week = 5
|
||||||
bot.WkPath = os.Getenv("WK_PATH")
|
bot.WkPath = os.Getenv("WK_PATH")
|
||||||
|
bot.Debug = log.New(io.MultiWriter(os.Stderr, database.CreateLog("messages")), "", log.LstdFlags)
|
||||||
err = bot.InitBot(os.Getenv("TELEGRAM_APITOKEN"), *engine)
|
err = bot.InitBot(os.Getenv("TELEGRAM_APITOKEN"), *engine)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Fatal(err)
|
log.Fatal(err)
|
||||||
|
@ -34,7 +36,7 @@ func main() {
|
||||||
for update := range *updates {
|
for update := range *updates {
|
||||||
if update.Message != nil {
|
if update.Message != nil {
|
||||||
msg := update.Message
|
msg := update.Message
|
||||||
log.Printf("Message [%s] %s", msg.From.UserName, msg.Text)
|
bot.Debug.Printf("Message [%s] %s", msg.From.UserName, msg.Text)
|
||||||
|
|
||||||
tg_user, err := bot.InitUser(msg.From.ID, msg.From.UserName)
|
tg_user, err := bot.InitUser(msg.From.ID, msg.From.UserName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -55,7 +57,7 @@ func main() {
|
||||||
|
|
||||||
if update.CallbackQuery != nil {
|
if update.CallbackQuery != nil {
|
||||||
query := update.CallbackQuery
|
query := update.CallbackQuery
|
||||||
log.Printf("Callback [%s] %s", query.From.UserName, query.Data)
|
bot.Debug.Printf("Callback [%s] %s", query.From.UserName, query.Data)
|
||||||
|
|
||||||
tg_user, err := bot.InitUser(query.From.ID, query.From.UserName)
|
tg_user, err := bot.InitUser(query.From.ID, query.From.UserName)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -1,10 +1,13 @@
|
||||||
package database
|
package database
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"log"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
|
"os"
|
||||||
|
|
||||||
_ "github.com/go-sql-driver/mysql"
|
_ "github.com/go-sql-driver/mysql"
|
||||||
"xorm.io/xorm"
|
"xorm.io/xorm"
|
||||||
|
xlog "xorm.io/xorm/log"
|
||||||
"xorm.io/xorm/names"
|
"xorm.io/xorm/names"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -13,7 +16,8 @@ func Connect(user, pass, db string) (*xorm.Engine, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
sqlLogger := xlog.NewSimpleLogger(CreateLog("sql"))
|
||||||
|
engine.SetLogger(sqlLogger)
|
||||||
engine.ShowSQL(true)
|
engine.ShowSQL(true)
|
||||||
engine.SetMapper(names.SameMapper{})
|
engine.SetMapper(names.SameMapper{})
|
||||||
|
|
||||||
|
@ -38,3 +42,18 @@ func GenerateID(engine *xorm.Engine) (int64, error) {
|
||||||
return id, nil
|
return id, nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func CreateLog(name string) *os.File {
|
||||||
|
if _, err := os.Stat("logs"); os.IsNotExist(err) {
|
||||||
|
err = os.Mkdir("logs", os.ModePerm)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Fail to create log folder")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
fileName := "./logs/" + name + ".log"
|
||||||
|
logFile, err := os.Create(fileName)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("Fail to open tg.log file")
|
||||||
|
}
|
||||||
|
return logFile
|
||||||
|
}
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
package tg
|
package tg
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
|
"os"
|
||||||
|
|
||||||
"git.l9labs.ru/anufriev.g.a/l9_stud_bot/modules/database"
|
"git.l9labs.ru/anufriev.g.a/l9_stud_bot/modules/database"
|
||||||
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api/v5"
|
||||||
|
@ -14,6 +16,7 @@ type Bot struct {
|
||||||
TG_user database.TgUser
|
TG_user database.TgUser
|
||||||
Week int
|
Week int
|
||||||
WkPath string
|
WkPath string
|
||||||
|
Debug *log.Logger
|
||||||
}
|
}
|
||||||
|
|
||||||
func (bot *Bot) InitBot(token string, engine xorm.Engine) error {
|
func (bot *Bot) InitBot(token string, engine xorm.Engine) error {
|
||||||
|
@ -24,6 +27,9 @@ func (bot *Bot) InitBot(token string, engine xorm.Engine) error {
|
||||||
}
|
}
|
||||||
bot.TG.Debug = true
|
bot.TG.Debug = true
|
||||||
|
|
||||||
|
logger := log.New(io.MultiWriter(os.Stderr, database.CreateLog("tg")), "", log.LstdFlags)
|
||||||
|
tgbotapi.SetLogger(logger)
|
||||||
|
|
||||||
bot.DB = &engine
|
bot.DB = &engine
|
||||||
|
|
||||||
log.Printf("Authorized on account %s", bot.TG.Self.UserName)
|
log.Printf("Authorized on account %s", bot.TG.Self.UserName)
|
||||||
|
|
Reference in New Issue