diff --git a/pkg/infrastructure/jsonlog/logger.go b/pkg/infrastructure/jsonlog/logger.go index db7f9c7..d100964 100644 --- a/pkg/infrastructure/jsonlog/logger.go +++ b/pkg/infrastructure/jsonlog/logger.go @@ -14,6 +14,8 @@ var fieldMap = logrus.FieldMap{ } type Logger interface { + WithField(key string, value interface{}) Logger + Info(...interface{}) Error(error, ...interface{}) FatalError(error, ...interface{}) @@ -40,6 +42,10 @@ type logger struct { logrus.FieldLogger } +func (l *logger) WithField(key string, value interface{}) Logger { + return &logger{l.FieldLogger.WithField(key, value)} +} + func (l *logger) Error(err error, args ...interface{}) { l.FieldLogger.WithError(err).Error(args) } diff --git a/pkg/infrastructure/telegram/middleware/loggingmiddleware.go b/pkg/infrastructure/telegram/middleware/loggingmiddleware.go index 1cc0469..1eac382 100644 --- a/pkg/infrastructure/telegram/middleware/loggingmiddleware.go +++ b/pkg/infrastructure/telegram/middleware/loggingmiddleware.go @@ -10,16 +10,29 @@ import ( "github.com/go-telegram/bot/models" ) +const ( + chatIDField = "chat_id" + usernameField = "username" +) + 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)) + if update.Message == nil { + return } + + chatLogger := logger. + WithField(chatIDField, update.Message.Chat.ID). + WithField(usernameField, update.Message.From.Username) + + text := update.Message.Text + if len(update.Message.Caption) > 0 { + text = update.Message.Caption + } + + chatLogger.Info(fmt.Sprintf("new message: %s", text)) + next(ctx, bot, update) } }