Skip to content

Commit

Permalink
refactor: replace zap logger with slog
Browse files Browse the repository at this point in the history
resolves #18
  • Loading branch information
cecobask committed Nov 22, 2023
1 parent 478e9ed commit f1d9203
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 177 deletions.
2 changes: 0 additions & 2 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@ go 1.21
require (
github.com/PuerkitoBio/goquery v1.8.1
github.com/joho/godotenv v1.5.1
go.uber.org/zap v1.26.0
)

require (
github.com/andybalholm/cascadia v1.3.1 // indirect
go.uber.org/multierr v1.10.0 // indirect
golang.org/x/net v0.17.0 // indirect
)
14 changes: 0 additions & 14 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,9 @@ github.com/PuerkitoBio/goquery v1.8.1 h1:uQxhNlArOIdbrH1tr0UXwdVFgDcZDrZVdcpygAc
github.com/PuerkitoBio/goquery v1.8.1/go.mod h1:Q8ICL1kNUJ2sXGoAhPGUdYDJvgQgHzJsnnd3H7Ho5jQ=
github.com/andybalholm/cascadia v1.3.1 h1:nhxRkql1kdYCc8Snf7D5/D3spOX+dBgjA6u8x004T2c=
github.com/andybalholm/cascadia v1.3.1/go.mod h1:R4bJ1UQfqADjvDa4P6HZHLh/3OxWWEqc0Sk8XGwHqvA=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/joho/godotenv v1.5.1 h1:7eLL/+HRGLY0ldzfGMeQkb7vMd0as4CfYvUVzLqw0N0=
github.com/joho/godotenv v1.5.1/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwAbqwq4=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.1 h1:w7B6lhMri9wdJUVmEZPGGhZzrYTPvgJArz7wNPgYKsk=
github.com/stretchr/testify v1.8.1/go.mod h1:w2LPCIKwWwSfY2zedu0+kehJoqGctiVI29o6fzry7u4=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
go.uber.org/goleak v1.2.0 h1:xqgm/S+aQvhWFTtR0XK3Jvg7z8kGV8P4X14IzwN3Eqk=
go.uber.org/goleak v1.2.0/go.mod h1:XJYK+MuIchqpmGmUSAzotztawfKvYLUIgg7guXrwVUo=
go.uber.org/multierr v1.10.0 h1:S0h4aNzvfcFsC3dRF1jLoaov7oRaKqRGC/pUEJ2yvPQ=
go.uber.org/multierr v1.10.0/go.mod h1:20+QtiLqy0Nd6FdQB9TLXag12DsQkrbs3htMFfDN80Y=
go.uber.org/zap v1.26.0 h1:sI7k6L95XOKS281NhVKOFCUNIvv9e0w4BF8N3u+tCRo=
go.uber.org/zap v1.26.0/go.mod h1:dtElttAiwGvoJ/vj4IwHBS/gXsEu/pZ50mUIRWuG0so=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=
golang.org/x/mod v0.6.0-dev.0.20220419223038-86c51ed26bb4/go.mod h1:jJ57K6gSWd91VN4djpZkiMVwK6gcyfeH4XE8wZrZaV4=
Expand Down Expand Up @@ -48,5 +36,3 @@ golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGm
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.12/go.mod h1:hNGJHUnrk76NpqgfD5Aqm5Crs+Hm0VOH/i9J2+nxYbc=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
9 changes: 5 additions & 4 deletions pkg/client/imdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/csv"
"errors"
"fmt"
"log/slog"
"mime"
"net/http"
"net/http/cookiejar"
Expand All @@ -16,7 +17,7 @@ import (

"github.com/PuerkitoBio/goquery"
"github.com/cecobask/imdb-trakt-sync/pkg/entities"
"go.uber.org/zap"
"github.com/cecobask/imdb-trakt-sync/pkg/logger"
)

