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,13 @@
package indexer
import (
"fmt"
"github.com/rs/zerolog"
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/internal/logger"
"github.com/autobrr/autobrr/internal/mock"
"github.com/autobrr/autobrr/pkg/btn"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/autobrr/autobrr/pkg/ggn"
"github.com/autobrr/autobrr/pkg/ptp"
"github.com/autobrr/autobrr/pkg/red"
@ -25,13 +26,13 @@ type apiClient interface {
}
type apiService struct {
log logger.Logger
log zerolog.Logger
apiClients map[string]apiClient
}
func NewAPIService(log logger.Logger) APIService {
return &apiService{
log: log,
log: log.With().Str("module", "indexer-api").Logger(),
apiClients: make(map[string]apiClient),
}
}
@ -42,7 +43,7 @@ func (s *apiService) GetTorrentByID(indexer string, torrentID string) (*domain.T
return nil, nil
}
s.log.Trace().Str("service", "api").Str("method", "GetTorrentByID").Msgf("'%v' trying to fetch torrent from api", indexer)
s.log.Trace().Str("method", "GetTorrentByID").Msgf("'%v' trying to fetch torrent from api", indexer)
t, err := v.GetTorrentByID(torrentID)
if err != nil {
@ -50,7 +51,7 @@ func (s *apiService) GetTorrentByID(indexer string, torrentID string) (*domain.T
return nil, err
}
s.log.Trace().Str("service", "api").Str("method", "GetTorrentByID").Msgf("'%v' successfully fetched torrent from api: %+v", indexer, t)
s.log.Trace().Str("method", "GetTorrentByID").Msgf("'%v' successfully fetched torrent from api: %+v", indexer, t)
return t, nil
}
@ -63,6 +64,7 @@ func (s *apiService) TestConnection(indexer string) (bool, error) {
t, err := v.TestAPI()
if err != nil {
s.log.Error().Err(err).Msgf("error testing connection for api: %v", indexer)
return false, err
}
@ -72,9 +74,9 @@ func (s *apiService) TestConnection(indexer string) (bool, error) {
func (s *apiService) AddClient(indexer string, settings map[string]string) error {
// basic validation
if indexer == "" {
return fmt.Errorf("api.Service.AddClient: validation falied: indexer can't be empty")
return errors.New("api.Service.AddClient: validation falied: indexer can't be empty")
} else if len(settings) == 0 {
return fmt.Errorf("api.Service.AddClient: validation falied: settings can't be empty")
return errors.New("api.Service.AddClient: validation falied: settings can't be empty")
}
s.log.Trace().Msgf("api.Service.AddClient: init api client for '%v'", indexer)
@ -84,33 +86,33 @@ func (s *apiService) AddClient(indexer string, settings map[string]string) error
case "btn":
key, ok := settings["api_key"]
if !ok || key == "" {
return fmt.Errorf("api.Service.AddClient: could not initialize btn client: missing var 'api_key'")
return errors.New("api.Service.AddClient: could not initialize btn client: missing var 'api_key'")
}
s.apiClients[indexer] = btn.NewClient("", key)
case "ptp":
user, ok := settings["api_user"]
if !ok || user == "" {
return fmt.Errorf("api.Service.AddClient: could not initialize ptp client: missing var 'api_user'")
return errors.New("api.Service.AddClient: could not initialize ptp client: missing var 'api_user'")
}
key, ok := settings["api_key"]
if !ok || key == "" {
return fmt.Errorf("api.Service.AddClient: could not initialize ptp client: missing var 'api_key'")
return errors.New("api.Service.AddClient: could not initialize ptp client: missing var 'api_key'")
}
s.apiClients[indexer] = ptp.NewClient("", user, key)
case "ggn":
key, ok := settings["api_key"]
if !ok || key == "" {
return fmt.Errorf("api.Service.AddClient: could not initialize ggn client: missing var 'api_key'")
return errors.New("api.Service.AddClient: could not initialize ggn client: missing var 'api_key'")
}
s.apiClients[indexer] = ggn.NewClient("", key)
case "redacted":
key, ok := settings["api_key"]
if !ok || key == "" {
return fmt.Errorf("api.Service.AddClient: could not initialize red client: missing var 'api_key'")
return errors.New("api.Service.AddClient: could not initialize red client: missing var 'api_key'")
}
s.apiClients[indexer] = red.NewClient("", key)
@ -118,7 +120,7 @@ func (s *apiService) AddClient(indexer string, settings map[string]string) error
s.apiClients[indexer] = mock.NewMockClient("", "mock")
default:
return fmt.Errorf("api.Service.AddClient: could not initialize client: unsupported indexer '%v'", indexer)
return errors.New("api.Service.AddClient: could not initialize client: unsupported indexer '%v'", indexer)
}
return nil

View file

@ -2,7 +2,6 @@ package indexer
import (
"context"
"errors"
"fmt"
"io/fs"
"os"
@ -13,6 +12,7 @@ import (
"github.com/autobrr/autobrr/internal/domain"
"github.com/autobrr/autobrr/internal/logger"
"github.com/autobrr/autobrr/internal/scheduler"
"github.com/autobrr/autobrr/pkg/errors"
"github.com/gosimple/slug"
"github.com/rs/zerolog"
@ -52,7 +52,7 @@ type service struct {
func NewService(log logger.Logger, config *domain.Config, repo domain.IndexerRepo, apiService APIService, scheduler scheduler.Service) Service {
return &service{
log: log.With().Str("service", "indexer").Logger(),
log: log.With().Str("module", "indexer").Logger(),
config: config,
repo: repo,
apiService: apiService,
@ -94,6 +94,7 @@ func (s *service) Store(ctx context.Context, indexer domain.Indexer) (*domain.In
func (s *service) Update(ctx context.Context, indexer domain.Indexer) (*domain.Indexer, error) {
i, err := s.repo.Update(ctx, indexer)
if err != nil {
s.log.Error().Err(err).Msgf("could not update indexer: %+v", indexer)
return nil, err
}
@ -115,6 +116,7 @@ func (s *service) Update(ctx context.Context, indexer domain.Indexer) (*domain.I
func (s *service) Delete(ctx context.Context, id int) error {
if err := s.repo.Delete(ctx, id); err != nil {
s.log.Error().Err(err).Msgf("could not delete indexer by id: %v", id)
return err
}
@ -125,11 +127,23 @@ func (s *service) Delete(ctx context.Context, id int) error {
}
func (s *service) FindByFilterID(ctx context.Context, id int) ([]domain.Indexer, error) {
return s.repo.FindByFilterID(ctx, id)
indexers, err := s.repo.FindByFilterID(ctx, id)
if err != nil {
s.log.Error().Err(err).Msgf("could not find indexers by filter id: %v", id)
return nil, err
}
return indexers, err
}
func (s *service) List(ctx context.Context) ([]domain.Indexer, error) {
return s.repo.List(ctx)
indexers, err := s.repo.List(ctx)
if err != nil {
s.log.Error().Err(err).Msg("could not get indexer list")
return nil, err
}
return indexers, err
}
func (s *service) GetAll() ([]*domain.IndexerDefinition, error) {
@ -154,6 +168,7 @@ func (s *service) GetAll() ([]*domain.IndexerDefinition, error) {
func (s *service) mapIndexers() (map[string]*domain.IndexerDefinition, error) {
indexers, err := s.repo.List(context.Background())
if err != nil {
s.log.Error().Err(err).Msg("could not read indexer list")
return nil, err
}
@ -264,6 +279,7 @@ func (s *service) Start() error {
// load all indexer definitions
err := s.LoadIndexerDefinitions()
if err != nil {
s.log.Error().Err(err).Msg("could not load indexer definitions")
return err
}
@ -271,7 +287,7 @@ func (s *service) Start() error {
// load custom indexer definitions
err = s.LoadCustomIndexerDefinitions()
if err != nil {
return fmt.Errorf("could not load custom indexer definitions: %w", err)
return errors.Wrap(err, "could not load custom indexer definitions")
}
}
@ -394,12 +410,12 @@ func (s *service) mapIRCServerDefinitionLookup(ircServer string, indexerDefiniti
func (s *service) LoadIndexerDefinitions() error {
entries, err := fs.ReadDir(Definitions, "definitions")
if err != nil {
s.log.Fatal().Stack().Msgf("failed reading directory: %s", err)
s.log.Fatal().Err(err).Stack().Msg("failed reading directory")
}
if len(entries) == 0 {
s.log.Fatal().Stack().Msgf("failed reading directory: %s", err)
return err
s.log.Fatal().Err(err).Stack().Msg("failed reading directory")
return errors.Wrap(err, "could not read directory")
}
for _, f := range entries {
@ -417,13 +433,13 @@ func (s *service) LoadIndexerDefinitions() error {
data, err := fs.ReadFile(Definitions, file)
if err != nil {
s.log.Error().Stack().Err(err).Msgf("failed reading file: %v", file)
return err
return errors.Wrap(err, "could not read file: %v", file)
}
err = yaml.Unmarshal(data, &d)
if err != nil {
s.log.Error().Stack().Err(err).Msgf("failed unmarshal file: %v", file)
return err
return errors.Wrap(err, "could not unmarshal file: %v", file)
}
if d.Implementation == "" {
@ -454,7 +470,8 @@ func (s *service) LoadCustomIndexerDefinitions() error {
entries, err := outputDirRead.ReadDir(0)
if err != nil {
s.log.Fatal().Stack().Msgf("failed reading directory: %s", err)
s.log.Fatal().Err(err).Stack().Msg("failed reading directory")
return errors.Wrap(err, "could not read directory")
}
customCount := 0
@ -474,13 +491,13 @@ func (s *service) LoadCustomIndexerDefinitions() error {
data, err := os.ReadFile(file)
if err != nil {
s.log.Error().Stack().Err(err).Msgf("failed reading file: %v", file)
return err
return errors.Wrap(err, "could not read file: %v", file)
}
var d *domain.IndexerDefinition
if err = yaml.Unmarshal(data, &d); err != nil {
s.log.Error().Stack().Err(err).Msgf("failed unmarshal file: %v", file)
return err
return errors.Wrap(err, "could not unmarshal file: %v", file)
}
if d == nil {