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

refactor: Change logging calls to use feedback in CLI package #714

Merged
merged 1 commit into from
Aug 3, 2022
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
4 changes: 2 additions & 2 deletions cli/addreplicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ for the p2p data sync system.`,
return fmt.Errorf("could not parse peer address: %w", err)
}

log.Info(
log.FeedbackInfo(
cmd.Context(),
"Adding replicator for collection",
logging.NewKV("PeerAddress", peerAddr),
Expand All @@ -67,7 +67,7 @@ for the p2p data sync system.`,
if err != nil {
return fmt.Errorf("failed to add replicator, request failed: %w", err)
}
log.Info(ctx, "Successfully added replicator", logging.NewKV("PID", pid))
log.FeedbackInfo(ctx, "Successfully added replicator", logging.NewKV("PID", pid))
return nil
},
}
Expand Down
16 changes: 8 additions & 8 deletions cli/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,9 @@ See https://docs.source.network/BSL.txt for more information.
}

if defaultConfig {
log.Info(cmd.Context(), "Using default configuration")
log.FeedbackInfo(cmd.Context(), "Using default configuration")
} else {
log.Info(cmd.Context(), fmt.Sprintf("Configuration loaded from DefraDB directory %v", rootDir))
log.FeedbackInfo(cmd.Context(), fmt.Sprintf("Configuration loaded from DefraDB directory %v", rootDir))

}
return nil
Expand All @@ -82,7 +82,7 @@ func init() {
)
err := viper.BindPFlag("log.level", rootCmd.PersistentFlags().Lookup("loglevel"))
if err != nil {
log.FatalE(context.Background(), "Could not bind logging.loglevel", err)
log.FeedbackFatalE(context.Background(), "Could not bind logging.loglevel", err)
}

