Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: switch to slog for logging #242

Merged
merged 1 commit into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 36 additions & 17 deletions cmd/bluefin/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@ package main
import (
"flag"
"fmt"
"log/slog"
"os"

_ "go.uber.org/automaxprocs"

"github.com/blinklabs-io/bluefin/internal/config"
"github.com/blinklabs-io/bluefin/internal/indexer"
"github.com/blinklabs-io/bluefin/internal/logging"
"github.com/blinklabs-io/bluefin/internal/storage"
"github.com/blinklabs-io/bluefin/internal/version"
"github.com/blinklabs-io/bluefin/internal/wallet"
)

var cmdlineFlags struct {
configFile string
debug bool
}

func main() {
Expand All @@ -40,6 +41,12 @@ func main() {
"",
"path to config file to load",
)
flag.BoolVar(
&cmdlineFlags.debug,
"debug",
false,
"enable debug logging",
)
flag.Parse()

// Load config
Expand All @@ -49,36 +56,48 @@ func main() {
os.Exit(1)
}

// Configure logging
logging.Setup()
logger := logging.GetLogger()
// Sync logger on exit
defer func() {
if err := logger.Sync(); err != nil {
// We don't actually care about the error here, but we have to do something
// to appease the linter
return
}
}()
// Configure logger
logLevel := slog.LevelInfo
if cmdlineFlags.debug {
logLevel = slog.LevelDebug
}
logger := slog.New(
slog.NewJSONHandler(os.Stdout, &slog.HandlerOptions{
Level: logLevel,
}),
)
slog.SetDefault(logger)

logger.Infof("bluefin %s started", version.GetVersionString())
slog.Info(
fmt.Sprintf("bluefin %s started", version.GetVersionString()),
)
// Load storage
if err := storage.GetStorage().Load(); err != nil {
logger.Fatalf("failed to load storage: %s", err)
slog.Error(
fmt.Sprintf("failed to load storage: %s", err),
)
os.Exit(1)
}

// Setup wallet
wallet.Setup()
bursa := wallet.GetWallet()
logger.Infof("loaded mnemonic for address: %s", bursa.PaymentAddress)
slog.Info(
fmt.Sprintf("loaded mnemonic for address: %s", bursa.PaymentAddress),
)

// Fake Tx
//tx.SendTx([]byte("foo"))

// Start indexer
logger.Infof("starting indexer on %s", cfg.Network)
slog.Info(
fmt.Sprintf("starting indexer on %s", cfg.Network),
)
if err := indexer.GetIndexer().Start(); err != nil {
logger.Fatalf("failed to start indexer: %s", err)
slog.Error(
fmt.Sprintf("failed to start indexer: %s", err),
)
os.Exit(1)
}

// Wait forever
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ require (
github.com/kelseyhightower/envconfig v1.4.0
github.com/minio/sha256-simd v1.0.1
go.uber.org/automaxprocs v1.5.3
go.uber.org/zap v1.27.0
golang.org/x/crypto v0.25.0
gopkg.in/yaml.v2 v2.4.0
)
Expand Down Expand Up @@ -62,6 +61,7 @@ require (
github.com/x448/float16 v0.8.4 // indirect
go.opencensus.io v0.22.5 // indirect
go.uber.org/multierr v1.11.0 // indirect
go.uber.org/zap v1.27.0 // indirect
golang.org/x/exp v0.0.0-20240404231335-c0f41cb1a7a0 // indirect
golang.org/x/net v0.25.0 // indirect
golang.org/x/sys v0.22.0 // indirect
Expand Down
10 changes: 0 additions & 10 deletions internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ type Config struct {
Submit SubmitConfig `yaml:"submit"`
Wallet WalletConfig `yaml:"wallet"`
Miner MinerConfig `yaml:"miner"`
Logging LoggingConfig `yaml:"logging"`
Metrics MetricsConfig `yaml:"metrics"`
Debug DebugConfig `yaml:"debug"`
Profile string `yaml:"profile" envconfig:"PROFILE"`
Expand Down Expand Up @@ -68,11 +67,6 @@ type MinerConfig struct {
Message string `yaml:"message" envconfig:"MINER_MESSAGE"`
}

type LoggingConfig struct {
Healthchecks bool `yaml:"healthchecks" envconfig:"LOGGING_HEALTHCHECKS"`
Level string `yaml:"level" envconfig:"LOGGING_LEVEL"`
}

type MetricsConfig struct {
ListenAddress string `yaml:"address" envconfig:"METRICS_LISTEN_ADDRESS"`
ListenPort uint `yaml:"port" envconfig:"METRICS_LISTEN_PORT"`
Expand All @@ -85,10 +79,6 @@ type DebugConfig struct {

// Singleton config instance with default values
var globalConfig = &Config{
Logging: LoggingConfig{
Level: "info",
Healthchecks: false,
},
Debug: DebugConfig{
ListenAddress: "localhost",
ListenPort: 0,
Expand Down
Loading