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