0

Logrus заменён на штатный log/slog, обновлены зависимости

This commit is contained in:
eleksir
2025-10-07 23:59:18 +03:00
parent e000489790
commit a49abdd715
380 changed files with 105 additions and 224708 deletions

View File

@@ -3,9 +3,9 @@ package misc
import (
"context"
"encoding/json"
"fmt"
"log/slog"
"strings"
log "github.com/sirupsen/logrus"
)
// MsgParser горутинка, которая парсит json-чики прилетевшие из REDIS-ки.
@@ -15,29 +15,29 @@ func MsgParser(ctx context.Context, msg string) {
j rMsg
)
log.Debugf("Incomming raw json: %s", msg)
slog.Debug(fmt.Sprintf("Incomming raw json: %s", msg))
if err := json.Unmarshal([]byte(msg), &j); err != nil {
log.Warnf("Unable to to parse message from redis channel: %s", err)
slog.Warn(fmt.Sprintf("Unable to to parse message from redis channel: %s", err))
return
}
// Validate our j.
if exist := j.From; exist == "" {
log.Warnf("Incorrect msg from redis, no from field: %s", msg)
slog.Warn(fmt.Sprintf("Incorrect msg from redis, no from field: %s", msg))
return
}
if exist := j.Chatid; exist == "" {
log.Warnf("Incorrect msg from redis, no chatid field: %s", msg)
slog.Warn(fmt.Sprintf("Incorrect msg from redis, no chatid field: %s", msg))
return
}
if exist := j.Userid; exist == "" {
log.Warnf("Incorrect msg from redis, no userid field: %s", msg)
slog.Warn(fmt.Sprintf("Incorrect msg from redis, no userid field: %s", msg))
return
}
@@ -45,19 +45,19 @@ func MsgParser(ctx context.Context, msg string) {
// j.Threadid может быть пустым, значит либо нам его не дали, либо дали пустым. Это нормально.
if exist := j.Message; exist == "" {
log.Warnf("Incorrect msg from redis, no message field: %s", msg)
slog.Warn(fmt.Sprintf("Incorrect msg from redis, no message field: %s", msg))
return
}
if exist := j.Plugin; exist == "" {
log.Warnf("Incorrect msg from redis, no plugin field: %s", msg)
slog.Warn(fmt.Sprintf("Incorrect msg from redis, no plugin field: %s", msg))
return
}
if exist := j.Mode; exist == "" {
log.Warnf("Incorrect msg from redis, no mode field: %s", msg)
slog.Warn(fmt.Sprintf("Incorrect msg from redis, no mode field: %s", msg))
return
}
@@ -82,7 +82,7 @@ func MsgParser(ctx context.Context, msg string) {
// Если у нас циклическая пересылка сообщения, попробуем её тут разорвать, отбросив сообщение.
if j.Misc.Fwdcnt > Config.ForwardsMax {
log.Warnf("Discarding msg with fwd_cnt exceeding max value: %s", msg)
slog.Warn(fmt.Sprintf("Discarding msg with fwd_cnt exceeding max value: %s", msg))
return
}
@@ -222,16 +222,16 @@ func MsgParser(ctx context.Context, msg string) {
data, err := json.Marshal(message)
if err != nil {
log.Warnf("Unable to to serialize message for redis: %s", err)
slog.Warn(fmt.Sprintf("Unable to to serialize message for redis: %s", err))
return
}
// Заталкиваем наш json в редиску.
if err := RedisClient.Publish(ctx, sendTo, data).Err(); err != nil {
log.Warnf("Unable to send data to redis channel %s: %s", sendTo, err)
slog.Warn(fmt.Sprintf("Unable to send data to redis channel %s: %s", sendTo, err))
} else {
log.Debugf("Send msg to redis channel %s: %s", sendTo, string(data))
slog.Debug(fmt.Sprintf("Send msg to redis channel %s: %s", sendTo, string(data)))
}
}

View File

@@ -3,11 +3,11 @@ package misc
import (
"encoding/json"
"fmt"
"log/slog"
"os"
"path/filepath"
"github.com/hjson/hjson-go"
log "github.com/sirupsen/logrus"
)
// ReadConfig читает и валидирует конфиг, а также выставляет некоторые default-ы, если значений для параметров в конфиге
@@ -17,7 +17,7 @@ func ReadConfig() {
executablePath, err := os.Executable()
if err != nil {
log.Errorf("Unable to get current executable path: %s", err)
slog.Error(fmt.Sprintf("Unable to get current executable path: %s", err))
}
configJSONPath := fmt.Sprintf("%s/data/config.json", filepath.Dir(executablePath))
@@ -39,7 +39,7 @@ func ReadConfig() {
// Конфиг-файл длинноват для конфига, попробуем следующего кандидата
if fileInfo.Size() > 65535 {
log.Warnf("Config file %s is too long for config, skipping", location)
slog.Warn(fmt.Sprintf("Config file %s is too long for config, skipping", location))
continue
}
@@ -48,7 +48,7 @@ func ReadConfig() {
// Не удалось прочитать, попробуем следующего кандидата
if err != nil {
log.Warnf("Skip reading config file %s: %s", location, err)
slog.Warn(fmt.Sprintf("Skip reading config file %s: %s", location, err))
continue
}
@@ -65,7 +65,7 @@ func ReadConfig() {
// Не удалось распарсить - попробуем следующего кандидата
if err != nil {
log.Warnf("Skip parsing config file %s: %s", location, err)
slog.Warn(fmt.Sprintf("Skip parsing config file %s: %s", location, err))
continue
}
@@ -74,13 +74,13 @@ func ReadConfig() {
// Не удалось преобразовать map-ку в json
if err != nil {
log.Warnf("Skip parsing config file %s: %s", location, err)
slog.Warn(fmt.Sprintf("Skip parsing config file %s: %s", location, err))
continue
}
if err := json.Unmarshal(tmpjson, &sampleConfig); err != nil {
log.Warnf("Skip parsing config file %s: %s", location, err)
slog.Warn(fmt.Sprintf("Skip parsing config file %s: %s", location, err))
continue
}
@@ -105,7 +105,7 @@ func ReadConfig() {
// sampleConfig.Log = "" if not set
if sampleConfig.Channel == "" {
log.Errorf("Channel field in config file %s must be set", location)
slog.Error(fmt.Sprintf("Channel field in config file %s must be set", location))
}
// Частичная проверка, ровно то, куда мы _точно_ щепрввляем сообщения исходя из бизнес-логики приложения
@@ -130,7 +130,7 @@ func ReadConfig() {
}
if sampleConfig.Csign == "" {
log.Errorf("Csign field in config file %s must be set", location)
slog.Error(fmt.Sprintf("Csign field in config file %s must be set", location))
}
if sampleConfig.ForwardsMax == 0 {
@@ -140,13 +140,13 @@ func ReadConfig() {
Config = sampleConfig
configLoaded = true
log.Infof("Using %s as config file", location)
slog.Info(fmt.Sprintf("Using %s as config file", location))
break
}
if !configLoaded {
log.Error("Config was not loaded! Refusing to start.")
slog.Error("Config was not loaded! Refusing to start.")
os.Exit(1)
}
}

View File

@@ -1,27 +1,27 @@
package misc
import (
"fmt"
"log/slog"
"os"
"syscall"
log "github.com/sirupsen/logrus"
)
// SigHandler хэндлер сигналов закрывает все бд и сваливает из приложения.
func SigHandler() {
var err error
log.Infoln("Install signal handler")
slog.Info("Install signal handler")
for {
var s = <-SigChan
switch s {
case syscall.SIGINT:
log.Infoln("Got SIGINT, quitting")
slog.Info("Got SIGINT, quitting")
case syscall.SIGTERM:
log.Infoln("Got SIGTERM, quitting")
slog.Info("Got SIGTERM, quitting")
case syscall.SIGQUIT:
log.Infoln("Got SIGQUIT, quitting")
slog.Info("Got SIGQUIT, quitting")
// Заходим на новую итерацию, если у нас "неинтересный" сигнал.
default:
@@ -33,11 +33,11 @@ func SigHandler() {
// Отпишемся от всех каналов и закроем коннект к редиске.
if err = Subscriber.Unsubscribe(Ctx); err != nil {
log.Errorf("Unable to unsubscribe from redis channels cleanly: %s", err)
slog.Error(fmt.Sprintf("Unable to unsubscribe from redis channels cleanly: %s", err))
}
if err = Subscriber.Close(); err != nil {
log.Errorf("Unable to close redis connection cleanly: %s", err)
slog.Error(fmt.Sprintf("Unable to close redis connection cleanly: %s", err))
}
os.Exit(0)