Refactoring: implement proper JSON logging, better structure for app layers
This commit is contained in:
+13
-12
@@ -5,10 +5,11 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"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/go-telegram/bot"
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
const appID = "anon3anon"
|
const appID = "anon3anon"
|
||||||
@@ -18,18 +19,18 @@ func main() {
|
|||||||
|
|
||||||
conf, err := parseEnv()
|
conf, err := parseEnv()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal(err)
|
logger.FatalError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
opts := []bot.Option{
|
opts := []bot.Option{
|
||||||
bot.WithDebug(),
|
bot.WithMiddlewares(middleware.NewLoggingMiddleware(logger)),
|
||||||
bot.WithMessageTextHandler("/start", bot.MatchTypeExact, app.GetStartCommandHandler(logger)),
|
bot.WithMessageTextHandler("/start", bot.MatchTypeExact, handler.NewStartCommandHandler(logger)),
|
||||||
bot.WithDefaultHandler(app.GetAnonymousMessagesHandler(logger, conf.OwnerChatID)),
|
bot.WithDefaultHandler(handler.NewAnonymousMessagesHandler(logger, conf.OwnerChatID)),
|
||||||
}
|
}
|
||||||
|
|
||||||
b, err := bot.New(conf.TelegramBotToken, opts...)
|
b, err := bot.New(conf.TelegramBotToken, opts...)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
logger.Fatal(err)
|
logger.FatalError(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt)
|
||||||
@@ -38,10 +39,10 @@ func main() {
|
|||||||
b.Start(ctx)
|
b.Start(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
func initLogger() *logrus.Logger {
|
func initLogger() jsonlog.Logger {
|
||||||
logger := logrus.New()
|
logger := jsonlog.NewLogger(&jsonlog.Config{
|
||||||
logger.SetOutput(os.Stdout)
|
AppName: appID,
|
||||||
logger.SetFormatter(&logrus.JSONFormatter{})
|
Level: jsonlog.InfoLevel,
|
||||||
logger.SetLevel(logrus.WarnLevel)
|
})
|
||||||
return logger
|
return logger
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -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)
|
||||||
|
)
|
||||||
@@ -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...)
|
||||||
|
}
|
||||||
@@ -1,27 +1,15 @@
|
|||||||
package app
|
package handler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
|
||||||
|
"github.com/nightnoryu/anon3anon/pkg/infrastructure/jsonlog"
|
||||||
|
|
||||||
"github.com/go-telegram/bot"
|
"github.com/go-telegram/bot"
|
||||||
"github.com/go-telegram/bot/models"
|
"github.com/go-telegram/bot/models"
|
||||||
"github.com/sirupsen/logrus"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func GetStartCommandHandler(logger *logrus.Logger) bot.HandlerFunc {
|
func NewAnonymousMessagesHandler(logger jsonlog.Logger, ownerChatId int) 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 {
|
|
||||||
return func(ctx context.Context, b *bot.Bot, update *models.Update) {
|
return func(ctx context.Context, b *bot.Bot, update *models.Update) {
|
||||||
if update.Message == nil {
|
if update.Message == nil {
|
||||||
return
|
return
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user