[chore] use our own logging implementation (#716)

* first commit

Signed-off-by: kim <grufwub@gmail.com>

* replace logging with our own log library

Signed-off-by: kim <grufwub@gmail.com>

* fix imports

Signed-off-by: kim <grufwub@gmail.com>

* fix log imports

Signed-off-by: kim <grufwub@gmail.com>

* add license text

Signed-off-by: kim <grufwub@gmail.com>

* fix package import cycle between config and log package

Signed-off-by: kim <grufwub@gmail.com>

* fix empty kv.Fields{} being passed to WithFields()

Signed-off-by: kim <grufwub@gmail.com>

* fix uses of log.WithFields() with whitespace issues and empty slices

Signed-off-by: kim <grufwub@gmail.com>

* *linter related grumbling*

Signed-off-by: kim <grufwub@gmail.com>

* gofmt the codebase! also fix more log.WithFields() formatting issues

Signed-off-by: kim <grufwub@gmail.com>

* update testrig code to match new changes

Signed-off-by: kim <grufwub@gmail.com>

* fix error wrapping in non fmt.Errorf function

Signed-off-by: kim <grufwub@gmail.com>

* add benchmarking of log.Caller() vs non-cached

Signed-off-by: kim <grufwub@gmail.com>

* fix syslog tests, add standard build tags to test runner to ensure consistency

Signed-off-by: kim <grufwub@gmail.com>

* make syslog tests more robust

Signed-off-by: kim <grufwub@gmail.com>

* fix caller depth arithmatic (is that how you spell it?)

Signed-off-by: kim <grufwub@gmail.com>

* update to use unkeyed fields in kv.Field{} instances

Signed-off-by: kim <grufwub@gmail.com>

* update go-kv library

Signed-off-by: kim <grufwub@gmail.com>

* update libraries list

Signed-off-by: kim <grufwub@gmail.com>

* fuck you linter get nerfed

Signed-off-by: kim <grufwub@gmail.com>

Co-authored-by: tobi <31960611+tsmethurst@users.noreply.github.com>
This commit is contained in:
kim 2022-07-19 09:47:55 +01:00 committed by GitHub
commit 098dbe6ff4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
141 changed files with 3046 additions and 997 deletions

View file

@ -25,11 +25,11 @@ import (
"strings"
"time"
"github.com/sirupsen/logrus"
"github.com/superseriousbusiness/gotosocial/internal/cache"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/log"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect"
)
@ -287,7 +287,7 @@ func (a *accountDB) GetAccountStatuses(ctx context.Context, accountID string, li
Where("? != '{}'", bun.Ident("attachments")).
Where("? != '[]'", bun.Ident("attachments"))
default:
logrus.Panic("db dialect was neither pg nor sqlite")
log.Panic("db dialect was neither pg nor sqlite")
return q
}
})
@ -383,7 +383,7 @@ func (a *accountDB) statusesFromIDs(ctx context.Context, statusIDs []string) ([]
// Fetch from status from database by ID
status, err := a.status.GetStatusByID(ctx, id)
if err != nil {
logrus.Errorf("statusesFromIDs: error getting status %q: %v", id, err)
log.Errorf("statusesFromIDs: error getting status %q: %v", id, err)
continue
}

View file

@ -29,13 +29,12 @@ import (
"strings"
"time"
"github.com/sirupsen/logrus"
"github.com/superseriousbusiness/gotosocial/internal/ap"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/id"
"github.com/superseriousbusiness/gotosocial/internal/log"
"github.com/superseriousbusiness/gotosocial/internal/uris"
"golang.org/x/crypto/bcrypt"
)
@ -87,7 +86,7 @@ func (a *adminDB) IsEmailAvailable(ctx context.Context, email string) (bool, db.
func (a *adminDB) NewSignup(ctx context.Context, username string, reason string, requireApproval bool, email string, password string, signUpIP net.IP, locale string, appID string, emailVerified bool, admin bool) (*gtsmodel.User, db.Error) {
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
logrus.Errorf("error creating new rsa key: %s", err)
log.Errorf("error creating new rsa key: %s", err)
return nil, err
}
@ -190,13 +189,13 @@ func (a *adminDB) CreateInstanceAccount(ctx context.Context) db.Error {
return err
}
if exists {
logrus.Infof("instance account %s already exists", username)
log.Infof("instance account %s already exists", username)
return nil
}
key, err := rsa.GenerateKey(rand.Reader, 2048)
if err != nil {
logrus.Errorf("error creating new rsa key: %s", err)
log.Errorf("error creating new rsa key: %s", err)
return err
}
@ -231,7 +230,7 @@ func (a *adminDB) CreateInstanceAccount(ctx context.Context) db.Error {
return a.conn.ProcessError(err)
}
logrus.Infof("instance account %s CREATED with id %s", username, acct.ID)
log.Infof("instance account %s CREATED with id %s", username, acct.ID)
return nil
}
@ -250,7 +249,7 @@ func (a *adminDB) CreateInstanceInstance(ctx context.Context) db.Error {
return err
}
if exists {
logrus.Infof("instance entry already exists")
log.Infof("instance entry already exists")
return nil
}
@ -275,6 +274,6 @@ func (a *adminDB) CreateInstanceInstance(ctx context.Context) db.Error {
return a.conn.ProcessError(err)
}
logrus.Infof("created instance instance %s with id %s", host, i.ID)
log.Infof("created instance instance %s with id %s", host, i.ID)
return nil
}