const (
Expand All @@ -36,7 +37,7 @@ const (
type ImdbClient struct {
client *http.Client
config ImdbConfig
logger *zap.Logger
logger *slog.Logger
}

type ImdbConfig struct {
Expand All @@ -46,7 +47,7 @@ type ImdbConfig struct {
WatchlistId string
}

func NewImdbClient(config ImdbConfig, logger *zap.Logger) (ImdbClientInterface, error) {
func NewImdbClient(config ImdbConfig, logger *slog.Logger) (ImdbClientInterface, error) {
jar, err := setupCookieJar(config)
if err != nil {
return nil, err
Expand Down Expand Up @@ -203,7 +204,7 @@ func (c *ImdbClient) ListsGet(listIds []string) ([]entities.ImdbList, error) {
if err != nil {
var apiError *ApiError
if errors.As(err, &apiError) && apiError.StatusCode == http.StatusNotFound {
c.logger.Debug("silencing not found error while fetching imdb lists", zap.Error(apiError))
c.logger.Debug("silencing not found error while fetching imdb lists", logger.Error(apiError))
return
}
errChan <- fmt.Errorf("unexpected error while fetching imdb lists: %w", err)
Expand Down
46 changes: 27 additions & 19 deletions pkg/client/trakt.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"errors"
"fmt"
"io"
"log/slog"
"net/http"
"net/http/cookiejar"
"net/url"
Expand All @@ -15,7 +16,6 @@ import (
"time"

"github.com/cecobask/imdb-trakt-sync/pkg/entities"
"go.uber.org/zap"
)

const (
Expand Down Expand Up @@ -61,7 +61,7 @@ const (
type TraktClient struct {
client *http.Client
config TraktConfig
logger *zap.Logger
logger *slog.Logger
}

type TraktConfig struct {
Expand All @@ -74,7 +74,7 @@ type TraktConfig struct {
SyncMode string
}

func NewTraktClient(config TraktConfig, logger *zap.Logger) (TraktClientInterface, error) {
func NewTraktClient(config TraktConfig, logger *slog.Logger) (TraktClientInterface, error) {
jar, err := cookiejar.New(nil)
if err != nil {
return nil, fmt.Errorf("failure creating cookie jar: %w", err)
Expand Down Expand Up @@ -355,7 +355,8 @@ func (tc *TraktClient) WatchlistGet() (*entities.TraktList, error) {

func (tc *TraktClient) WatchlistItemsAdd(items entities.TraktItems) error {
if tc.config.SyncMode == traktSyncModeDryRun {
tc.logger.Info(fmt.Sprintf("sync mode dry run would have added %d trakt list item(s)", len(items)), zap.Array("watchlist", items))
msg := fmt.Sprintf("sync mode dry run would have added %d trakt list item(s)", len(items))
tc.logger.Info(msg, slog.Any("watchlist", items))
return nil
}
body, err := json.Marshal(mapTraktItemsToTraktBody(items))
Expand All @@ -376,13 +377,14 @@ func (tc *TraktClient) WatchlistItemsAdd(items entities.TraktItems) error {
if err != nil {
return err
}
tc.logger.Info("synced trakt watchlist", zap.Object("watchlist", traktResponse))
tc.logger.Info("synced trakt watchlist", slog.Any("watchlist", traktResponse))
return nil
}

func (tc *TraktClient) WatchlistItemsRemove(items entities.TraktItems) error {
if tc.config.SyncMode == traktSyncModeDryRun || tc.config.SyncMode == traktSyncModeAddOnly {
tc.logger.Info(fmt.Sprintf("sync mode %s would have deleted %d trakt list item(s)", tc.config.SyncMode, len(items)), zap.Array("watchlist", items))
msg := fmt.Sprintf("sync mode %s would have deleted %d trakt list item(s)", tc.config.SyncMode, len(items))
tc.logger.Info(msg, slog.Any("watchlist", items))
return nil
}
body, err := json.Marshal(mapTraktItemsToTraktBody(items))
Expand All @@ -403,7 +405,7 @@ func (tc *TraktClient) WatchlistItemsRemove(items entities.TraktItems) error {
if err != nil {
return err
}
tc.logger.Info("synced trakt watchlist", zap.Object("watchlist", traktResponse))
tc.logger.Info("synced trakt watchlist", slog.Any("watchlist", traktResponse))
return nil
}

Expand Down Expand Up @@ -436,7 +438,8 @@ func (tc *TraktClient) ListGet(listId string) (*entities.TraktList, error) {

func (tc *TraktClient) ListItemsAdd(listId string, items entities.TraktItems) error {
if tc.config.SyncMode == traktSyncModeDryRun {
tc.logger.Info(fmt.Sprintf("sync mode dry run would have added %d trakt list item(s)", len(items)), zap.Array(listId, items))
msg := fmt.Sprintf("sync mode dry run would have added %d trakt list item(s)", len(items))
tc.logger.Info(msg, slog.Any(listId, items))
return nil
}
body, err := json.Marshal(mapTraktItemsToTraktBody(items))
Expand All @@ -457,13 +460,14 @@ func (tc *TraktClient) ListItemsAdd(listId string, items entities.TraktItems) er
if err != nil {
return err
}
tc.logger.Info("synced trakt list", zap.Object(listId, traktResponse))
tc.logger.Info("synced trakt list", slog.Any(listId, traktResponse))
return nil
}

func (tc *TraktClient) ListItemsRemove(listId string, items entities.TraktItems) error {
if tc.config.SyncMode == traktSyncModeDryRun || tc.config.SyncMode == traktSyncModeAddOnly {
tc.logger.Info(fmt.Sprintf("sync mode %s would have deleted %d trakt list item(s)", tc.config.SyncMode, len(items)), zap.Array(listId, items))
msg := fmt.Sprintf("sync mode %s would have deleted %d trakt list item(s)", tc.config.SyncMode, len(items))
tc.logger.Info(msg, slog.Any(listId, items))
return nil
}
body, err := json.Marshal(mapTraktItemsToTraktBody(items))
Expand All @@ -484,7 +488,7 @@ func (tc *TraktClient) ListItemsRemove(listId string, items entities.TraktItems)
if err != nil {
return err
}
tc.logger.Info("synced trakt list", zap.Object(listId, traktResponse))
tc.logger.Info("synced trakt list", slog.Any(listId, traktResponse))
return nil
}

Expand Down Expand Up @@ -603,7 +607,8 @@ func (tc *TraktClient) RatingsGet() (entities.TraktItems, error) {

func (tc *TraktClient) RatingsAdd(items entities.TraktItems) error {
if tc.config.SyncMode == traktSyncModeDryRun {
tc.logger.Info(fmt.Sprintf("sync mode dry run would have added %d trakt rating item(s)", len(items)), zap.Array("ratings", items))
msg := fmt.Sprintf("sync mode dry run would have added %d trakt rating item(s)", len(items))
tc.logger.Info(msg, slog.Any("ratings", items))
return nil
}
body, err := json.Marshal(mapTraktItemsToTraktBody(items))
Expand All @@ -624,13 +629,14 @@ func (tc *TraktClient) RatingsAdd(items entities.TraktItems) error {
if err != nil {
return err
}
tc.logger.Info("synced trakt ratings", zap.Object("ratings", traktResponse))
tc.logger.Info("synced trakt ratings", slog.Any("ratings", traktResponse))
return nil
}

func (tc *TraktClient) RatingsRemove(items entities.TraktItems) error {
if tc.config.SyncMode == traktSyncModeDryRun || tc.config.SyncMode == traktSyncModeAddOnly {
tc.logger.Info(fmt.Sprintf("sync mode %s would have deleted %d trakt rating item(s)", tc.config.SyncMode, len(items)), zap.Array("ratings", items))
msg := fmt.Sprintf("sync mode %s would have deleted %d trakt rating item(s)", tc.config.SyncMode, len(items))
tc.logger.Info(msg, slog.Any("ratings", items))
return nil
}
body, err := json.Marshal(mapTraktItemsToTraktBody(items))
Expand All @@ -651,7 +657,7 @@ func (tc *TraktClient) RatingsRemove(items entities.TraktItems) error {
if err != nil {
return err
}
tc.logger.Info("synced trakt ratings", zap.Object("ratings", traktResponse))
tc.logger.Info("synced trakt ratings", slog.Any("ratings", traktResponse))
return nil
}

Expand All @@ -671,7 +677,8 @@ func (tc *TraktClient) HistoryGet(itemType, itemId string) (entities.TraktItems,

func (tc *TraktClient) HistoryAdd(items entities.TraktItems) error {
if tc.config.SyncMode == traktSyncModeDryRun {
tc.logger.Info(fmt.Sprintf("sync mode dry run would have added %d trakt history item(s)", len(items)), zap.Array("history", items))
msg := fmt.Sprintf("sync mode dry run would have added %d trakt history item(s)", len(items))
tc.logger.Info(msg, slog.Any("history", items))
return nil
}
body, err := json.Marshal(mapTraktItemsToTraktBody(items))
Expand All @@ -692,13 +699,14 @@ func (tc *TraktClient) HistoryAdd(items entities.TraktItems) error {
if err != nil {
return err
}
tc.logger.Info("synced trakt history", zap.Object("history", traktResponse))
tc.logger.Info("synced trakt history", slog.Any("history", traktResponse))
return nil
}

func (tc *TraktClient) HistoryRemove(items entities.TraktItems) error {
if tc.config.SyncMode == traktSyncModeDryRun || tc.config.SyncMode == traktSyncModeAddOnly {
tc.logger.Info(fmt.Sprintf("sync mode %s would have deleted %d trakt history item(s)", tc.config.SyncMode, len(items)), zap.Array("history", items))
msg := fmt.Sprintf("sync mode %s would have deleted %d trakt history item(s)", tc.config.SyncMode, len(items))
tc.logger.Info(msg, slog.Any("history", items))
return nil
}
body, err := json.Marshal(mapTraktItemsToTraktBody(items))
Expand All @@ -719,7 +727,7 @@ func (tc *TraktClient) HistoryRemove(items entities.TraktItems) error {
if err != nil {
return err
}
tc.logger.Info("synced trakt history", zap.Object("history", traktResponse))
tc.logger.Info("synced trakt history", slog.Any("history", traktResponse))
return nil
}

Expand Down
Loading

0 comments on commit f1d9203

Please sign in to comment.