Skip to content

Commit

Permalink
reference global logrus (superseriousbusiness#274)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
raidancampbell authored Oct 11, 2021
1 parent 367bdca commit 083099a
Show file tree
Hide file tree
Showing 210 changed files with 506 additions and 662 deletions.
6 changes: 3 additions & 3 deletions cmd/gotosocial/runaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ func runAction(c *cli.Context, a cliactions.GTSAction) error {
return fmt.Errorf("error parsing config: %s", err)
}

// create a logger with the log level, formatting, and output splitter already set
log, err := log.New(conf.LogLevel)
// initialize the global logger to the log level, with formatting and output splitter already set
err = log.Initialize(conf.LogLevel)
if err != nil {
return fmt.Errorf("error creating logger: %s", err)
}

return a(c.Context, conf, log)
return a(c.Context, conf)
}
5 changes: 1 addition & 4 deletions internal/api/client/account/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import (
"strings"

"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/superseriousbusiness/gotosocial/internal/api"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/processing"
Expand Down Expand Up @@ -75,15 +74,13 @@ const (
type Module struct {
config *config.Config
processor processing.Processor
log *logrus.Logger
}

// New returns a new account module
func New(config *config.Config, processor processing.Processor, log *logrus.Logger) api.ClientModule {
func New(config *config.Config, processor processing.Processor) api.ClientModule {
return &Module{
config: config,
processor: processor,
log: log,
}
}

Expand Down
6 changes: 2 additions & 4 deletions internal/api/client/account/account_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (

"git.iim.gay/grufwub/go-store/kv"
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/suite"
"github.com/superseriousbusiness/gotosocial/internal/api/client/account"
"github.com/superseriousbusiness/gotosocial/internal/config"
Expand All @@ -26,7 +25,6 @@ type AccountStandardTestSuite struct {
suite.Suite
config *config.Config
db db.DB
log *logrus.Logger
tc typeutils.TypeConverter
storage *kv.KVStore
federator federation.Federator
Expand Down Expand Up @@ -59,10 +57,10 @@ func (suite *AccountStandardTestSuite) SetupTest() {
suite.config = testrig.NewTestConfig()
suite.db = testrig.NewTestDB()
suite.storage = testrig.NewTestStorage()
suite.log = testrig.NewTestLog()
testrig.InitTestLog()
suite.federator = testrig.NewTestFederator(suite.db, testrig.NewTestTransportController(testrig.NewMockHTTPClient(nil), suite.db), suite.storage)
suite.processor = testrig.NewTestProcessor(suite.db, suite.storage, suite.federator)
suite.accountModule = account.New(suite.config, suite.processor, suite.log).(*account.Module)
suite.accountModule = account.New(suite.config, suite.processor).(*account.Module)
testrig.StandardDBSetup(suite.db, nil)
testrig.StandardStorageSetup(suite.storage, "../../../../testrig/media")
}
Expand Down
3 changes: 2 additions & 1 deletion internal/api/client/account/accountcreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package account

import (
"errors"
"github.com/sirupsen/logrus"
"net"
"net/http"

Expand Down Expand Up @@ -67,7 +68,7 @@ import (
// '500':
// description: internal error
func (m *Module) AccountCreatePOSTHandler(c *gin.Context) {
l := m.log.WithField("func", "accountCreatePOSTHandler")
l := logrus.WithField("func", "accountCreatePOSTHandler")
authed, err := oauth.Authed(c, true, true, false, false)
if err != nil {
l.Debugf("couldn't auth: %s", err)
Expand Down
3 changes: 2 additions & 1 deletion internal/api/client/account/accountupdate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package account

import (
"fmt"
"github.com/sirupsen/logrus"
"net/http"
"strconv"

Expand Down Expand Up @@ -100,7 +101,7 @@ import (
// '400':
// description: bad request
func (m *Module) AccountUpdateCredentialsPATCHHandler(c *gin.Context) {
l := m.log.WithField("func", "accountUpdateCredentialsPATCHHandler")
l := logrus.WithField("func", "accountUpdateCredentialsPATCHHandler")
authed, err := oauth.Authed(c, true, true, true, true)
if err != nil {
l.Debugf("couldn't auth: %s", err)
Expand Down
3 changes: 2 additions & 1 deletion internal/api/client/account/accountverify.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package account

import (
"github.com/sirupsen/logrus"
"net/http"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -51,7 +52,7 @@ import (
// '404':
// description: not found
func (m *Module) AccountVerifyGETHandler(c *gin.Context) {
l := m.log.WithField("func", "accountVerifyGETHandler")
l := logrus.WithField("func", "accountVerifyGETHandler")
authed, err := oauth.Authed(c, true, false, false, true)
if err != nil {
l.Debugf("couldn't auth: %s", err)
Expand Down
3 changes: 2 additions & 1 deletion internal/api/client/account/relationships.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package account

import (
"github.com/sirupsen/logrus"
"net/http"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -47,7 +48,7 @@ import (
// '404':
// description: not found
func (m *Module) AccountRelationshipsGETHandler(c *gin.Context) {
l := m.log.WithField("func", "AccountRelationshipsGETHandler")
l := logrus.WithField("func", "AccountRelationshipsGETHandler")

authed, err := oauth.Authed(c, true, true, true, true)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/api/client/account/statuses.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package account

import (
"github.com/sirupsen/logrus"
"net/http"
"strconv"

Expand Down Expand Up @@ -96,7 +97,7 @@ import (
// '404':
// description: not found
func (m *Module) AccountStatusesGETHandler(c *gin.Context) {
l := m.log.WithField("func", "AccountStatusesGETHandler")
l := logrus.WithField("func", "AccountStatusesGETHandler")

authed, err := oauth.Authed(c, false, false, false, false)
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion internal/api/client/account/unfollow.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package account

import (
"github.com/sirupsen/logrus"
"net/http"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -60,7 +61,7 @@ import (
// '404':
// description: not found
func (m *Module) AccountUnfollowPOSTHandler(c *gin.Context) {
l := m.log.WithField("func", "AccountUnfollowPOSTHandler")
l := logrus.WithField("func", "AccountUnfollowPOSTHandler")
authed, err := oauth.Authed(c, true, true, true, true)
if err != nil {
l.Debug(err)
Expand Down
5 changes: 1 addition & 4 deletions internal/api/client/admin/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package admin
import (
"net/http"

"github.com/sirupsen/logrus"
"github.com/superseriousbusiness/gotosocial/internal/api"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/processing"
Expand Down Expand Up @@ -50,15 +49,13 @@ const (
type Module struct {
config *config.Config
processor processing.Processor
log *logrus.Logger
}

// New returns a new admin module
func New(config *config.Config, processor processing.Processor, log *logrus.Logger) api.ClientModule {
func New(config *config.Config, processor processing.Processor) api.ClientModule {
return &Module{
config: config,
processor: processor,
log: log,
}
}

Expand Down
2 changes: 1 addition & 1 deletion internal/api/client/admin/domainblockcreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ import (
// '400':
// description: bad request
func (m *Module) DomainBlocksPOSTHandler(c *gin.Context) {
l := m.log.WithFields(logrus.Fields{
l := logrus.WithFields(logrus.Fields{
"func": "DomainBlocksPOSTHandler",
"request_uri": c.Request.RequestURI,
"user_agent": c.Request.UserAgent(),
Expand Down
2 changes: 1 addition & 1 deletion internal/api/client/admin/domainblockdelete.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ import (
// '404':
// description: not found
func (m *Module) DomainBlockDELETEHandler(c *gin.Context) {
l := m.log.WithFields(logrus.Fields{
l := logrus.WithFields(logrus.Fields{
"func": "DomainBlockDELETEHandler",
"request_uri": c.Request.RequestURI,
"user_agent": c.Request.UserAgent(),
Expand Down
2 changes: 1 addition & 1 deletion internal/api/client/admin/domainblockget.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ import (
// '404':
// description: not found
func (m *Module) DomainBlockGETHandler(c *gin.Context) {
l := m.log.WithFields(logrus.Fields{
l := logrus.WithFields(logrus.Fields{
"func": "DomainBlockGETHandler",
"request_uri": c.Request.RequestURI,
"user_agent": c.Request.UserAgent(),
Expand Down
2 changes: 1 addition & 1 deletion internal/api/client/admin/domainblocksget.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ import (
// '404':
// description: not found
func (m *Module) DomainBlocksGETHandler(c *gin.Context) {
l := m.log.WithFields(logrus.Fields{
l := logrus.WithFields(logrus.Fields{
"func": "DomainBlocksGETHandler",
"request_uri": c.Request.RequestURI,
"user_agent": c.Request.UserAgent(),
Expand Down
2 changes: 1 addition & 1 deletion internal/api/client/admin/emojicreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ import (
// '400':
// description: bad request
func (m *Module) emojiCreatePOSTHandler(c *gin.Context) {
l := m.log.WithFields(logrus.Fields{
l := logrus.WithFields(logrus.Fields{
"func": "emojiCreatePOSTHandler",
"request_uri": c.Request.RequestURI,
"user_agent": c.Request.UserAgent(),
Expand Down
5 changes: 1 addition & 4 deletions internal/api/client/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package app
import (
"net/http"

"github.com/sirupsen/logrus"
"github.com/superseriousbusiness/gotosocial/internal/api"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/processing"
Expand All @@ -35,15 +34,13 @@ const BasePath = "/api/v1/apps"
type Module struct {
config *config.Config
processor processing.Processor
log *logrus.Logger
}

// New returns a new auth module
func New(config *config.Config, processor processing.Processor, log *logrus.Logger) api.ClientModule {
func New(config *config.Config, processor processing.Processor) api.ClientModule {
return &Module{
config: config,
processor: processor,
log: log,
}
}

Expand Down
3 changes: 2 additions & 1 deletion internal/api/client/app/appcreate.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package app

import (
"fmt"
"github.com/sirupsen/logrus"
"net/http"

"github.com/gin-gonic/gin"
Expand Down Expand Up @@ -63,7 +64,7 @@ import (
// '500':
// description: internal error
func (m *Module) AppsPOSTHandler(c *gin.Context) {
l := m.log.WithField("func", "AppsPOSTHandler")
l := logrus.WithField("func", "AppsPOSTHandler")
l.Trace("entering AppsPOSTHandler")

authed, err := oauth.Authed(c, false, false, false, false)
Expand Down
5 changes: 1 addition & 4 deletions internal/api/client/auth/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ package auth
import (
"net/http"

"github.com/sirupsen/logrus"
"github.com/superseriousbusiness/gotosocial/internal/api"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
Expand Down Expand Up @@ -58,17 +57,15 @@ type Module struct {
db db.DB
server oauth.Server
idp oidc.IDP
log *logrus.Logger
}

// New returns a new auth module
func New(config *config.Config, db db.DB, server oauth.Server, idp oidc.IDP, log *logrus.Logger) api.ClientModule {
func New(config *config.Config, db db.DB, server oauth.Server, idp oidc.IDP) api.ClientModule {
return &Module{
config: config,
db: db,
server: server,
idp: idp,
log: log,
}
}

Expand Down
4 changes: 2 additions & 2 deletions internal/api/client/auth/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ func (suite *AuthTestSuite) SetupTest() {

log := logrus.New()
log.SetLevel(logrus.TraceLevel)
db, err := bundb.NewBunDBService(context.Background(), suite.config, log)
db, err := bundb.NewBunDBService(context.Background(), suite.config)
if err != nil {
logrus.Panicf("error creating database connection: %s", err)
}
Expand All @@ -124,7 +124,7 @@ func (suite *AuthTestSuite) SetupTest() {
}
}

suite.oauthServer = oauth.New(context.Background(), suite.db, log)
suite.oauthServer = oauth.New(context.Background(), suite.db)

if err := suite.db.Put(context.Background(), suite.testAccount); err != nil {
logrus.Panicf("could not insert test account into db: %s", err)
Expand Down
5 changes: 3 additions & 2 deletions internal/api/client/auth/authorize.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package auth
import (
"errors"
"fmt"
"github.com/sirupsen/logrus"
"net/http"
"net/url"
"strings"
Expand All @@ -37,7 +38,7 @@ import (
// The idea here is to present an oauth authorize page to the user, with a button
// that they have to click to accept.
func (m *Module) AuthorizeGETHandler(c *gin.Context) {
l := m.log.WithField("func", "AuthorizeGETHandler")
l := logrus.WithField("func", "AuthorizeGETHandler")
s := sessions.Default(c)

// UserID will be set in the session by AuthorizePOSTHandler if the caller has already gone through the authentication flow
Expand Down Expand Up @@ -123,7 +124,7 @@ func (m *Module) AuthorizeGETHandler(c *gin.Context) {
// At this point we assume that the user has A) logged in and B) accepted that the app should act for them,
// so we should proceed with the authentication flow and generate an oauth token for them if we can.
func (m *Module) AuthorizePOSTHandler(c *gin.Context) {
l := m.log.WithField("func", "AuthorizePOSTHandler")
l := logrus.WithField("func", "AuthorizePOSTHandler")
s := sessions.Default(c)

// We need to retrieve the original form submitted to the authorizeGEThandler, and
Expand Down
3 changes: 2 additions & 1 deletion internal/api/client/auth/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ package auth

import (
"github.com/gin-gonic/gin"
"github.com/sirupsen/logrus"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/oauth"
Expand All @@ -31,7 +32,7 @@ import (
// If user or account can't be found, then the handler won't *fail*, in case the server wants to allow
// public requests that don't have a Bearer token set (eg., for public instance information and so on).
func (m *Module) OauthTokenMiddleware(c *gin.Context) {
l := m.log.WithField("func", "OauthTokenMiddleware")
l := logrus.WithField("func", "OauthTokenMiddleware")
l.Trace("entering OauthTokenMiddleware")

ti, err := m.server.ValidationBearerToken(c.Copy().Request)
Expand Down
Loading

0 comments on commit 083099a

Please sign in to comment.