Skip to content

Commit

Permalink
fix: do not take logger from context
Browse files Browse the repository at this point in the history
  • Loading branch information
Vilsol committed Jul 7, 2022
1 parent 6e59980 commit 9257513
Show file tree
Hide file tree
Showing 24 changed files with 71 additions and 74 deletions.
10 changes: 5 additions & 5 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func Serve() {

if !viper.GetBool("production") {
go func() {
log.Ctx(ctx).Err(http.ListenAndServe("0.0.0.0:6060", nil)).Msg("Debug server")
log.Err(http.ListenAndServe("0.0.0.0:6060", nil)).Msg("Debug server")
}()
}

Expand Down Expand Up @@ -236,7 +236,7 @@ func Serve() {
bytesIn = "0"
}

log.Ctx(ctx).Info().
log.Info().
Str("time_rfc3339", time.Now().Format(time.RFC3339)).
Str("remote_ip", c.RealIP()).
Str("host", req.Host).
Expand Down Expand Up @@ -266,7 +266,7 @@ func Serve() {
}()

address := fmt.Sprintf(":%d", viper.GetInt("port"))
log.Ctx(ctx).Info().Str("address", address).Msg("starting server")
log.Info().Str("address", address).Msg("starting server")

e.HidePort = true
e.Logger.Error(e.Start(address))
Expand All @@ -276,7 +276,7 @@ func installExportPipeline(ctx context.Context) func() {
client := otlptracehttp.NewClient()
exporter, err := otlptrace.New(ctx, client)
if err != nil {
log.Ctx(ctx).Fatal().Err(err).Msg("creating OTLP trace exporter")
log.Fatal().Err(err).Msg("creating OTLP trace exporter")
}

tracerProvider := sdktrace.NewTracerProvider(
Expand All @@ -288,7 +288,7 @@ func installExportPipeline(ctx context.Context) func() {

return func() {
if err := tracerProvider.Shutdown(ctx); err != nil {
log.Ctx(ctx).Fatal().Err(err).Msg("stopping tracer provider")
log.Fatal().Err(err).Msg("stopping tracer provider")
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ func InitializeConfig() context.Context {
ctx := log.Logger.WithContext(context.Background())

if err != nil {
log.Ctx(ctx).Warn().Err(err).Msg("config initialized using defaults and environment only!")
log.Warn().Err(err).Msg("config initialized using defaults and environment only!")
}

log.Ctx(ctx).Info().Msg("Config initialized")
log.Info().Msg("Config initialized")

return ctx
}
Expand Down
16 changes: 7 additions & 9 deletions db/postgres/postgres.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,15 @@ func (l *GormLogger) LogMode(mode logger.LogLevel) logger.Interface {
}

func (*GormLogger) Info(ctx context.Context, msg string, data ...interface{}) {
log.Ctx(ctx).Info().Str("file", utils.FileWithLineNum()).Msgf(msg, data...)
log.Info().Str("file", utils.FileWithLineNum()).Msgf(msg, data...)
}

func (*GormLogger) Warn(ctx context.Context, msg string, data ...interface{}) {
log.Ctx(ctx).Warn().Str("file", utils.FileWithLineNum()).Msgf(msg, data...)
log.Warn().Str("file", utils.FileWithLineNum()).Msgf(msg, data...)
}

func (*GormLogger) Error(ctx context.Context, msg string, data ...interface{}) {
log.Ctx(ctx).Error().Str("file", utils.FileWithLineNum()).Msgf(msg, data...)
log.Error().Str("file", utils.FileWithLineNum()).Msgf(msg, data...)
}

func (l *GormLogger) Trace(ctx context.Context, begin time.Time, fc func() (string, int64), err error) {
Expand All @@ -52,16 +52,14 @@ func (l *GormLogger) Trace(ctx context.Context, begin time.Time, fc func() (stri

sql, rows := fc()

logCtx := log.Ctx(ctx)

var logEv *zerolog.Event
switch {
case err != nil:
logEv = logCtx.Err(err)
logEv = log.Err(err)
case since > l.SlowThreshold && l.SlowThreshold != 0:
logEv = logCtx.Warn()
logEv = log.Warn()
case l.Debug:
logEv = logCtx.Info()
logEv = log.Info()
}

if logEv != nil {
Expand Down Expand Up @@ -102,7 +100,7 @@ func InitializePostgres(ctx context.Context) {

// TODO Create search indexes

log.Ctx(ctx).Info().Msg("Postgres initialized")
log.Info().Msg("Postgres initialized")
}

func Save(ctx context.Context, object interface{}) {
Expand Down
4 changes: 2 additions & 2 deletions db/statistics.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ func RunAsyncStatisticLoop(ctx context.Context) {
for {
start := time.Now()
keys := redis.GetAllKeys()
log.Ctx(ctx).Info().Msgf("Fetched: %d keys in %s", len(keys), time.Since(start).String())
log.Info().Msgf("Fetched: %d keys in %s", len(keys), time.Since(start).String())
resultMap := make(map[string]map[string]map[string]uint)
for _, key := range keys {
if matches := keyRegex.FindStringSubmatch(key); matches != nil {
Expand Down Expand Up @@ -104,7 +104,7 @@ func RunAsyncStatisticLoop(ctx context.Context) {
updateTx.Commit()
}

log.Ctx(ctx).Info().Msgf("Statistics Updated! Took %s", time.Since(start).String())
log.Info().Msgf("Statistics Updated! Took %s", time.Since(start).String())
time.Sleep(time.Minute)
}
}()
Expand Down
14 changes: 7 additions & 7 deletions gql/resolver_versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,33 +100,33 @@ func (r *mutationResolver) FinalizeCreateVersion(ctx context.Context, modID stri
return false, errors.New("you must update your mod reference on the site to match your mod_reference in your data.json")
}

log.Ctx(ctx).Info().Str("mod_id", mod.ID).Str("version_id", versionID).Msg("finalization gql call")
log.Info().Str("mod_id", mod.ID).Str("version_id", versionID).Msg("finalization gql call")

go func(ctx context.Context, mod *postgres.Mod, versionID string, version generated.NewVersion) {
defer func() {
if r := recover(); r != nil {
log.Ctx(ctx).Error().Interface("recover", r).Str("stack", string(debug.Stack())).Msgf("recovered from version finalization")
log.Error().Interface("recover", r).Str("stack", string(debug.Stack())).Msgf("recovered from version finalization")

if err := redis.StoreVersionUploadState(versionID, nil, errors.New("internal error, please try again, if it fails again, please report on discord")); err != nil {
log.Error().Err(err).Msg("failed to store version upload state")
}
}
}()

log.Ctx(ctx).Info().Str("mod_id", mod.ID).Str("version_id", versionID).Msg("calling FinalizeVersionUploadAsync")
log.Info().Str("mod_id", mod.ID).Str("version_id", versionID).Msg("calling FinalizeVersionUploadAsync")

data, err := FinalizeVersionUploadAsync(ctx, mod, versionID, version)
if err2 := redis.StoreVersionUploadState(versionID, data, err); err2 != nil {
log.Ctx(ctx).Err(err2).Msg("error storing redis state")
log.Err(err2).Msg("error storing redis state")
return
}

log.Ctx(ctx).Info().Str("mod_id", mod.ID).Str("version_id", versionID).Msg("finished FinalizeVersionUploadAsync")
log.Info().Str("mod_id", mod.ID).Str("version_id", versionID).Msg("finished FinalizeVersionUploadAsync")

if err != nil {
log.Ctx(ctx).Err(err).Msgf("error completing version upload [%s]", versionID)
log.Err(err).Msgf("error completing version upload [%s]", versionID)
} else {
log.Ctx(ctx).Info().Msgf("completed version upload: %s", versionID)
log.Info().Msgf("completed version upload: %s", versionID)
}
}(util.ReWrapCtx(ctx), mod, versionID, version)

Expand Down
2 changes: 1 addition & 1 deletion gql/versions.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func FinalizeVersionUploadAsync(ctx context.Context, mod *postgres.Mod, versionI

jsonData, err := json.Marshal(modInfo.Metadata)
if err != nil {
log.Ctx(ctx).Err(err).Msgf("[%s] failed serializing", dbVersion.ID)
log.Err(err).Msgf("[%s] failed serializing", dbVersion.ID)
} else {
metadata := string(jsonData)
dbVersion.Metadata = &metadata
Expand Down
6 changes: 3 additions & 3 deletions integrations/discord.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func NewMod(ctx context.Context, mod *postgres.Mod) {
payloadJSON, err := json.Marshal(payload)

if err != nil {
log.Ctx(ctx).Err(err).Msg("error marshaling discord webhook")
log.Err(err).Msg("error marshaling discord webhook")
return
}

Expand All @@ -80,7 +80,7 @@ func NewMod(ctx context.Context, mod *postgres.Mod) {
}

func NewVersion(ctx context.Context, version *postgres.Version) {
log.Ctx(ctx).Info().Str("stack", string(debug.Stack())).Msg("new version discord webhook")
log.Info().Str("stack", string(debug.Stack())).Msg("new version discord webhook")

if version == nil {
return
Expand Down Expand Up @@ -146,7 +146,7 @@ func NewVersion(ctx context.Context, version *postgres.Version) {
payloadJSON, err := json.Marshal(payload)

if err != nil {
log.Ctx(ctx).Err(err).Msg("error marshaling discord webhook")
log.Err(err).Msg("error marshaling discord webhook")
return
}

Expand Down
5 changes: 2 additions & 3 deletions migrations/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ package migrations
import (
"context"
"os"
"strings"

postgres2 "github.com/satisfactorymodding/smr-api/db/postgres"

"strings"

// Import all migrations
_ "github.com/satisfactorymodding/smr-api/migrations/code"

Expand All @@ -32,7 +31,7 @@ func codeMigrations(ctx context.Context) {
source := migration.DefaultCodeSource()

// TODO Custom reporter, this one's very ugly
reporter := migration.NewDefaultReporterWithParams(codeMigrationLogger{log: log.Ctx(ctx)}, os.Exit)
reporter := migration.NewDefaultReporterWithParams(codeMigrationLogger{log: &log.Logger}, os.Exit)

db, _ := postgres2.DBCtx(ctx).DB()
manager := migration.NewDefaultManager(migration.NewPostgreSQLTarget(db), source)
Expand Down
2 changes: 1 addition & 1 deletion migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
func RunMigrations(ctx context.Context) {
databaseMigrations(ctx)
codeMigrations(ctx)
log.Ctx(ctx).Info().Msg("Migrations Complete")
log.Info().Msg("Migrations Complete")
}

func databaseMigrations(ctx context.Context) {
Expand Down
6 changes: 3 additions & 3 deletions redis/jobs/consumers/consumer_scan_mod_on_virus_total.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func ScanModOnVirusTotalConsumer(ctx context.Context, payload []byte) error {
return errors.Wrap(err, "failed to unmarshal task data")
}

log.Ctx(ctx).Info().Msgf("starting virus scan of mod %s version %s", task.ModID, task.VersionID)
log.Info().Msgf("starting virus scan of mod %s version %s", task.ModID, task.VersionID)

version := postgres.GetVersion(ctx, task.VersionID)
link := storage.GenerateDownloadLink(version.Key)
Expand Down Expand Up @@ -78,12 +78,12 @@ func ScanModOnVirusTotalConsumer(ctx context.Context, payload []byte) error {
}

if !success {
log.Ctx(ctx).Warn().Msgf("mod %s version %s failed to pass virus scan", task.ModID, task.VersionID)
log.Warn().Msgf("mod %s version %s failed to pass virus scan", task.ModID, task.VersionID)
return nil
}

if task.ApproveAfter {
log.Ctx(ctx).Info().Msgf("approving mod %s version %s after successful virus scan", task.ModID, task.VersionID)
log.Info().Msgf("approving mod %s version %s after successful virus scan", task.ModID, task.VersionID)
version.Approved = true
postgres.Save(ctx, &version)

Expand Down
6 changes: 3 additions & 3 deletions redis/jobs/consumers/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import (

func UpdateModDataFromStorage(ctx context.Context, modID string, versionID string, metadata bool) error {
// perform task
log.Ctx(ctx).Info().Msgf("[%s] Updating DB for mod %s version with metadata: %v", versionID, modID, metadata)
log.Info().Msgf("[%s] Updating DB for mod %s version with metadata: %v", versionID, modID, metadata)

version := postgres.GetVersion(ctx, versionID)
link := storage.GenerateDownloadLink(version.Key)
Expand All @@ -39,7 +39,7 @@ func UpdateModDataFromStorage(ctx context.Context, modID string, versionID strin
info, err := validation.ExtractModInfo(ctx, fileData, metadata, false, mod.ModReference)

if err != nil {
log.Ctx(ctx).Warn().Err(err).Msgf("[%s] Failed updating mod, likely outdated", versionID)
log.Warn().Err(err).Msgf("[%s] Failed updating mod, likely outdated", versionID)
// Outdated version
return nil
}
Expand Down Expand Up @@ -69,7 +69,7 @@ func UpdateModDataFromStorage(ctx context.Context, modID string, versionID strin
if metadata {
jsonData, err := json.Marshal(info.Metadata)
if err != nil {
log.Ctx(ctx).Err(err).Msgf("[%s] failed serializing", versionID)
log.Err(err).Msgf("[%s] failed serializing", versionID)
} else {
metadata := string(jsonData)
version.Metadata = &metadata
Expand Down
10 changes: 5 additions & 5 deletions redis/jobs/jobs.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func SubmitJobUpdateDBFromModVersionFileTask(ctx context.Context, modID string,
err := queue.Add(tasks.UpdateDBFromModVersionFileTask.WithArgs(ctx, task))

if err != nil {
log.Ctx(ctx).Err(err).Msg("error adding task")
log.Err(err).Msg("error adding task")
}
}

Expand All @@ -68,7 +68,7 @@ func SubmitJobUpdateDBFromModVersionJSONFileTask(ctx context.Context, modID stri
err := queue.Add(tasks.UpdateDBFromModVersionJSONFileTask.WithArgs(ctx, task))

if err != nil {
log.Ctx(ctx).Err(err).Msg("error adding task")
log.Err(err).Msg("error adding task")
}
}

Expand All @@ -80,7 +80,7 @@ func SubmitJobCopyObjectFromOldBucketTask(ctx context.Context, key string) {
err := queue.Add(tasks.CopyObjectFromOldBucketTask.WithArgs(ctx, task))

if err != nil {
log.Ctx(ctx).Err(err).Msg("error adding task")
log.Err(err).Msg("error adding task")
}
}

Expand All @@ -92,7 +92,7 @@ func SubmitJobCopyObjectToOldBucketTask(ctx context.Context, key string) {
err := queue.Add(tasks.CopyObjectToOldBucketTask.WithArgs(ctx, task))

if err != nil {
log.Ctx(ctx).Err(err).Msg("error adding task")
log.Err(err).Msg("error adding task")
}
}

Expand All @@ -106,6 +106,6 @@ func SubmitJobScanModOnVirusTotalTask(ctx context.Context, modID string, version
err := queue.Add(tasks.ScanModOnVirusTotalTask.WithArgs(ctx, task))

if err != nil {
log.Ctx(ctx).Err(err).Msg("error adding task")
log.Err(err).Msg("error adding task")
}
}
2 changes: 1 addition & 1 deletion redis/redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func InitializeRedis(ctx context.Context) {
panic(ping.Err())
}

log.Ctx(ctx).Info().Msg("Redis initialized")
log.Info().Msg("Redis initialized")
}

func CanIncrement(ip string, action string, object string, expiration time.Duration) bool {
Expand Down
2 changes: 1 addition & 1 deletion storage/b2.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func initializeB2(ctx context.Context, config Config) *B2 {
newSession, err := session.NewSession(s3Config)

if err != nil {
log.Ctx(ctx).Err(err).Msg("failed to create S3 session")
log.Err(err).Msg("failed to create S3 session")
return nil
}

Expand Down
2 changes: 1 addition & 1 deletion storage/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func initializeS3(ctx context.Context, config Config) *S3 {
newSession, err := session.NewSession(s3Config)

if err != nil {
log.Ctx(ctx).Err(err).Msg("failed to create S3 session")
log.Err(err).Msg("failed to create S3 session")
return nil
}

Expand Down
Loading

0 comments on commit 9257513

Please sign in to comment.