View file

@ -22,10 +22,9 @@ import (
"context"
"errors"
"github.com/sirupsen/logrus"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/log"
"github.com/uptrace/bun"
)
@ -165,6 +164,6 @@ func (b *basicDB) IsHealthy(ctx context.Context) db.Error {
}
func (b *basicDB) Stop(ctx context.Context) db.Error {
logrus.Info("closing db connection")
log.Info("closing db connection")
return b.conn.Close()
}

View file

@ -33,13 +33,13 @@ import (
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/stdlib"
"github.com/sirupsen/logrus"
"github.com/superseriousbusiness/gotosocial/internal/cache"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/db/bundb/migrations"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/id"
"github.com/superseriousbusiness/gotosocial/internal/log"
"github.com/uptrace/bun"
"github.com/uptrace/bun/dialect/pgdialect"
"github.com/uptrace/bun/dialect/sqlitedialect"
@ -89,8 +89,6 @@ type bunDBService struct {
}
func doMigration(ctx context.Context, db *bun.DB) error {
l := logrus.WithField("func", "doMigration")
migrator := migrate.NewMigrator(db, migrations.Migrations)
if err := migrator.Init(ctx); err != nil {
@ -106,11 +104,11 @@ func doMigration(ctx context.Context, db *bun.DB) error {
}
if group.ID == 0 {
l.Info("there are no new migrations to run")
log.Info("there are no new migrations to run")
return nil
}
l.Infof("MIGRATED DATABASE TO %s", group)
log.Infof("MIGRATED DATABASE TO %s", group)
return nil
}
@ -243,7 +241,7 @@ func sqliteConn(ctx context.Context) (*DBConn, error) {
tweakConnectionValues(sqldb)
if dbAddress == "file::memory:?cache=shared" {
logrus.Warn("sqlite in-memory database should only be used for debugging")
log.Warn("sqlite in-memory database should only be used for debugging")
// don't close connections on disconnect -- otherwise
// the SQLite database will be deleted when there
// are no active connections
@ -260,7 +258,7 @@ func sqliteConn(ctx context.Context) (*DBConn, error) {
return nil, fmt.Errorf("sqlite ping: %s", err)
}
logrus.Info("connected to SQLITE database")
log.Info("connected to SQLITE database")
return conn, nil
}
@ -281,7 +279,7 @@ func pgConn(ctx context.Context) (*DBConn, error) {
return nil, fmt.Errorf("postgres ping: %s", err)
}
logrus.Info("connected to POSTGRES database")
log.Info("connected to POSTGRES database")
return conn, nil
}
@ -436,7 +434,7 @@ func (ps *bunDBService) EmojiStringsToEmojis(ctx context.Context, emojis []strin
if err != nil {
if err == sql.ErrNoRows {
// no result found for this username/domain so just don't include it as an emoji and carry on about our business
logrus.Debugf("no emoji found with shortcode %s, skipping it", e)
log.Debugf("no emoji found with shortcode %s, skipping it", e)
continue
}
// a serious error has happened so bail

View file

@ -22,7 +22,9 @@ import (
"context"
"time"
"github.com/sirupsen/logrus"
"codeberg.org/gruf/go-kv"
"codeberg.org/gruf/go-logger/v2/level"
"github.com/superseriousbusiness/gotosocial/internal/log"
"github.com/uptrace/bun"
)
@ -38,21 +40,17 @@ func (queryHook) AfterQuery(_ context.Context, event *bun.QueryEvent) {
// Get the DB query duration
dur := time.Since(event.StartTime)
log := func(lvl logrus.Level, msg string) {
logrus.WithFields(logrus.Fields{
"duration": dur,
"operation": event.Operation(),
"query": event.Query,
}).Log(lvl, msg)
}
switch {
// Warn on slow database queries
case dur > time.Second:
log(logrus.WarnLevel, "SLOW DATABASE QUERY")
log.WithFields(kv.Fields{
{"duration", dur},
{"query", event.Query},
}...).Warn("SLOW DATABASE QUERY")
// On trace, we log query information
case logrus.GetLevel() == logrus.TraceLevel:
log(logrus.TraceLevel, "database query")
// On trace, we log query information,
// manually crafting so DB query not escaped.
case log.Level() >= level.TRACE:
log.Printf("level=TRACE duration=%s query=%s", dur, event.Query)
}
}

View file

@ -21,11 +21,10 @@ package bundb
import (
"context"
"github.com/sirupsen/logrus"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/log"
"github.com/uptrace/bun"
)
@ -118,7 +117,7 @@ func (i *instanceDB) GetInstancePeers(ctx context.Context, includeSuspended bool
}
func (i *instanceDB) GetInstanceAccounts(ctx context.Context, domain string, maxID string, limit int) ([]*gtsmodel.Account, db.Error) {
logrus.Debug("GetAccountsForInstance")
log.Debug("GetAccountsForInstance")
accounts := []*gtsmodel.Account{}

View file

@ -22,9 +22,9 @@ import (
"context"
"codeberg.org/gruf/go-cache/v2"
"github.com/sirupsen/logrus"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/log"
"github.com/uptrace/bun"
)
@ -72,7 +72,7 @@ func (m *mentionDB) GetMentions(ctx context.Context, ids []string) ([]*gtsmodel.
// Attempt fetch from DB
mention, err := m.GetMention(ctx, id)
if err != nil {
logrus.Errorf("GetMentions: error getting mention %q: %v", id, err)
log.Errorf("GetMentions: error getting mention %q: %v", id, err)
continue
}

View file

@ -26,14 +26,14 @@ import (
"codeberg.org/gruf/go-store/kv"
"codeberg.org/gruf/go-store/storage"
"github.com/sirupsen/logrus"
"github.com/superseriousbusiness/gotosocial/internal/config"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/log"
"github.com/uptrace/bun"
)
func init() {
deleteAttachment := func(ctx context.Context, l *logrus.Entry, a *gtsmodel.MediaAttachment, s *kv.KVStore, tx bun.Tx) {
deleteAttachment := func(ctx context.Context, l log.Entry, a *gtsmodel.MediaAttachment, s *kv.KVStore, tx bun.Tx) {
if err := s.Delete(a.File.Path); err != nil && err != storage.ErrNotFound {
l.Errorf("error removing file %s: %s", a.File.Path, err)
} else {
@ -57,7 +57,7 @@ func init() {
}
up := func(ctx context.Context, db *bun.DB) error {
l := logrus.WithField("migration", "20220612091800_duplicated_media_cleanup")
l := log.WithField("migration", "20220612091800_duplicated_media_cleanup")
if config.GetStorageBackend() != "local" {
// this migration only affects versions which only supported local storage

View file

@ -22,7 +22,5 @@ import (
"github.com/uptrace/bun/migrate"
)
var (
// Migrations provides migration logic for bun
Migrations = migrate.NewMigrations()
)
// Migrations provides migration logic for bun
var Migrations = migrate.NewMigrations()

View file

@ -22,9 +22,9 @@ import (
"context"
"codeberg.org/gruf/go-cache/v2"
"github.com/sirupsen/logrus"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/log"
)
type notificationDB struct {
@ -98,7 +98,7 @@ func (n *notificationDB) GetNotifications(ctx context.Context, accountID string,
// Attempt fetch from DB
notif, err := n.GetNotification(ctx, id)
if err != nil {
logrus.Errorf("GetNotifications: error getting notification %q: %v", id, err)
log.Errorf("GetNotifications: error getting notification %q: %v", id, err)
continue
}

View file

@ -24,10 +24,10 @@ import (
"database/sql"
"time"
"github.com/sirupsen/logrus"
"github.com/superseriousbusiness/gotosocial/internal/cache"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/log"
"github.com/uptrace/bun"
)
@ -208,7 +208,7 @@ func (s *statusDB) GetStatusChildren(ctx context.Context, status *gtsmodel.Statu
// only append children, not the overall parent status
entry, ok := e.Value.(*gtsmodel.Status)
if !ok {
logrus.Panic("GetStatusChildren: found status could not be asserted to *gtsmodel.Status")
log.Panic("GetStatusChildren: found status could not be asserted to *gtsmodel.Status")
}
if entry.ID != status.ID {
@ -233,7 +233,7 @@ func (s *statusDB) statusChildren(ctx context.Context, status *gtsmodel.Status,
if err := q.Scan(ctx, &childIDs); err != nil {
if err != sql.ErrNoRows {
logrus.Errorf("statusChildren: error getting children for %q: %v", status.ID, err)
log.Errorf("statusChildren: error getting children for %q: %v", status.ID, err)
}
return
}
@ -242,7 +242,7 @@ func (s *statusDB) statusChildren(ctx context.Context, status *gtsmodel.Status,
// Fetch child with ID from database
child, err := s.GetStatusByID(ctx, id)
if err != nil {
logrus.Errorf("statusChildren: error getting child status %q: %v", id, err)
log.Errorf("statusChildren: error getting child status %q: %v", id, err)
continue
}
@ -250,7 +250,7 @@ func (s *statusDB) statusChildren(ctx context.Context, status *gtsmodel.Status,
for e := foundStatuses.Front(); e != nil; e = e.Next() {
entry, ok := e.Value.(*gtsmodel.Status)
if !ok {
logrus.Panic("statusChildren: found status could not be asserted to *gtsmodel.Status")
log.Panic("statusChildren: found status could not be asserted to *gtsmodel.Status")
}
if child.InReplyToAccountID != "" && entry.ID == child.InReplyToID {

View file

@ -21,9 +21,9 @@ package bundb
import (
"context"
"github.com/sirupsen/logrus"
"github.com/superseriousbusiness/gotosocial/internal/db"
"github.com/superseriousbusiness/gotosocial/internal/gtsmodel"
"github.com/superseriousbusiness/gotosocial/internal/log"
"github.com/uptrace/bun"
"golang.org/x/exp/slices"
)
@ -96,7 +96,7 @@ func (t *timelineDB) GetHomeTimeline(ctx context.Context, accountID string, maxI
// Fetch status from db for ID
status, err := t.status.GetStatusByID(ctx, id)
if err != nil {
logrus.Errorf("GetHomeTimeline: error fetching status %q: %v", id, err)
log.Errorf("GetHomeTimeline: error fetching status %q: %v", id, err)
continue
}
@ -156,7 +156,7 @@ func (t *timelineDB) GetPublicTimeline(ctx context.Context, accountID string, ma
// Fetch status from db for ID
status, err := t.status.GetStatusByID(ctx, id)
if err != nil {
logrus.Errorf("GetPublicTimeline: error fetching status %q: %v", id, err)
log.Errorf("GetPublicTimeline: error fetching status %q: %v", id, err)
continue
}
@ -216,7 +216,7 @@ func (t *timelineDB) GetFavedTimeline(ctx context.Context, accountID string, max
// Fetch status from db for corresponding favourite
status, err := t.status.GetStatusByID(ctx, fave.StatusID)
if err != nil {
logrus.Errorf("GetFavedTimeline: error fetching status for fave %q: %v", fave.ID, err)
log.Errorf("GetFavedTimeline: error fetching status for fave %q: %v", fave.ID, err)
continue
}