reference global logrus (#274)

* reference logrus' global logger instead of passing and storing a logger reference everywhere

* always directly use global logrus logger instead of referencing an instance

* test suites should also directly use the global logrus logger

* rename gin logging function to clarify that it's middleware

* correct comments which erroneously referenced removed logger parameter

* setting log level for tests now uses logrus' exported type instead of the string value, to guarantee error isn't possible
This commit is contained in:
R. Aidan Campbell 2021-10-11 05:37:33 -07:00 committed by GitHub
commit 083099a957
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
210 changed files with 506 additions and 662 deletions

View file

@ -59,7 +59,7 @@ func (d *deref) EnrichRemoteAccount(ctx context.Context, username string, accoun
updated, err := d.db.UpdateAccount(ctx, account)
if err != nil {
d.log.Errorf("EnrichRemoteAccount: error updating account: %s", err)
logrus.Errorf("EnrichRemoteAccount: error updating account: %s", err)
return account, nil
}
@ -203,7 +203,7 @@ func (d *deref) dereferenceAccountable(ctx context.Context, username string, rem
// PopulateAccountFields populates any fields on the given account that weren't populated by the initial
// dereferencing. This includes things like header and avatar etc.
func (d *deref) PopulateAccountFields(ctx context.Context, account *gtsmodel.Account, requestingUsername string, refresh bool) error {
l := d.log.WithFields(logrus.Fields{
l := logrus.WithFields(logrus.Fields{
"func": "PopulateAccountFields",
"requestingUsername": requestingUsername,
})

View file

@ -34,7 +34,7 @@ func (d *deref) GetRemoteAttachment(ctx context.Context, requestingUsername stri
}
remoteAttachmentURL := minAttachment.RemoteURL
l := d.log.WithFields(logrus.Fields{
l := logrus.WithFields(logrus.Fields{
"username": requestingUsername,
"remoteAttachmentURL": remoteAttachmentURL,
})

View file

@ -23,7 +23,6 @@ import (
"net/url"
"sync"
"github.com/sirupsen/logrus"
"github.com/superseriousbusiness/gotosocial/internal/ap"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
@ -79,7 +78,6 @@ type Dereferencer interface {
}
type deref struct {
log *logrus.Logger
db db.DB
typeConverter typeutils.TypeConverter
transportController transport.Controller
@ -90,9 +88,8 @@ type deref struct {
}
// NewDereferencer returns a Dereferencer initialized with the given parameters.
func NewDereferencer(config *config.Config, db db.DB, typeConverter typeutils.TypeConverter, transportController transport.Controller, mediaHandler media.Handler, log *logrus.Logger) Dereferencer {
func NewDereferencer(config *config.Config, db db.DB, typeConverter typeutils.TypeConverter, transportController transport.Controller, mediaHandler media.Handler) Dereferencer {
return &deref{
log: log,
db: db,
typeConverter: typeConverter,
transportController: transportController,

View file

@ -41,7 +41,6 @@ type DereferencerStandardTestSuite struct {
suite.Suite
config *config.Config
db db.DB
log *logrus.Logger
storage *kv.KVStore
testRemoteStatuses map[string]vocab.ActivityStreamsNote
@ -64,14 +63,9 @@ func (suite *DereferencerStandardTestSuite) SetupSuite() {
func (suite *DereferencerStandardTestSuite) SetupTest() {
suite.config = testrig.NewTestConfig()
suite.db = testrig.NewTestDB()
suite.log = testrig.NewTestLog()
testrig.InitTestLog()
suite.storage = testrig.NewTestStorage()
suite.dereferencer = dereferencing.NewDereferencer(suite.config,
suite.db,
testrig.NewTestTypeConverter(suite.db),
suite.mockTransportController(),
testrig.NewTestMediaHandler(suite.db, suite.storage),
suite.log)
suite.dereferencer = dereferencing.NewDereferencer(suite.config, suite.db, testrig.NewTestTypeConverter(suite.db), suite.mockTransportController(), testrig.NewTestMediaHandler(suite.db, suite.storage))
testrig.StandardDBSetup(suite.db, nil)
}
@ -85,7 +79,7 @@ func (suite *DereferencerStandardTestSuite) TearDownTest() {
// or note or attachment that we have stored, then just a 200 code will be returned, with an empty body.
func (suite *DereferencerStandardTestSuite) mockTransportController() transport.Controller {
do := func(req *http.Request) (*http.Response, error) {
suite.log.Debugf("received request for %s", req.URL)
logrus.Debugf("received request for %s", req.URL)
responseBytes := []byte{}
responseType := ""
@ -140,7 +134,7 @@ func (suite *DereferencerStandardTestSuite) mockTransportController() transport.
if len(responseBytes) != 0 {
// we found something, so print what we're going to return
suite.log.Debugf("returning response %s", string(responseBytes))
logrus.Debugf("returning response %s", string(responseBytes))
}
responseLength = len(responseBytes)

View file

@ -236,7 +236,7 @@ func (d *deref) dereferenceStatusable(ctx context.Context, username string, remo
// and attach them to the status. The status itself will not be added to the database yet,
// that's up the caller to do.
func (d *deref) populateStatusFields(ctx context.Context, status *gtsmodel.Status, requestingUsername string, includeParent bool) error {
l := d.log.WithFields(logrus.Fields{
l := logrus.WithFields(logrus.Fields{
"func": "dereferenceStatusFields",
"status": fmt.Sprintf("%+v", status),
})
@ -292,8 +292,6 @@ func (d *deref) populateStatusFields(ctx context.Context, status *gtsmodel.Statu
}
func (d *deref) populateStatusMentions(ctx context.Context, status *gtsmodel.Status, requestingUsername string) error {
l := d.log
// At this point, mentions should have the namestring and mentionedAccountURI set on them.
// We can use these to find the accounts.
@ -302,20 +300,20 @@ func (d *deref) populateStatusMentions(ctx context.Context, status *gtsmodel.Sta
for _, m := range status.Mentions {
if m.ID != "" {
// we've already populated this mention, since it has an ID
l.Debug("populateStatusMentions: mention already populated")
logrus.Debug("populateStatusMentions: mention already populated")
mentionIDs = append(mentionIDs, m.ID)
newMentions = append(newMentions, m)
continue
}
if m.TargetAccountURI == "" {
l.Debug("populateStatusMentions: target URI not set on mention")
logrus.Debug("populateStatusMentions: target URI not set on mention")
continue
}
targetAccountURI, err := url.Parse(m.TargetAccountURI)
if err != nil {
l.Debugf("populateStatusMentions: error parsing mentioned account uri %s: %s", m.TargetAccountURI, err)
logrus.Debugf("populateStatusMentions: error parsing mentioned account uri %s: %s", m.TargetAccountURI, err)
continue
}
@ -326,7 +324,7 @@ func (d *deref) populateStatusMentions(ctx context.Context, status *gtsmodel.Sta
if a, err := d.db.GetAccountByURI(ctx, targetAccountURI.String()); err != nil {
errs = append(errs, err.Error())
} else {
l.Debugf("populateStatusMentions: got target account %s with id %s through GetAccountByURI", targetAccountURI, a.ID)
logrus.Debugf("populateStatusMentions: got target account %s with id %s through GetAccountByURI", targetAccountURI, a.ID)
targetAccount = a
}
@ -336,13 +334,13 @@ func (d *deref) populateStatusMentions(ctx context.Context, status *gtsmodel.Sta
if a, _, err := d.GetRemoteAccount(ctx, requestingUsername, targetAccountURI, false); err != nil {
errs = append(errs, err.Error())
} else {
l.Debugf("populateStatusMentions: got target account %s with id %s through GetRemoteAccount", targetAccountURI, a.ID)
logrus.Debugf("populateStatusMentions: got target account %s with id %s through GetRemoteAccount", targetAccountURI, a.ID)
targetAccount = a
}
}
if targetAccount == nil {
l.Debugf("populateStatusMentions: couldn't get target account %s: %s", m.TargetAccountURI, strings.Join(errs, " : "))
logrus.Debugf("populateStatusMentions: couldn't get target account %s: %s", m.TargetAccountURI, strings.Join(errs, " : "))
continue
}
@ -382,8 +380,6 @@ func (d *deref) populateStatusMentions(ctx context.Context, status *gtsmodel.Sta
}
func (d *deref) populateStatusAttachments(ctx context.Context, status *gtsmodel.Status, requestingUsername string) error {
l := d.log
// At this point we should know:
// * the media type of the file we're looking for (a.File.ContentType)
// * the file type (a.Type)
@ -399,7 +395,7 @@ func (d *deref) populateStatusAttachments(ctx context.Context, status *gtsmodel.
attachment, err := d.GetRemoteAttachment(ctx, requestingUsername, a)
if err != nil {
l.Errorf("populateStatusAttachments: couldn't get remote attachment %s: %s", a.RemoteURL, err)
logrus.Errorf("populateStatusAttachments: couldn't get remote attachment %s: %s", a.RemoteURL, err)
continue
}

View file

@ -35,7 +35,7 @@ import (
// presented by remote instances as part of their replies collections, and will likely involve making several calls to
// multiple different hosts.
func (d *deref) DereferenceThread(ctx context.Context, username string, statusIRI *url.URL) error {
l := d.log.WithFields(logrus.Fields{
l := logrus.WithFields(logrus.Fields{
"func": "DereferenceThread",
"username": username,
"statusIRI": statusIRI.String(),
@ -69,7 +69,7 @@ func (d *deref) DereferenceThread(ctx context.Context, username string, statusIR
// iterateAncestors has the goal of reaching the oldest ancestor of a given status, and stashing all statuses along the way.
func (d *deref) iterateAncestors(ctx context.Context, username string, statusIRI url.URL) error {
l := d.log.WithFields(logrus.Fields{
l := logrus.WithFields(logrus.Fields{
"func": "iterateAncestors",
"username": username,
"statusIRI": statusIRI.String(),
@ -121,7 +121,7 @@ func (d *deref) iterateAncestors(ctx context.Context, username string, statusIRI
}
func (d *deref) iterateDescendants(ctx context.Context, username string, statusIRI url.URL, statusable ap.Statusable) error {
l := d.log.WithFields(logrus.Fields{
l := logrus.WithFields(logrus.Fields{
"func": "iterateDescendants",
"username": username,
"statusIRI": statusIRI.String(),