Skip to content
This repository has been archived by the owner on Oct 6, 2023. It is now read-only.

Commit

Permalink
Logging with logrus (#232)
Browse files Browse the repository at this point in the history
* Use logrus

* Configure logger

* Lint
  • Loading branch information
dmaretskyi authored Jul 1, 2021
1 parent 51668ae commit 9ffecf6
Show file tree
Hide file tree
Showing 23 changed files with 68 additions and 35 deletions.
8 changes: 0 additions & 8 deletions .golangci.yaml
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
linters-settings:
depguard:
list-type: blacklist
packages:
# logging is allowed only by logutils.Log, logrus
# is allowed to use only in logutils package
- github.com/sirupsen/logrus
packages-with-error-message:
- github.com/sirupsen/logrus: "logging is allowed only by logutils.Log"
funlen:
lines: 100
statements: 50
Expand Down
3 changes: 2 additions & 1 deletion api/middleware/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ import (
"bytes"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"time"

log "github.com/sirupsen/logrus"
)

type payload struct {
Expand Down
3 changes: 1 addition & 2 deletions bls/public_key.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package bls

import (
"log"

"github.com/Worldcoin/hubble-commander/models"
"github.com/kilic/bn254/bls"
log "github.com/sirupsen/logrus"
)

func fromBLSPublicKey(blsPK *bls.PublicKey) *models.PublicKey {
Expand Down
3 changes: 1 addition & 2 deletions commander/batches.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
package commander

import (
"log"

"github.com/Worldcoin/hubble-commander/commander/executor"
"github.com/Worldcoin/hubble-commander/eth"
"github.com/Worldcoin/hubble-commander/models"
st "github.com/Worldcoin/hubble-commander/storage"
"github.com/ethereum/go-ethereum/accounts/abi/bind"
log "github.com/sirupsen/logrus"
)

func (c *Commander) syncBatches(startBlock, endBlock uint64) error {
Expand Down
2 changes: 1 addition & 1 deletion commander/commander.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package commander

import (
"log"
"net/http"
"sync"

Expand All @@ -17,6 +16,7 @@ import (
"github.com/Worldcoin/hubble-commander/models/dto"
st "github.com/Worldcoin/hubble-commander/storage"
"github.com/Worldcoin/hubble-commander/testutils/simulator"
log "github.com/sirupsen/logrus"
"github.com/ybbus/jsonrpc/v2"
)

Expand Down
2 changes: 1 addition & 1 deletion commander/executor/create_batch.go
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package executor

import (
"log"
"time"

"github.com/Worldcoin/hubble-commander/bls"
"github.com/Worldcoin/hubble-commander/models"
"github.com/Worldcoin/hubble-commander/models/enums/txtype"
st "github.com/Worldcoin/hubble-commander/storage"
log "github.com/sirupsen/logrus"
)

func (t *TransactionExecutor) CreateAndSubmitBatch(batchType txtype.TransactionType, domain *bls.Domain) (err error) {
Expand Down
2 changes: 1 addition & 1 deletion commander/executor/create_create2transfer_commitments.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package executor

import (
"log"
"time"

"github.com/Worldcoin/hubble-commander/bls"
"github.com/Worldcoin/hubble-commander/models"
"github.com/Worldcoin/hubble-commander/models/enums/txtype"
log "github.com/sirupsen/logrus"
)

var (
Expand Down
2 changes: 1 addition & 1 deletion commander/executor/create_transfer_commitments.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package executor

import (
"log"
"time"

"github.com/Worldcoin/hubble-commander/bls"
"github.com/Worldcoin/hubble-commander/models"
"github.com/Worldcoin/hubble-commander/models/enums/txtype"
log "github.com/sirupsen/logrus"
)

var (
Expand Down
3 changes: 1 addition & 2 deletions commander/executor/sync.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package executor

import (
"log"

"github.com/Worldcoin/hubble-commander/encoder"
"github.com/Worldcoin/hubble-commander/eth"
"github.com/Worldcoin/hubble-commander/models"
Expand All @@ -11,6 +9,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)

var (
Expand Down
3 changes: 1 addition & 2 deletions commander/executor/utils.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
package executor

import (
"log"

"github.com/Worldcoin/hubble-commander/models"
st "github.com/Worldcoin/hubble-commander/storage"
log "github.com/sirupsen/logrus"
)

func logAndSaveTransactionError(storage *st.Storage, transaction *models.TransactionBase, transactionError error) {
Expand Down
2 changes: 1 addition & 1 deletion commander/new_block.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ package commander
import (
"context"
stdErrors "errors"
"log"
"math/big"

"github.com/Worldcoin/hubble-commander/utils/ref"
"github.com/ethereum/go-ethereum/core/types"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)

var (
Expand Down
21 changes: 21 additions & 0 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (

"github.com/Worldcoin/hubble-commander/models"
"github.com/Worldcoin/hubble-commander/utils"
"github.com/sirupsen/logrus"
"github.com/spf13/viper"
)

Expand All @@ -30,6 +31,7 @@ func GetConfig() *Config {
setupViper()

return &Config{
Log: getLogConfig(),
Bootstrap: &BootstrapConfig{
Prune: false, // overridden in main
GenesisAccounts: getGenesisAccounts(),
Expand Down Expand Up @@ -68,6 +70,7 @@ func GetTestConfig() *Config {
setupViper()

return &Config{
Log: getLogConfig(),
Bootstrap: &BootstrapConfig{
Prune: false, // overridden in main
GenesisAccounts: getGenesisAccounts(),
Expand Down Expand Up @@ -127,6 +130,24 @@ func getBadgerPath() string {
return path.Join(utils.GetProjectRoot(), "db", "badger", "data")
}

func getLogConfig() *LogConfig {
level, err := logrus.ParseLevel(getString("log.level", "info"))
if err != nil {
log.Fatalf("invalid log level: %e", err)
}

format := getString("log.level", "text")

if format != "text" && format != "json" {
log.Fatalf("invalid log format: %s", format)
}

return &LogConfig{
Level: level,
Format: format,
}
}

func getEthereumConfig() *EthereumConfig {
rpcURL := getStringOrNil("ethereum.rpc_url")
if rpcURL == nil {
Expand Down
2 changes: 1 addition & 1 deletion config/get_viper.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package config

import (
"log"
"time"

log "github.com/sirupsen/logrus"
"github.com/spf13/viper"
)

Expand Down
8 changes: 8 additions & 0 deletions config/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ import (
"time"

"github.com/Worldcoin/hubble-commander/models"
"github.com/sirupsen/logrus"
)

type Config struct {
Log *LogConfig
Bootstrap *BootstrapConfig
Rollup *RollupConfig
API *APIConfig
Expand All @@ -15,6 +17,12 @@ type Config struct {
Ethereum *EthereumConfig
}

type LogConfig struct {
Level logrus.Level
// "json" or "text" (default)
Format string
}

type BootstrapConfig struct {
Prune bool
GenesisAccounts []models.GenesisAccount
Expand Down
2 changes: 1 addition & 1 deletion db/postgres/create_database.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package postgres

import (
"fmt"
"log"
"strings"

"github.com/Worldcoin/hubble-commander/config"
"github.com/Worldcoin/hubble-commander/utils/ref"
"github.com/jmoiron/sqlx"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)

func CreateDatabaseIfNotExist(cfg *config.PostgresConfig) (err error) {
Expand Down
2 changes: 1 addition & 1 deletion e2e/setup/docker_commander.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package setup
import (
"context"
"fmt"
"log"
"os"
"time"

Expand All @@ -15,6 +14,7 @@ import (
docker "github.com/docker/docker/client"
"github.com/docker/docker/pkg/stdcopy"
"github.com/docker/go-connections/nat"
log "github.com/sirupsen/logrus"
"github.com/ybbus/jsonrpc/v2"
)

Expand Down
2 changes: 1 addition & 1 deletion e2e/sync_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
package e2e

import (
"log"
log "github.com/sirupsen/logrus"
"testing"
"time"

Expand Down
3 changes: 1 addition & 2 deletions encoder/generic_transaction.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
package encoder

import (
"log"

"github.com/Worldcoin/hubble-commander/models"
"github.com/Worldcoin/hubble-commander/models/enums/txtype"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)

func HashGenericTransaction(tx models.GenericTransaction) (*common.Hash, error) {
Expand Down
3 changes: 1 addition & 2 deletions eth/deployer/account_registry.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
package deployer

import (
"log"

"github.com/Worldcoin/hubble-commander/contracts/accountregistry"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)

func DeployAccountRegistry(c ChainConnection) (*common.Address, *uint64, *accountregistry.AccountRegistry, error) {
Expand Down
3 changes: 1 addition & 2 deletions eth/rollup/rollup.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package rollup

import (
"log"

"github.com/Worldcoin/hubble-commander/contracts/accountregistry"
"github.com/Worldcoin/hubble-commander/contracts/create2transfer"
"github.com/Worldcoin/hubble-commander/contracts/depositmanager"
Expand All @@ -17,6 +15,7 @@ import (
"github.com/Worldcoin/hubble-commander/models"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)

const (
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ require (
github.com/kr/pretty v0.2.0 // indirect
github.com/lib/pq v1.9.0 // indirect
github.com/pkg/errors v0.9.1
github.com/sirupsen/logrus v1.7.0
github.com/spf13/viper v1.7.1
github.com/stretchr/testify v1.7.0
github.com/timshannon/badgerhold/v3 v3.0.0-20210415132401-e7c90fb5919f
Expand Down
21 changes: 19 additions & 2 deletions main/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ package main
import (
"flag"
"fmt"
"log"
"os"
"os/signal"
"syscall"

"github.com/Worldcoin/hubble-commander/commander"
"github.com/Worldcoin/hubble-commander/config"
log "github.com/sirupsen/logrus"
)

func main() {
cmd := commander.NewCommander(getConfig())
cfg := getConfig()

configureLogger(cfg)
cmd := commander.NewCommander(cfg)

setupCloseHandler(cmd)

Expand All @@ -23,6 +26,20 @@ func main() {
}
}

func configureLogger(cfg *config.Config) {
if cfg.Log.Format == "json" {
// Log as JSON instead of the default ASCII formatter.
log.SetFormatter(&log.JSONFormatter{})
}

// Output to stdout instead of the default stderr
// Can be any io.Writer, see below for File example
log.SetOutput(os.Stdout)

// Only log the warning severity or above.
log.SetLevel(cfg.Log.Level)
}

func getConfig() *config.Config {
prune := flag.Bool("prune", false, "drop database before running app")
devMode := flag.Bool("dev", false, "disable signature verification")
Expand Down
2 changes: 1 addition & 1 deletion utils/merkletree/merkle_tree.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package merkletree

import (
"fmt"
"log"
"math"

"github.com/Worldcoin/hubble-commander/models"
"github.com/Worldcoin/hubble-commander/utils"
"github.com/ethereum/go-ethereum/common"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
)

const MaxDepth = 32
Expand Down

0 comments on commit 9ffecf6

Please sign in to comment.