rootCmd.PersistentFlags().StringArray(
Expand All @@ -96,7 +96,7 @@ func init() {
)
err = viper.BindPFlag("log.outputpath", rootCmd.PersistentFlags().Lookup("logoutput"))
if err != nil {
log.FatalE(context.Background(), "Could not bind log.outputpath", err)
log.FeedbackFatalE(context.Background(), "Could not bind log.outputpath", err)
}

rootCmd.PersistentFlags().String(
Expand All @@ -105,7 +105,7 @@ func init() {
)
err = viper.BindPFlag("log.format", rootCmd.PersistentFlags().Lookup("logformat"))
if err != nil {
log.FatalE(context.Background(), "Could not bind log.format", err)
log.FeedbackFatalE(context.Background(), "Could not bind log.format", err)
}

rootCmd.PersistentFlags().Bool(
Expand All @@ -114,7 +114,7 @@ func init() {
)
err = viper.BindPFlag("log.stacktrace", rootCmd.PersistentFlags().Lookup("logtrace"))
if err != nil {
log.FatalE(context.Background(), "Could not bind log.stacktrace", err)
log.FeedbackFatalE(context.Background(), "Could not bind log.stacktrace", err)
}

rootCmd.PersistentFlags().Bool(
Expand All @@ -123,7 +123,7 @@ func init() {
)
err = viper.BindPFlag("log.nocolor", rootCmd.PersistentFlags().Lookup("lognocolor"))
if err != nil {
log.FatalE(context.Background(), "Could not bind log.nocolor", err)
log.FeedbackFatalE(context.Background(), "Could not bind log.nocolor", err)
}

rootCmd.PersistentFlags().String(
Expand All @@ -132,6 +132,6 @@ func init() {
)
err = viper.BindPFlag("api.address", rootCmd.PersistentFlags().Lookup("url"))
if err != nil {
log.FatalE(context.Background(), "Could not bind api.address", err)
log.FeedbackFatalE(context.Background(), "Could not bind api.address", err)
}
}
2 changes: 1 addition & 1 deletion cli/rpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func init() {
)
err := viper.BindPFlag("net.rpcaddress", rpcCmd.PersistentFlags().Lookup("addr"))
if err != nil {
log.FatalE(context.Background(), "Could not bind net.rpcaddress", err)
log.FeedbackFatalE(context.Background(), "Could not bind net.rpcaddress", err)
}
clientCmd.AddCommand(rpcCmd)
}
8 changes: 4 additions & 4 deletions cli/serverdump.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ var serverDumpCmd = &cobra.Command{
Use: "server-dump",
Short: "Dumps the state of the entire database",
RunE: func(cmd *cobra.Command, _ []string) error {
log.Info(cmd.Context(), "Starting DefraDB process...")
log.FeedbackInfo(cmd.Context(), "Starting DefraDB process...")

// setup signal handlers
signalCh := make(chan os.Signal, 1)
Expand All @@ -41,11 +41,11 @@ var serverDumpCmd = &cobra.Command{
exists := (err == nil && info.IsDir())
if !exists {
return fmt.Errorf(
"Badger store does not exist at %s. Try with an existing directory",
"badger store does not exist at %s. Try with an existing directory",
cfg.Datastore.Badger.Path,
)
}
log.Info(cmd.Context(), "Opening badger store", logging.NewKV("Path", cfg.Datastore.Badger.Path))
log.FeedbackInfo(cmd.Context(), "Opening badger store", logging.NewKV("Path", cfg.Datastore.Badger.Path))
rootstore, err = badgerds.NewDatastore(cfg.Datastore.Badger.Path, cfg.Datastore.Badger.Options)
if err != nil {
return fmt.Errorf("could not open badger datastore: %w", err)
Expand All @@ -59,7 +59,7 @@ var serverDumpCmd = &cobra.Command{
return fmt.Errorf("failed to initialize database: %w", err)
}

log.Info(cmd.Context(), "Dumping DB state...")
log.FeedbackInfo(cmd.Context(), "Dumping DB state...")
db.PrintDump(cmd.Context())
return nil
},
Expand Down
36 changes: 17 additions & 19 deletions cli/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ var startCmd = &cobra.Command{
if err := parseAndConfigLog(cmd.Context(), cfg.Log, cmd); err != nil {
return err
}
log.Info(cmd.Context(), fmt.Sprintf("Configuration loaded from DefraDB directory %v", rootDir))
log.FeedbackInfo(cmd.Context(), fmt.Sprintf("Configuration loaded from DefraDB directory %v", rootDir))
return nil
},
RunE: func(cmd *cobra.Command, args []string) error {
ctx := context.Background()
log.Info(ctx, "Starting DefraDB service...")
log.FeedbackInfo(ctx, "Starting DefraDB service...")

// setup signal handlers
signalCh := make(chan os.Signal, 1)
Expand All @@ -82,7 +82,7 @@ var startCmd = &cobra.Command{

var err error
if cfg.Datastore.Store == badgerDatastoreName {
log.Info(
log.FeedbackInfo(
cmd.Context(),
"Opening badger store",
logging.NewKV("Path", cfg.Datastore.Badger.Path),
Expand All @@ -92,7 +92,7 @@ var startCmd = &cobra.Command{
cfg.Datastore.Badger.Options,
)
} else if cfg.Datastore.Store == "memory" {
log.Info(cmd.Context(), "Building new memory store")
log.FeedbackInfo(cmd.Context(), "Building new memory store")
opts := badgerds.Options{Options: badger.DefaultOptions("").WithInMemory(true)}
rootstore, err = badgerds.NewDatastore("", &opts)
}
Expand All @@ -118,18 +118,17 @@ var startCmd = &cobra.Command{
// init the p2p node
var n *node.Node
if !cfg.Net.P2PDisabled {
log.Info(cmd.Context(), "Starting P2P node", logging.NewKV("P2P address", cfg.Net.P2PAddress))
log.FeedbackInfo(cmd.Context(), "Starting P2P node", logging.NewKV("P2P address", cfg.Net.P2PAddress))
n, err = node.NewNode(
cmd.Context(),
db,
bs,
cfg.NodeConfig(),
)
if err != nil {
log.ErrorE(cmd.Context(), "Failed to start P2P node", err)
n.Close() //nolint:errcheck
db.Close(cmd.Context())
return err
return fmt.Errorf("failed to start P2P node: %w", err)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

question: We remove logging here because we will log it once it bubble ups?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes 🙂

}

// parse peers and bootstrap
Expand All @@ -144,10 +143,9 @@ var startCmd = &cobra.Command{
}

if err := n.Start(); err != nil {
log.ErrorE(cmd.Context(), "Failed to start P2P listeners", err)
n.Close() //nolint:errcheck
db.Close(cmd.Context())
return err
return fmt.Errorf("failed to start P2P listeners: %w", err)
}

MtcpAddr, err := ma.NewMultiaddr(cfg.Net.TCPAddress)
Expand Down Expand Up @@ -175,17 +173,17 @@ var startCmd = &cobra.Command{
netService := netapi.NewService(n.Peer)

go func() {
log.Info(cmd.Context(), "Started RPC server", logging.NewKV("Address", addr))
log.FeedbackInfo(cmd.Context(), "Started RPC server", logging.NewKV("Address", addr))
netpb.RegisterServiceServer(server, netService)
if err := server.Serve(tcplistener); err != nil && !errors.Is(err, grpc.ErrServerStopped) {
log.FatalE(cmd.Context(), "failed to start RPC server", err)
log.FeedbackFatalE(cmd.Context(), "failed to start RPC server", err)
}
}()
}

// run the server listener in a separate goroutine
go func() {
log.Info(
log.FeedbackInfo(
cmd.Context(),
fmt.Sprintf(
"Providing HTTP API at %s%s. Use the GraphQL query endpoint at %s%s/graphql ",
Expand All @@ -197,7 +195,7 @@ var startCmd = &cobra.Command{
)
s := http.NewServer(db, http.WithAddress(cfg.API.Address))
if err := s.Listen(); err != nil {
log.ErrorE(cmd.Context(), "Failed to start HTTP API listener", err)
log.FeedbackErrorE(cmd.Context(), "Failed to start HTTP API listener", err)
if n != nil {
n.Close() //nolint:errcheck
}
Expand All @@ -208,7 +206,7 @@ var startCmd = &cobra.Command{

// wait for shutdown signal
<-signalCh
log.Info(cmd.Context(), "Received interrupt; closing database...")
log.FeedbackInfo(cmd.Context(), "Received interrupt; closing database...")
if n != nil {
n.Close() //nolint:errcheck
}
Expand All @@ -225,7 +223,7 @@ func init() {
)
err := viper.BindPFlag("net.peers", startCmd.Flags().Lookup("peers"))
if err != nil {
log.FatalE(context.Background(), "Could not bind net.peers", err)
log.FeedbackFatalE(context.Background(), "Could not bind net.peers", err)
}

startCmd.Flags().String(
Expand All @@ -234,7 +232,7 @@ func init() {
)
err = viper.BindPFlag("datastore.store", startCmd.Flags().Lookup("store"))
if err != nil {
log.FatalE(context.Background(), "Could not bind datastore.store", err)
log.FeedbackFatalE(context.Background(), "Could not bind datastore.store", err)
}

startCmd.Flags().String(
Expand All @@ -243,7 +241,7 @@ func init() {
)
err = viper.BindPFlag("net.p2paddress", startCmd.Flags().Lookup("p2paddr"))
if err != nil {
log.FatalE(context.Background(), "Could not bind net.p2paddress", err)
log.FeedbackFatalE(context.Background(), "Could not bind net.p2paddress", err)
}

startCmd.Flags().String(
Expand All @@ -252,7 +250,7 @@ func init() {
)
err = viper.BindPFlag("net.tcpaddress", startCmd.Flags().Lookup("tcpaddr"))
if err != nil {
log.FatalE(context.Background(), "Could not bind net.tcpaddress", err)
log.FeedbackFatalE(context.Background(), "Could not bind net.tcpaddress", err)
}

startCmd.Flags().Bool(
Expand All @@ -261,7 +259,7 @@ func init() {
)
err = viper.BindPFlag("net.p2pdisabled", startCmd.Flags().Lookup("no-p2p"))
if err != nil {
log.FatalE(context.Background(), "Could not bind net.p2pdisabled", err)
log.FeedbackFatalE(context.Background(), "Could not bind net.p2pdisabled", err)
}

rootCmd.AddCommand(startCmd)
Expand Down