Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactoring: migrate to zerolog #38

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
# Telegram BOT
SFB_TOKEN=
SFB_WEBHOOK_URL=

# Database
SFB_DATABASE=postgres://sfb:sfb@localhost/sfb?sslmode=disable
SFB_DATABASE_MAX_OPEN_CONNS=10
SFB_DATABASE_MAX_IDLE_CONNS=0

# Redis
SFB_REDIS=redis://localhost:6379
SFB_REDIS_MAX_OPEN_CONNS=5
SFB_REDIS_MAX_IDLE_CONNS=0

SFB_ADDR=:8000
SFB_SECRET_ID_SALT=-secret-1234-

# Logging
SFB_LOG_DEBUG=true
SFB_LOG_PRETTY=false
6 changes: 3 additions & 3 deletions bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import (

"github.com/bots-house/share-file-bot/bot/state"
"github.com/bots-house/share-file-bot/core"
"github.com/bots-house/share-file-bot/pkg/log"
"github.com/bots-house/share-file-bot/pkg/tg"
"github.com/bots-house/share-file-bot/service"
"github.com/fatih/structs"
"github.com/friendsofgo/errors"
"github.com/getsentry/sentry-go"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/rs/zerolog/log"
"github.com/tomasen/realip"
)

Expand Down Expand Up @@ -71,7 +71,7 @@ func (bot *Bot) SetWebhookIfNeed(ctx context.Context, u string) error {
return errors.Wrap(err, "invalid provided webhook url")
}

