feat(logging); improve messages and errors (#336)

* feat(logger): add module context

* feat(logger): change errors package

* feat(logger): update tests
This commit is contained in:
Ludvig Lundgren 2022-07-05 13:31:44 +02:00 committed by GitHub
parent 95471a4cf7
commit 0e88117702
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
69 changed files with 1172 additions and 957 deletions

View file

@ -1,12 +1,10 @@
package download_client
import (
"fmt"
"time"
"github.com/pkg/errors"
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/autobrr/autobrr/pkg/lidarr"
"github.com/autobrr/autobrr/pkg/qbittorrent"
"github.com/autobrr/autobrr/pkg/radarr"
@ -48,6 +46,7 @@ func (s *service) testQbittorrentConnection(client domain.DownloadClient) error
Password: client.Password,
TLS: client.TLS,
TLSSkipVerify: client.TLSSkipVerify,
Log: s.subLogger,
}
// only set basic auth if enabled
@ -60,7 +59,7 @@ func (s *service) testQbittorrentConnection(client domain.DownloadClient) error
qbt := qbittorrent.NewClient(qbtSettings)
err := qbt.Login()
if err != nil {
return errors.Wrap(err, fmt.Sprintf("error logging into client: %v", client.Host))
return errors.Wrap(err, "error logging into client: %v", client.Host)
}
s.log.Debug().Msgf("test client connection for qBittorrent: success")
@ -94,7 +93,7 @@ func (s *service) testDelugeConnection(client domain.DownloadClient) error {
// perform connection to Deluge server
err := deluge.Connect()
if err != nil {
return errors.Wrap(err, fmt.Sprintf("error logging into client: %v", client.Host))
return errors.Wrap(err, "error logging into client: %v", client.Host)
}
defer deluge.Close()
@ -102,7 +101,7 @@ func (s *service) testDelugeConnection(client domain.DownloadClient) error {
// print daemon version
ver, err := deluge.DaemonVersion()
if err != nil {
return errors.Wrap(err, fmt.Sprintf("could not get daemon version: %v", client.Host))
return errors.Wrap(err, "could not get daemon version: %v", client.Host)
}
s.log.Debug().Msgf("test client connection for Deluge: success - daemon version: %v", ver)
@ -117,11 +116,12 @@ func (s *service) testRadarrConnection(client domain.DownloadClient) error {
BasicAuth: client.Settings.Basic.Auth,
Username: client.Settings.Basic.Username,
Password: client.Settings.Basic.Password,
Log: s.subLogger,
})
_, err := r.Test()
if err != nil {
return errors.Wrap(err, fmt.Sprintf("radarr: connection test failed: %v", client.Host))
return errors.Wrap(err, "radarr: connection test failed: %v", client.Host)
}
s.log.Debug().Msgf("test client connection for Radarr: success")
@ -136,11 +136,12 @@ func (s *service) testSonarrConnection(client domain.DownloadClient) error {
BasicAuth: client.Settings.Basic.Auth,
Username: client.Settings.Basic.Username,
Password: client.Settings.Basic.Password,
Log: s.subLogger,
})
_, err := r.Test()
if err != nil {
return errors.Wrap(err, fmt.Sprintf("sonarr: connection test failed: %v", client.Host))
return errors.Wrap(err, "sonarr: connection test failed: %v", client.Host)
}
s.log.Debug().Msgf("test client connection for Sonarr: success")
@ -155,11 +156,12 @@ func (s *service) testLidarrConnection(client domain.DownloadClient) error {
BasicAuth: client.Settings.Basic.Auth,
Username: client.Settings.Basic.Username,
Password: client.Settings.Basic.Password,
Log: s.subLogger,
})
_, err := r.Test()
if err != nil {
return errors.Wrap(err, fmt.Sprintf("lidarr: connection test failed: %v", client.Host))
return errors.Wrap(err, "lidarr: connection test failed: %v", client.Host)
}
s.log.Debug().Msgf("test client connection for Lidarr: success")
@ -174,11 +176,12 @@ func (s *service) testWhisparrConnection(client domain.DownloadClient) error {
BasicAuth: client.Settings.Basic.Auth,
Username: client.Settings.Basic.Username,
Password: client.Settings.Basic.Password,
Log: s.subLogger,
})
_, err := r.Test()
if err != nil {
return errors.Wrap(err, fmt.Sprintf("whisparr: connection test failed: %v", client.Host))
return errors.Wrap(err, "whisparr: connection test failed: %v", client.Host)
}
s.log.Debug().Msgf("test client connection for whisparr: success")

View file

@ -3,9 +3,13 @@ package download_client
import (
"context"
"errors"
"log"
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/internal/logger"
"github.com/dcarbone/zadapters/zstdlog"
"github.com/rs/zerolog"
)
type Service interface {
@ -18,23 +22,40 @@ type Service interface {
}
type service struct {
log logger.Logger
repo domain.DownloadClientRepo
log zerolog.Logger
repo domain.DownloadClientRepo
subLogger *log.Logger
}
func NewService(log logger.Logger, repo domain.DownloadClientRepo) Service {
return &service{
log: log,
s := &service{
log: log.With().Str("module", "download_client").Logger(),
repo: repo,
}
s.subLogger = zstdlog.NewStdLoggerWithLevel(s.log.With().Logger(), zerolog.TraceLevel)
return s
}
func (s *service) List(ctx context.Context) ([]domain.DownloadClient, error) {
return s.repo.List(ctx)
clients, err := s.repo.List(ctx)
if err != nil {
s.log.Error().Err(err).Msg("could not list download clients")
return nil, err
}
return clients, nil
}
func (s *service) FindByID(ctx context.Context, id int32) (*domain.DownloadClient, error) {
return s.repo.FindByID(ctx, id)
client, err := s.repo.FindByID(ctx, id)
if err != nil {
s.log.Error().Err(err).Msgf("could not find download client by id: %v", id)
return nil, err
}
return client, nil
}
func (s *service) Store(ctx context.Context, client domain.DownloadClient) (*domain.DownloadClient, error) {
@ -46,7 +67,13 @@ func (s *service) Store(ctx context.Context, client domain.DownloadClient) (*dom
}
// store
return s.repo.Store(ctx, client)
c, err := s.repo.Store(ctx, client)
if err != nil {
s.log.Error().Err(err).Msgf("could not store download client: %+v", client)
return nil, err
}
return c, err
}
func (s *service) Update(ctx context.Context, client domain.DownloadClient) (*domain.DownloadClient, error) {
@ -57,12 +84,23 @@ func (s *service) Update(ctx context.Context, client domain.DownloadClient) (*do
return nil, errors.New("validation error: no type")
}
// store
return s.repo.Update(ctx, client)
// update
c, err := s.repo.Update(ctx, client)
if err != nil {
s.log.Error().Err(err).Msgf("could not update download client: %+v", client)
return nil, err
}
return c, err
}
func (s *service) Delete(ctx context.Context, clientID int) error {
return s.repo.Delete(ctx, clientID)
err := s.repo.Delete(ctx, clientID)
if err != nil {
s.log.Error().Err(err).Msgf("could not delete download client: %v", clientID)
return err
}
return nil
}
func (s *service) Test(client domain.DownloadClient) error {