diff --git a/cmd/anon3anon/main.go b/cmd/anon3anon/main.go index 6448dfa..4a3dacb 100644 --- a/cmd/anon3anon/main.go +++ b/cmd/anon3anon/main.go @@ -5,10 +5,11 @@ import ( "os" "os/signal" - "github.com/nightnoryu/anon3anon/pkg/app" + "github.com/nightnoryu/anon3anon/pkg/infrastructure/jsonlog" + "github.com/nightnoryu/anon3anon/pkg/infrastructure/telegram/handler" + "github.com/nightnoryu/anon3anon/pkg/infrastructure/telegram/middleware" "github.com/go-telegram/bot" - "github.com/sirupsen/logrus" ) const appID = "anon3anon" @@ -18,18 +19,18 @@ func main() { conf, err := parseEnv() if err != nil { - logger.Fatal(err) + logger.FatalError(err) } opts := []bot.Option{ - bot.WithDebug(), - bot.WithMessageTextHandler("/start", bot.MatchTypeExact, app.GetStartCommandHandler(logger)), - bot.WithDefaultHandler(app.GetAnonymousMessagesHandler(logger, conf.OwnerChatID)), + bot.WithMiddlewares(middleware.NewLoggingMiddleware(logger)), + bot.WithMessageTextHandler("/start", bot.MatchTypeExact, handler.NewStartCommandHandler(logger)), + bot.WithDefaultHandler(handler.NewAnonymousMessagesHandler(logger, conf.OwnerChatID)), } b, err := bot.New(conf.TelegramBotToken, opts...) if err != nil { - logger.Fatal(err) + logger.FatalError(err) } ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt) @@ -38,10 +39,10 @@ func main() { b.Start(ctx) } -func initLogger() *logrus.Logger { - logger := logrus.New() - logger.SetOutput(os.Stdout) - logger.SetFormatter(&logrus.JSONFormatter{}) - logger.SetLevel(logrus.WarnLevel) +func initLogger() jsonlog.Logger { + logger := jsonlog.NewLogger(&jsonlog.Config{ + AppName: appID, + Level: jsonlog.InfoLevel, + }) return logger } diff --git a/pkg/infrastructure/jsonlog/level.go b/pkg/infrastructure/jsonlog/level.go new file mode 100644 index 0000000..2be78a0 --- /dev/null +++ b/pkg/infrastructure/jsonlog/level.go @@ -0,0 +1,13 @@ +package jsonlog + +import "github.com/sirupsen/logrus" + +type Level logrus.Level + +const ( + FatalLevel = Level(logrus.FatalLevel) + ErrorLevel = Level(logrus.ErrorLevel) + WarnLevel = Level(logrus.WarnLevel) + InfoLevel = Level(logrus.InfoLevel) + DebugLevel = Level(logrus.DebugLevel) +) diff --git a/pkg/infrastructure/jsonlog/logger.go b/pkg/infrastructure/jsonlog/logger.go new file mode 100644 index 0000000..db7f9c7 --- /dev/null +++ b/pkg/infrastructure/jsonlog/logger.go @@ -0,0 +1,49 @@ +package jsonlog + +import ( + "time" + + "github.com/sirupsen/logrus" +) + +const appNameKey = "app_name" + +var fieldMap = logrus.FieldMap{ + logrus.FieldKeyTime: "@timestamp", + logrus.FieldKeyMsg: "message", +} + +type Logger interface { + Info(...interface{}) + Error(error, ...interface{}) + FatalError(error, ...interface{}) +} + +type Config struct { + AppName string + Level Level +} + +func NewLogger(config *Config) Logger { + impl := logrus.New() + impl.SetFormatter(&logrus.JSONFormatter{ + TimestampFormat: time.RFC3339Nano, + FieldMap: fieldMap, + }) + impl.SetLevel(logrus.Level(config.Level)) + return &logger{ + FieldLogger: impl.WithField(appNameKey, config.AppName), + } +} + +type logger struct { + logrus.FieldLogger +} + +func (l *logger) Error(err error, args ...interface{}) { + l.FieldLogger.WithError(err).Error(args) +} + +func (l *logger) FatalError(err error, args ...interface{}) { + l.FieldLogger.WithError(err).Fatal(args...) +} diff --git a/pkg/app/handlers.go b/pkg/infrastructure/telegram/handler/anonymousmessages.go similarity index 60% rename from pkg/app/handlers.go rename to pkg/infrastructure/telegram/handler/anonymousmessages.go index b971c0a..907bfbc 100644 --- a/pkg/app/handlers.go +++ b/pkg/infrastructure/telegram/handler/anonymousmessages.go @@ -1,27 +1,15 @@ -package app +package handler import ( "context" + "github.com/nightnoryu/anon3anon/pkg/infrastructure/jsonlog" + "github.com/go-telegram/bot" "github.com/go-telegram/bot/models" - "github.com/sirupsen/logrus" ) -func GetStartCommandHandler(logger *logrus.Logger) bot.HandlerFunc { - return func(ctx context.Context, b *bot.Bot, update *models.Update) { - params := &bot.SendMessageParams{ - ChatID: update.Message.Chat.ID, - Text: "Жду твоих сообщений!!", - } - _, err := b.SendMessage(ctx, params) - if err != nil { - logger.Error(err) - } - } -} - -func GetAnonymousMessagesHandler(logger *logrus.Logger, ownerChatId int) bot.HandlerFunc { +func NewAnonymousMessagesHandler(logger jsonlog.Logger, ownerChatId int) bot.HandlerFunc { return func(ctx context.Context, b *bot.Bot, update *models.Update) { if update.Message == nil { return diff --git a/pkg/infrastructure/telegram/handler/startcommand.go b/pkg/infrastructure/telegram/handler/startcommand.go new file mode 100644 index 0000000..fa8e329 --- /dev/null +++ b/pkg/infrastructure/telegram/handler/startcommand.go @@ -0,0 +1,23 @@ +package handler + +import ( + "context" + + "github.com/nightnoryu/anon3anon/pkg/infrastructure/jsonlog" + + "github.com/go-telegram/bot" + "github.com/go-telegram/bot/models" +) + +func NewStartCommandHandler(logger jsonlog.Logger) bot.HandlerFunc { + return func(ctx context.Context, b *bot.Bot, update *models.Update) { + params := &bot.SendMessageParams{ + ChatID: update.Message.Chat.ID, + Text: "Жду твоих сообщений!!", + } + _, err := b.SendMessage(ctx, params) + if err != nil { + logger.Error(err) + } + } +} diff --git a/pkg/infrastructure/telegram/middleware/loggingmiddleware.go b/pkg/infrastructure/telegram/middleware/loggingmiddleware.go new file mode 100644 index 0000000..1cc0469 --- /dev/null +++ b/pkg/infrastructure/telegram/middleware/loggingmiddleware.go @@ -0,0 +1,26 @@ +package middleware + +import ( + "context" + "fmt" + + "github.com/nightnoryu/anon3anon/pkg/infrastructure/jsonlog" + + "github.com/go-telegram/bot" + "github.com/go-telegram/bot/models" +) + +func NewLoggingMiddleware(logger jsonlog.Logger) bot.Middleware { + return func(next bot.HandlerFunc) bot.HandlerFunc { + return func(ctx context.Context, bot *bot.Bot, update *models.Update) { + if update.Message != nil { + text := update.Message.Text + if len(update.Message.Caption) > 0 { + text = update.Message.Caption + } + logger.Info(fmt.Sprintf("message from %s: %s", update.Message.From.Username, text)) + } + next(ctx, bot, update) + } + } +}