log.Info(ctx, "update bot webhook", "old", webhook.URL, "new", u.String())
log.Ctx(ctx).Info().Str("old", webhook.URL).Str("new", u.String()).Msg("update bot webhook")
if _, err := bot.client.SetWebhook(tgbotapi.WebhookConfig{
URL: u,
MaxConnections: 40,
Expand Down Expand Up @@ -315,7 +315,7 @@ func (bot *Bot) ServeHTTP(w http.ResponseWriter, r *http.Request) {
}

func (bot *Bot) onError(ctx context.Context, update *tgbotapi.Update, er error) {
log.Error(ctx, "handle update failed", "update_id", update.UpdateID, "err", er)
log.Ctx(ctx).Error().Int("update_id", update.UpdateID).Err(er).Msg("handle update failed")

sentry.AddBreadcrumb(&sentry.Breadcrumb{
Message: "Update",
Expand Down
6 changes: 3 additions & 3 deletions bot/handler_common.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ import (
"strings"

"github.com/bots-house/share-file-bot/core"
"github.com/bots-house/share-file-bot/pkg/log"
"github.com/friendsofgo/errors"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/rs/zerolog/log"
)

const (
Expand All @@ -26,7 +26,7 @@ func (bot *Bot) onStart(ctx context.Context, msg *tgbotapi.Message) error {
if args := msg.CommandArguments(); args != "" && !strings.HasPrefix(args, refDeepLinkPrefix) {
user := getUserCtx(ctx)

log.Debug(ctx, "query file", "public_id", args)
log.Ctx(ctx).Debug().Str("public_id", args).Msg("query file")
result, err := bot.fileSrv.GetFileByPublicID(ctx, user, args)
if errors.Cause(err) == core.ErrFileNotFound {
answer := bot.newAnswerMsg(msg, "😐Ничего не знаю о таком файле, проверь ссылку...")
Expand All @@ -43,7 +43,7 @@ func (bot *Bot) onStart(ctx context.Context, msg *tgbotapi.Message) error {
case result.ChatSubRequest != nil:
return bot.send(ctx, bot.renderSubRequest(msg, result.ChatSubRequest))
default:
log.Error(ctx, "bad result")
log.Ctx(ctx).Error().Msg("bad result")
}
}

Expand Down
4 changes: 2 additions & 2 deletions bot/handler_file.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ import (
"strings"

"github.com/bots-house/share-file-bot/core"
"github.com/bots-house/share-file-bot/pkg/log"
"github.com/bots-house/share-file-bot/service"
"github.com/friendsofgo/errors"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/lithammer/dedent"
"github.com/rs/zerolog/log"
)

const tgDomain = "t.me"
Expand Down Expand Up @@ -202,7 +202,7 @@ func (bot *Bot) deleteMessage(ctx context.Context, msg *tgbotapi.Message) error
msg.Chat.ID,
msg.MessageID,
)); err != nil {
log.Warn(ctx, "can't delete incoming message", "chat_id", msg.Chat.ID, "msg_id", msg.MessageID)
log.Ctx(ctx).Warn().Int64("chat_id", msg.Chat.ID).Int("msg_id", msg.MessageID).Msg("can't delete incoming message")
return err
}

Expand Down
4 changes: 2 additions & 2 deletions bot/handler_settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import (
"context"

"github.com/friendsofgo/errors"
"github.com/rs/zerolog/log"

"github.com/bots-house/share-file-bot/core"
"github.com/bots-house/share-file-bot/pkg/log"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/lithammer/dedent"
)
Expand Down Expand Up @@ -104,7 +104,7 @@ func (bot *Bot) onSettingsToggleLongIDsCBQ(ctx context.Context, cbq *tgbotapi.Ca

go func() {
if err := bot.answerCallbackQuery(ctx, cbq, answer); err != nil {
log.Warn(ctx, "cant answer inline query in onSettingsToggleLongIDsCBQ", "err", err)
log.Ctx(ctx).Warn().Err(err).Msg("cant answer inline query in onSettingsToggleLongIDsCBQ")
}
}()

Expand Down
10 changes: 6 additions & 4 deletions bot/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import (
"github.com/friendsofgo/errors"
"github.com/getsentry/sentry-go"
tgbotapi "github.com/go-telegram-bot-api/telegram-bot-api"
"github.com/rs/zerolog/log"

"github.com/bots-house/share-file-bot/pkg/log"
"github.com/bots-house/share-file-bot/pkg/tg"
"github.com/bots-house/share-file-bot/service"
)
Expand Down Expand Up @@ -47,15 +47,17 @@ func newAuthMiddleware(srv *service.Auth) tg.Middleware {
case update.ChannelPost != nil && update.ChannelPost.NewChatTitle != "":
tgUser = nil
default:
log.Warn(ctx, "unsupported update", "id", update.UpdateID)
log.Ctx(ctx).Warn().Int("id", update.UpdateID).Msg("unsupported update")
return nil
}

if tgUser != nil {
if tgUser.UserName != "" {
ctx = log.With(ctx, "user", fmt.Sprintf("%s#%d", tgUser.UserName, tgUser.ID))
logger := log.Ctx(ctx).With().Str("user", fmt.Sprintf("%s#%d", tgUser.UserName, tgUser.ID)).Logger()
ctx = logger.WithContext(ctx)
} else {
ctx = log.With(ctx, "user", fmt.Sprintf("#%d", tgUser.ID))
logger := log.Ctx(ctx).With().Str("user", fmt.Sprintf("#%d", tgUser.ID)).Logger()
ctx = logger.WithContext(ctx)
}

user, err := srv.Auth(ctx, &service.UserInfo{
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ require (
github.com/fatih/structs v1.1.0
github.com/friendsofgo/errors v0.9.2
github.com/getsentry/sentry-go v0.7.0
github.com/go-kit/kit v0.10.0
github.com/go-redis/redis/v8 v8.3.3
github.com/go-telegram-bot-api/telegram-bot-api v4.6.4+incompatible
github.com/gofrs/uuid v3.3.0+incompatible // indirect
Expand All @@ -20,6 +19,7 @@ require (
github.com/mattn/go-sqlite3 v2.0.3+incompatible // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/remind101/migrate v0.0.0-20170729031349-52c1edff7319
github.com/rs/zerolog v1.20.0
github.com/speps/go-hashids v2.0.0+incompatible
github.com/stretchr/testify v1.6.1
github.com/subosito/gotenv v1.2.0
Expand Down
Loading