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
parent 367bdca250
commit 083099a957
210 changed files with 506 additions and 662 deletions

View File

@@ -22,7 +22,6 @@ import (
"context"
"mime/multipart"
"github.com/sirupsen/logrus"
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
@@ -87,21 +86,19 @@ type processor struct {
formatter text.Formatter
db db.DB
federator federation.Federator
log *logrus.Logger
}
// New returns a new account processor.
func New(db db.DB, tc typeutils.TypeConverter, mediaHandler media.Handler, oauthServer oauth.Server, fromClientAPI chan messages.FromClientAPI, federator federation.Federator, config *config.Config, log *logrus.Logger) Processor {
func New(db db.DB, tc typeutils.TypeConverter, mediaHandler media.Handler, oauthServer oauth.Server, fromClientAPI chan messages.FromClientAPI, federator federation.Federator, config *config.Config) Processor {
return &processor{
tc: tc,
config: config,
mediaHandler: mediaHandler,
fromClientAPI: fromClientAPI,
oauthServer: oauthServer,
filter: visibility.NewFilter(db, log),
formatter: text.NewFormatter(config, db, log),
filter: visibility.NewFilter(db),
formatter: text.NewFormatter(config, db),
db: db,
federator: federator,
log: log,
}
}

View File

@@ -21,7 +21,6 @@ package account_test
import (
"git.iim.gay/grufwub/go-store/kv"
"github.com/go-fed/activity/pub"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/suite"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
@@ -41,7 +40,6 @@ type AccountStandardTestSuite struct {
suite.Suite
config *config.Config
db db.DB
log *logrus.Logger
tc typeutils.TypeConverter
storage *kv.KVStore
mediaHandler media.Handler
@@ -77,7 +75,7 @@ func (suite *AccountStandardTestSuite) SetupSuite() {
func (suite *AccountStandardTestSuite) SetupTest() {
suite.config = testrig.NewTestConfig()
suite.db = testrig.NewTestDB()
suite.log = testrig.NewTestLog()
testrig.InitTestLog()
suite.tc = testrig.NewTestTypeConverter(suite.db)
suite.storage = testrig.NewTestStorage()
suite.mediaHandler = testrig.NewTestMediaHandler(suite.db, suite.storage)
@@ -86,7 +84,7 @@ func (suite *AccountStandardTestSuite) SetupTest() {
suite.httpClient = testrig.NewMockHTTPClient(nil)
suite.transportController = testrig.NewTestTransportController(suite.httpClient, suite.db)
suite.federator = testrig.NewTestFederator(suite.db, suite.transportController, suite.storage)
suite.accountProcessor = account.New(suite.db, suite.tc, suite.mediaHandler, suite.oauthServer, suite.fromClientAPIChan, suite.federator, suite.config, suite.log)
suite.accountProcessor = account.New(suite.db, suite.tc, suite.mediaHandler, suite.oauthServer, suite.fromClientAPIChan, suite.federator, suite.config)
testrig.StandardDBSetup(suite.db, nil)
testrig.StandardStorageSetup(suite.storage, "../../../testrig/media")
}

View File

@@ -21,6 +21,7 @@ package account
import (
"context"
"fmt"
"github.com/sirupsen/logrus"
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
@@ -29,7 +30,7 @@ import (
)
func (p *processor) Create(ctx context.Context, applicationToken oauth2.TokenInfo, application *gtsmodel.Application, form *apimodel.AccountCreateRequest) (*apimodel.Token, error) {
l := p.log.WithField("func", "accountCreate")
l := logrus.WithField("func", "accountCreate")
emailAvailable, err := p.db.IsEmailAvailable(ctx, form.Email)
if err != nil {

View File

@@ -58,7 +58,7 @@ func (p *processor) Delete(ctx context.Context, account *gtsmodel.Account, origi
if account.Domain != "" {
fields["domain"] = account.Domain
}
l := p.log.WithFields(fields)
l := logrus.WithFields(fields)
l.Debug("beginning account delete process")

View File

@@ -23,6 +23,7 @@ import (
"context"
"errors"
"fmt"
"github.com/sirupsen/logrus"
"io"
"mime/multipart"
@@ -37,7 +38,7 @@ import (
)
func (p *processor) Update(ctx context.Context, account *gtsmodel.Account, form *apimodel.UpdateCredentialsRequest) (*apimodel.Account, error) {
l := p.log.WithField("func", "AccountUpdate")
l := logrus.WithField("func", "AccountUpdate")
if form.Discoverable != nil {
account.Discoverable = *form.Discoverable

View File

@@ -22,7 +22,6 @@ import (
"context"
"mime/multipart"
"github.com/sirupsen/logrus"
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
@@ -49,17 +48,15 @@ type processor struct {
mediaHandler media.Handler
fromClientAPI chan messages.FromClientAPI
db db.DB
log *logrus.Logger
}
// New returns a new admin processor.
func New(db db.DB, tc typeutils.TypeConverter, mediaHandler media.Handler, fromClientAPI chan messages.FromClientAPI, config *config.Config, log *logrus.Logger) Processor {
func New(db db.DB, tc typeutils.TypeConverter, mediaHandler media.Handler, fromClientAPI chan messages.FromClientAPI, config *config.Config) Processor {
return &processor{
tc: tc,
config: config,
mediaHandler: mediaHandler,
fromClientAPI: fromClientAPI,
db: db,
log: log,
}
}

View File

@@ -87,7 +87,7 @@ func (p *processor) DomainBlockCreate(ctx context.Context, account *gtsmodel.Acc
// 2. Delete the instance account for that instance if it exists.
// 3. Select all accounts from this instance and pass them through the delete functionality of the processor.
func (p *processor) initiateDomainBlockSideEffects(ctx context.Context, account *gtsmodel.Account, block *gtsmodel.DomainBlock) {
l := p.log.WithFields(logrus.Fields{
l := logrus.WithFields(logrus.Fields{
"func": "domainBlockProcessSideEffects",
"domain": block.Domain,
})

View File

@@ -35,7 +35,7 @@ import (
// and directs the message into the appropriate side effect handler function, or simply does nothing if there's
// no handler function defined for the combination of Activity and Object.
func (p *processor) ProcessFromFederator(ctx context.Context, federatorMsg messages.FromFederator) error {
l := p.log.WithFields(logrus.Fields{
l := logrus.WithFields(logrus.Fields{
"func": "processFromFederator",
"APActivityType": federatorMsg.APActivityType,
"APObjectType": federatorMsg.APObjectType,

View File

@@ -22,7 +22,6 @@ import (
"context"
"git.iim.gay/grufwub/go-store/kv"
"github.com/sirupsen/logrus"
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
@@ -49,17 +48,15 @@ type processor struct {
mediaHandler media.Handler
storage *kv.KVStore
db db.DB
log *logrus.Logger
}
// New returns a new media processor.
func New(db db.DB, tc typeutils.TypeConverter, mediaHandler media.Handler, storage *kv.KVStore, config *config.Config, log *logrus.Logger) Processor {
func New(db db.DB, tc typeutils.TypeConverter, mediaHandler media.Handler, storage *kv.KVStore, config *config.Config) Processor {
return &processor{
tc: tc,
config: config,
mediaHandler: mediaHandler,
storage: storage,
db: db,
log: log,
}
}

View File

@@ -20,6 +20,7 @@ package processing
import (
"context"
"github.com/sirupsen/logrus"
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
@@ -27,7 +28,7 @@ import (
)
func (p *processor) NotificationsGet(ctx context.Context, authed *oauth.Auth, limit int, maxID string, sinceID string) ([]*apimodel.Notification, gtserror.WithCode) {
l := p.log.WithField("func", "NotificationsGet")
l := logrus.WithField("func", "NotificationsGet")
notifs, err := p.db.GetNotifications(ctx, authed.Account.ID, limit, maxID, sinceID)
if err != nil {

View File

@@ -229,7 +229,6 @@ type processor struct {
fromFederator chan messages.FromFederator
federator federation.Federator
stop chan interface{}
log *logrus.Logger
config *config.Config
tc typeutils.TypeConverter
oauthServer oauth.Server
@@ -250,23 +249,22 @@ type processor struct {
mediaProcessor mediaProcessor.Processor
}
// NewProcessor returns a new Processor that uses the given federator and logger
func NewProcessor(config *config.Config, tc typeutils.TypeConverter, federator federation.Federator, oauthServer oauth.Server, mediaHandler media.Handler, storage *kv.KVStore, timelineManager timeline.Manager, db db.DB, log *logrus.Logger) Processor {
// NewProcessor returns a new Processor that uses the given federator
func NewProcessor(config *config.Config, tc typeutils.TypeConverter, federator federation.Federator, oauthServer oauth.Server, mediaHandler media.Handler, storage *kv.KVStore, timelineManager timeline.Manager, db db.DB) Processor {
fromClientAPI := make(chan messages.FromClientAPI, 1000)
fromFederator := make(chan messages.FromFederator, 1000)
statusProcessor := status.New(db, tc, config, fromClientAPI, log)
streamingProcessor := streaming.New(db, oauthServer, log)
accountProcessor := account.New(db, tc, mediaHandler, oauthServer, fromClientAPI, federator, config, log)
adminProcessor := admin.New(db, tc, mediaHandler, fromClientAPI, config, log)
mediaProcessor := mediaProcessor.New(db, tc, mediaHandler, storage, config, log)
statusProcessor := status.New(db, tc, config, fromClientAPI)
streamingProcessor := streaming.New(db, oauthServer)
accountProcessor := account.New(db, tc, mediaHandler, oauthServer, fromClientAPI, federator, config)
adminProcessor := admin.New(db, tc, mediaHandler, fromClientAPI, config)
mediaProcessor := mediaProcessor.New(db, tc, mediaHandler, storage, config)
return &processor{
fromClientAPI: fromClientAPI,
fromFederator: fromFederator,
federator: federator,
stop: make(chan interface{}),
log: log,
config: config,
tc: tc,
oauthServer: oauthServer,
@@ -274,7 +272,7 @@ func NewProcessor(config *config.Config, tc typeutils.TypeConverter, federator f
storage: storage,
timelineManager: timelineManager,
db: db,
filter: visibility.NewFilter(db, log),
filter: visibility.NewFilter(db),
accountProcessor: accountProcessor,
adminProcessor: adminProcessor,
@@ -291,17 +289,17 @@ func (p *processor) Start(ctx context.Context) error {
for {
select {
case clientMsg := <-p.fromClientAPI:
p.log.Tracef("received message FROM client API: %+v", clientMsg)
logrus.Tracef("received message FROM client API: %+v", clientMsg)
go func() {
if err := p.ProcessFromClientAPI(ctx, clientMsg); err != nil {
p.log.Error(err)
logrus.Error(err)
}
}()
case federatorMsg := <-p.fromFederator:
p.log.Tracef("received message FROM federator: %+v", federatorMsg)
logrus.Tracef("received message FROM federator: %+v", federatorMsg)
go func() {
if err := p.ProcessFromFederator(ctx, federatorMsg); err != nil {
p.log.Error(err)
logrus.Error(err)
}
}()
case <-p.stop:

View File

@@ -28,7 +28,6 @@ import (
"git.iim.gay/grufwub/go-store/kv"
"github.com/go-fed/activity/streams"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/suite"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
@@ -48,7 +47,6 @@ type ProcessingStandardTestSuite struct {
suite.Suite
config *config.Config
db db.DB
log *logrus.Logger
storage *kv.KVStore
typeconverter typeutils.TypeConverter
transportController transport.Controller
@@ -100,7 +98,7 @@ func (suite *ProcessingStandardTestSuite) SetupSuite() {
func (suite *ProcessingStandardTestSuite) SetupTest() {
suite.config = testrig.NewTestConfig()
suite.db = testrig.NewTestDB()
suite.log = testrig.NewTestLog()
testrig.InitTestLog()
suite.storage = testrig.NewTestStorage()
suite.typeconverter = testrig.NewTestTypeConverter(suite.db)
@@ -190,16 +188,7 @@ func (suite *ProcessingStandardTestSuite) SetupTest() {
suite.mediaHandler = testrig.NewTestMediaHandler(suite.db, suite.storage)
suite.timelineManager = testrig.NewTestTimelineManager(suite.db)
suite.processor = processing.NewProcessor(
suite.config,
suite.typeconverter,
suite.federator,
suite.oauthServer,
suite.mediaHandler,
suite.storage,
suite.timelineManager,
suite.db,
suite.log)
suite.processor = processing.NewProcessor(suite.config, suite.typeconverter, suite.federator, suite.oauthServer, suite.mediaHandler, suite.storage, suite.timelineManager, suite.db)
testrig.StandardDBSetup(suite.db, suite.testAccounts)
testrig.StandardStorageSetup(suite.storage, "../../testrig/media")

View File

@@ -34,7 +34,7 @@ import (
)
func (p *processor) SearchGet(ctx context.Context, authed *oauth.Auth, searchQuery *apimodel.SearchQuery) (*apimodel.SearchResult, gtserror.WithCode) {
l := p.log.WithFields(logrus.Fields{
l := logrus.WithFields(logrus.Fields{
"func": "SearchGet",
"query": searchQuery.Query,
})
@@ -116,7 +116,7 @@ func (p *processor) SearchGet(ctx context.Context, authed *oauth.Auth, searchQue
}
func (p *processor) searchStatusByURI(ctx context.Context, authed *oauth.Auth, uri *url.URL, resolve bool) (*gtsmodel.Status, error) {
l := p.log.WithFields(logrus.Fields{
l := logrus.WithFields(logrus.Fields{
"func": "searchStatusByURI",
"uri": uri.String(),
"resolve": resolve,

View File

@@ -21,7 +21,6 @@ package status
import (
"context"
"github.com/sirupsen/logrus"
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
@@ -77,18 +76,16 @@ type processor struct {
filter visibility.Filter
formatter text.Formatter
fromClientAPI chan messages.FromClientAPI
log *logrus.Logger
}
// New returns a new status processor.
func New(db db.DB, tc typeutils.TypeConverter, config *config.Config, fromClientAPI chan messages.FromClientAPI, log *logrus.Logger) Processor {
func New(db db.DB, tc typeutils.TypeConverter, config *config.Config, fromClientAPI chan messages.FromClientAPI) Processor {
return &processor{
tc: tc,
config: config,
db: db,
filter: visibility.NewFilter(db, log),
formatter: text.NewFormatter(config, db, log),
filter: visibility.NewFilter(db),
formatter: text.NewFormatter(config, db),
fromClientAPI: fromClientAPI,
log: log,
}
}

View File

@@ -19,7 +19,6 @@
package status_test
import (
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/suite"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
@@ -34,7 +33,6 @@ type StatusStandardTestSuite struct {
suite.Suite
config *config.Config
db db.DB
log *logrus.Logger
typeConverter typeutils.TypeConverter
fromClientAPIChan chan messages.FromClientAPI

View File

@@ -67,10 +67,10 @@ func (suite *UtilTestSuite) SetupSuite() {
func (suite *UtilTestSuite) SetupTest() {
suite.config = testrig.NewTestConfig()
suite.db = testrig.NewTestDB()
suite.log = testrig.NewTestLog()
testrig.InitTestLog()
suite.typeConverter = testrig.NewTestTypeConverter(suite.db)
suite.fromClientAPIChan = make(chan messages.FromClientAPI, 100)
suite.status = status.New(suite.db, suite.typeConverter, suite.config, suite.fromClientAPIChan, suite.log)
suite.status = status.New(suite.db, suite.typeConverter, suite.config, suite.fromClientAPIChan)
testrig.StandardDBSetup(suite.db, nil)
}

View File

@@ -31,7 +31,7 @@ import (
)
func (p *processor) OpenStreamForAccount(ctx context.Context, account *gtsmodel.Account, streamType string) (*stream.Stream, gtserror.WithCode) {
l := p.log.WithFields(logrus.Fields{
l := logrus.WithFields(logrus.Fields{
"func": "OpenStreamForAccount",
"account": account.ID,
"streamType": streamType,

View File

@@ -22,7 +22,6 @@ import (
"context"
"sync"
"github.com/sirupsen/logrus"
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtserror"
@@ -47,16 +46,14 @@ type Processor interface {
type processor struct {
db db.DB
log *logrus.Logger
oauthServer oauth.Server
streamMap *sync.Map
}
// New returns a new status processor.
func New(db db.DB, oauthServer oauth.Server, log *logrus.Logger) Processor {
func New(db db.DB, oauthServer oauth.Server) Processor {
return &processor{
db: db,
log: log,
oauthServer: oauthServer,
streamMap: &sync.Map{},
}

View File

@@ -19,7 +19,6 @@
package streaming_test
import (
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/suite"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
@@ -34,7 +33,6 @@ type StreamingTestSuite struct {
testTokens map[string]*gtsmodel.Token
db db.DB
oauthServer oauth.Server
log *logrus.Logger
streamingProcessor streaming.Processor
}
@@ -44,8 +42,8 @@ func (suite *StreamingTestSuite) SetupTest() {
suite.testTokens = testrig.NewTestTokens()
suite.db = testrig.NewTestDB()
suite.oauthServer = testrig.NewTestOauthServer(suite.db)
suite.log = testrig.NewTestLog()
suite.streamingProcessor = streaming.New(suite.db, suite.oauthServer, suite.log)
testrig.InitTestLog()
suite.streamingProcessor = streaming.New(suite.db, suite.oauthServer)
testrig.StandardDBSetup(suite.db, suite.testAccounts)
}

View File

@@ -21,6 +21,7 @@ package processing
import (
"context"
"fmt"
"github.com/sirupsen/logrus"
"net/url"
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
@@ -129,7 +130,7 @@ func (p *processor) FavedTimelineGet(ctx context.Context, authed *oauth.Auth, ma
}
func (p *processor) filterPublicStatuses(ctx context.Context, authed *oauth.Auth, statuses []*gtsmodel.Status) ([]*apimodel.Status, error) {
l := p.log.WithField("func", "filterPublicStatuses")
l := logrus.WithField("func", "filterPublicStatuses")
apiStatuses := []*apimodel.Status{}
for _, s := range statuses {
@@ -164,7 +165,7 @@ func (p *processor) filterPublicStatuses(ctx context.Context, authed *oauth.Auth
}
func (p *processor) filterFavedStatuses(ctx context.Context, authed *oauth.Auth, statuses []*gtsmodel.Status) ([]*apimodel.Status, error) {
l := p.log.WithField("func", "filterFavedStatuses")
l := logrus.WithField("func", "filterFavedStatuses")
apiStatuses := []*apimodel.Status{}
for _, s := range statuses {