Skip to content

Commit

Permalink
Unify the pattern of using a package-level logger (#8245)
Browse files Browse the repository at this point in the history
Co-authored-by: Preston Van Loon <preston@prysmaticlabs.com>
  • Loading branch information
rkapka and prestonvanloon committed Jan 11, 2021
1 parent 18bb867 commit 5fd03f8
Show file tree
Hide file tree
Showing 127 changed files with 281 additions and 146 deletions.
1 change: 1 addition & 0 deletions beacon-chain/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ load("@io_bazel_rules_docker//contrib:push-all.bzl", "docker_push")
go_library(
name = "go_default_library",
srcs = [
"log.go",
"main.go",
"usage.go",
],
Expand Down
1 change: 1 addition & 0 deletions beacon-chain/cache/depositcache/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go_library(
name = "go_default_library",
srcs = [
"deposits_cache.go",
"log.go",
"pending_deposits.go",
],
importpath = "github.com/prysmaticlabs/prysm/beacon-chain/cache/depositcache",
Expand Down
4 changes: 2 additions & 2 deletions beacon-chain/cache/depositcache/deposits_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/prysmaticlabs/prysm/shared/bytesutil"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/trieutil"
log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
"go.opencensus.io/trace"
)

Expand Down Expand Up @@ -78,7 +78,7 @@ func (dc *DepositCache) InsertDeposit(ctx context.Context, d *ethpb.Deposit, blo
ctx, span := trace.StartSpan(ctx, "DepositsCache.InsertDeposit")
defer span.End()
if d == nil {
log.WithFields(log.Fields{
log.WithFields(logrus.Fields{
"block": blockNum,
"deposit": d,
"index": index,
Expand Down
5 changes: 5 additions & 0 deletions beacon-chain/cache/depositcache/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package depositcache

import "github.com/sirupsen/logrus"

var log = logrus.WithField("prefix", "depositcache")
4 changes: 2 additions & 2 deletions beacon-chain/cache/depositcache/pending_deposits.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import (
ethpb "github.com/prysmaticlabs/ethereumapis/eth/v1alpha1"
dbpb "github.com/prysmaticlabs/prysm/proto/beacon/db"
"github.com/prysmaticlabs/prysm/shared/hashutil"
log "github.com/sirupsen/logrus"
"github.com/sirupsen/logrus"
"go.opencensus.io/trace"
)

Expand All @@ -33,7 +33,7 @@ func (dc *DepositCache) InsertPendingDeposit(ctx context.Context, d *ethpb.Depos
ctx, span := trace.StartSpan(ctx, "DepositsCache.InsertPendingDeposit")
defer span.End()
if d == nil {
log.WithFields(log.Fields{
log.WithFields(logrus.Fields{
"block": blockNum,
"deposit": d,
}).Debug("Ignoring nil deposit insertion")
Expand Down
1 change: 1 addition & 0 deletions beacon-chain/core/state/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ load("@io_bazel_rules_go//go:def.bzl", "go_test")
go_library(
name = "go_default_library",
srcs = [
"log.go",
"skip_slot_cache.go",
"state.go",
"transition.go",
Expand Down
5 changes: 5 additions & 0 deletions beacon-chain/core/state/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package state

import "github.com/sirupsen/logrus"

var log = logrus.WithField("prefix", "state")
7 changes: 3 additions & 4 deletions beacon-chain/core/state/transition.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"github.com/prysmaticlabs/prysm/shared/mathutil"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/prysmaticlabs/prysm/shared/traceutil"
"github.com/sirupsen/logrus"
"go.opencensus.io/trace"
)

Expand Down Expand Up @@ -312,7 +311,7 @@ func ProcessSlots(ctx context.Context, state *stateTrie.BeaconState, slot uint64
defer func() {
if err := SkipSlotCache.MarkNotInProgress(key); err != nil {
traceutil.AnnotateError(span, err)
logrus.WithError(err).Error("Failed to mark skip slot no longer in progress")
log.WithError(err).Error("Failed to mark skip slot no longer in progress")
}
}()

Expand All @@ -322,7 +321,7 @@ func ProcessSlots(ctx context.Context, state *stateTrie.BeaconState, slot uint64
// Cache last best value.
if highestSlot < state.Slot() {
if err := SkipSlotCache.Put(ctx, key, state); err != nil {
logrus.WithError(err).Error("Failed to put skip slot cache value")
log.WithError(err).Error("Failed to put skip slot cache value")
}
}
return nil, ctx.Err()
Expand All @@ -347,7 +346,7 @@ func ProcessSlots(ctx context.Context, state *stateTrie.BeaconState, slot uint64

if highestSlot < state.Slot() {
if err := SkipSlotCache.Put(ctx, key, state); err != nil {
logrus.WithError(err).Error("Failed to put skip slot cache value")
log.WithError(err).Error("Failed to put skip slot cache value")
traceutil.AnnotateError(span, err)
}
}
Expand Down
1 change: 1 addition & 0 deletions beacon-chain/db/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ go_library(
srcs = [
"alias.go",
"cmd.go",
"log.go",
"restore.go",
] + select({
":kafka_disabled": [
Expand Down
3 changes: 1 addition & 2 deletions beacon-chain/db/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package db
import (
"github.com/prysmaticlabs/prysm/shared/cmd"
"github.com/prysmaticlabs/prysm/shared/tos"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)

Expand All @@ -23,7 +22,7 @@ var DatabaseCommands = &cli.Command{
Before: tos.VerifyTosAcceptedOrPrompt,
Action: func(cliCtx *cli.Context) error {
if err := restore(cliCtx); err != nil {
logrus.Fatalf("Could not restore database: %v", err)
log.Fatalf("Could not restore database: %v", err)
}
return nil
},
Expand Down
1 change: 1 addition & 0 deletions beacon-chain/db/kafka/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ go_library(
name = "go_default_library",
srcs = [
"export_wrapper.go",
"log.go",
"passthrough.go",
],
importpath = "github.com/prysmaticlabs/prysm/beacon-chain/db/kafka",
Expand Down
2 changes: 0 additions & 2 deletions beacon-chain/db/kafka/export_wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,12 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/db/iface"
"github.com/prysmaticlabs/prysm/shared/featureconfig"
"github.com/prysmaticlabs/prysm/shared/traceutil"
"github.com/sirupsen/logrus"
"go.opencensus.io/trace"
"gopkg.in/confluentinc/confluent-kafka-go.v1/kafka"
_ "gopkg.in/confluentinc/confluent-kafka-go.v1/kafka/librdkafka" // Required for c++ kafka library.
)

var _ iface.Database = (*Exporter)(nil)
var log = logrus.WithField("prefix", "exporter")
var marshaler = &jsonpb.Marshaler{}

// Exporter wraps a database interface and exports certain objects to kafka topics.
Expand Down
5 changes: 5 additions & 0 deletions beacon-chain/db/kafka/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package kafka

import "github.com/sirupsen/logrus"

var log = logrus.WithField("prefix", "exporter")
1 change: 1 addition & 0 deletions beacon-chain/db/kv/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ go_library(
"encoding.go",
"finalized_block_roots.go",
"kv.go",
"log.go",
"migration.go",
"migration_archived_index.go",
"migration_block_slot_index.go",
Expand Down
7 changes: 3 additions & 4 deletions beacon-chain/db/kv/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import (
"github.com/pkg/errors"
"github.com/prysmaticlabs/prysm/shared/fileutil"
"github.com/prysmaticlabs/prysm/shared/params"
"github.com/sirupsen/logrus"
bolt "go.etcd.io/bbolt"
"go.opencensus.io/trace"
)
Expand Down Expand Up @@ -43,7 +42,7 @@ func (s *Store) Backup(ctx context.Context, outputDir string) error {
return err
}
backupPath := path.Join(backupsDir, fmt.Sprintf("prysm_beacondb_at_slot_%07d.backup", head.Block.Slot))
logrus.WithField("prefix", "db").WithField("backup", backupPath).Info("Writing backup database.")
log.WithField("backup", backupPath).Info("Writing backup database.")

copyDB, err := bolt.Open(
backupPath,
Expand All @@ -55,13 +54,13 @@ func (s *Store) Backup(ctx context.Context, outputDir string) error {
}
defer func() {
if err := copyDB.Close(); err != nil {
logrus.WithError(err).Error("Failed to close backup database")
log.WithError(err).Error("Failed to close backup database")
}
}()

return s.db.View(func(tx *bolt.Tx) error {
return tx.ForEach(func(name []byte, b *bolt.Bucket) error {
logrus.Debugf("Copying bucket %s\n", name)
log.Debugf("Copying bucket %s\n", name)
return copyDB.Update(func(tx2 *bolt.Tx) error {
b2, err := tx2.CreateBucketIfNotExists(name)
if err != nil {
Expand Down
5 changes: 5 additions & 0 deletions beacon-chain/db/kv/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package kv

import "github.com/sirupsen/logrus"

var log = logrus.WithField("prefix", "db")
1 change: 0 additions & 1 deletion beacon-chain/db/kv/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/prysmaticlabs/prysm/beacon-chain/state"
pb "github.com/prysmaticlabs/prysm/proto/beacon/p2p/v1"
"github.com/prysmaticlabs/prysm/shared/bytesutil"
log "github.com/sirupsen/logrus"
bolt "go.etcd.io/bbolt"
"go.opencensus.io/trace"
)
Expand Down
5 changes: 5 additions & 0 deletions beacon-chain/db/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package db

import "github.com/sirupsen/logrus"

var log = logrus.WithField("prefix", "db")
5 changes: 2 additions & 3 deletions beacon-chain/db/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"github.com/prysmaticlabs/prysm/shared/cmd"
"github.com/prysmaticlabs/prysm/shared/fileutil"
"github.com/prysmaticlabs/prysm/shared/promptutil"
"github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)

Expand All @@ -30,7 +29,7 @@ func restore(cliCtx *cli.Context) error {
return errors.Wrap(err, "could not validate choice")
}
if strings.EqualFold(resp, "n") {
logrus.Info("Restore aborted")
log.Info("Restore aborted")
return nil
}
}
Expand All @@ -41,6 +40,6 @@ func restore(cliCtx *cli.Context) error {
return err
}

logrus.Info("Restore completed successfully")
log.Info("Restore completed successfully")
return nil
}
1 change: 1 addition & 0 deletions beacon-chain/flags/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ go_library(
"base.go",
"config.go",
"interop.go",
"log.go",
],
importpath = "github.com/prysmaticlabs/prysm/beacon-chain/flags",
visibility = ["//beacon-chain:__subpackages__"],
Expand Down
1 change: 0 additions & 1 deletion beacon-chain/flags/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package flags

import (
"github.com/prysmaticlabs/prysm/shared/cmd"
log "github.com/sirupsen/logrus"
"github.com/urfave/cli/v2"
)

Expand Down
5 changes: 5 additions & 0 deletions beacon-chain/flags/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package flags

import "github.com/sirupsen/logrus"

var log = logrus.WithField("prefix", "flags")
5 changes: 4 additions & 1 deletion beacon-chain/gateway/server/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@ load("@io_bazel_rules_docker//contrib:push-all.bzl", "docker_push")

go_library(
name = "go_default_library",
srcs = ["main.go"],
srcs = [
"log.go",
"main.go",
],
importpath = "github.com/prysmaticlabs/prysm/beacon-chain/gateway/server",
visibility = ["//visibility:private"],
deps = [
Expand Down
5 changes: 5 additions & 0 deletions beacon-chain/gateway/server/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

import "github.com/sirupsen/logrus"

var log = logrus.New()
2 changes: 0 additions & 2 deletions beacon-chain/gateway/server/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ func init() {
logrus.SetFormatter(joonix.NewFormatter())
}

var log = logrus.New()

func main() {
flag.Parse()
if *debug {
Expand Down
5 changes: 5 additions & 0 deletions beacon-chain/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package main

import "github.com/sirupsen/logrus"

var log = logrus.WithField("prefix", "main")
1 change: 0 additions & 1 deletion beacon-chain/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,6 @@ func init() {
}

func main() {
log := logrus.WithField("prefix", "main")
app := cli.App{}
app.Name = "beacon-chain"
app.Usage = "this is a beacon chain implementation for Ethereum 2.0"
Expand Down
1 change: 1 addition & 0 deletions beacon-chain/node/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ go_library(
name = "go_default_library",
srcs = [
"helper.go",
"log.go",
"node.go",
],
importpath = "github.com/prysmaticlabs/prysm/beacon-chain/node",
Expand Down
5 changes: 5 additions & 0 deletions beacon-chain/node/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package node

import "github.com/sirupsen/logrus"

var log = logrus.WithField("prefix", "node")
2 changes: 0 additions & 2 deletions beacon-chain/node/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,6 @@ import (
"gopkg.in/yaml.v2"
)

var log = logrus.WithField("prefix", "node")

const testSkipPowFlag = "test-skip-pow"

// BeaconNode defines a struct that handles the services running a random beacon chain
Expand Down
3 changes: 1 addition & 2 deletions beacon-chain/p2p/info.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"github.com/libp2p/go-libp2p-core/host"
"github.com/libp2p/go-libp2p-core/peer"
ma "github.com/multiformats/go-multiaddr"
"github.com/sirupsen/logrus"
)

// InfoHandler is a handler to serve /p2p page in metrics.
Expand All @@ -26,7 +25,7 @@ self=%s
len(s.host.Network().Peers()),
formatPeers(s.host), // Must be last. Writes one entry per row.
); err != nil {
logrus.WithError(err).Error("Failed to render p2p info page")
log.WithError(err).Error("Failed to render p2p info page")
return
}

Expand Down
1 change: 1 addition & 0 deletions beacon-chain/powchain/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ go_library(
"block_cache.go",
"block_reader.go",
"deposit.go",
"log.go",
"log_processing.go",
"service.go",
],
Expand Down
5 changes: 5 additions & 0 deletions beacon-chain/powchain/log.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package powchain

import "github.com/sirupsen/logrus"

var log = logrus.WithField("prefix", "powchain")

0 comments on commit 5fd03f8

Please sign in to comment.