Skip to content

Commit

Permalink
chore: Improve log outputs (#506)
Browse files Browse the repository at this point in the history
  • Loading branch information
orpheuslummis authored Jun 7, 2022
1 parent 45978e0 commit 3c91e36
Show file tree
Hide file tree
Showing 21 changed files with 87 additions and 87 deletions.
8 changes: 4 additions & 4 deletions cli/defradb/cmd/blocks_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,26 +45,26 @@ var getCmd = &cobra.Command{

endpoint, err := httpapi.JoinPaths(dbaddr, httpapi.BlocksPath, cid)
if err != nil {
log.ErrorE(ctx, "join paths failed", err)
log.ErrorE(ctx, "Join paths failed", err)
return
}

res, err := http.Get(endpoint.String())
if err != nil {
log.ErrorE(ctx, "request failed", err)
log.ErrorE(ctx, "Request failed", err)
return
}

defer func() {
err = res.Body.Close()
if err != nil {
log.ErrorE(ctx, "response body closing failed", err)
log.ErrorE(ctx, "Response body closing failed", err)
}
}()

buf, err := io.ReadAll(res.Body)
if err != nil {
log.ErrorE(ctx, "request failed", err)
log.ErrorE(ctx, "Request failed", err)
return
}
log.Debug(ctx, "", logging.NewKV("Block", string(buf)))
Expand Down
8 changes: 4 additions & 4 deletions cli/defradb/cmd/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,26 +41,26 @@ var dumpCmd = &cobra.Command{

endpoint, err := httpapi.JoinPaths(dbaddr, httpapi.DumpPath)
if err != nil {
log.ErrorE(ctx, "join paths failed", err)
log.ErrorE(ctx, "Join paths failed", err)
return
}

res, err := http.Get(endpoint.String())
if err != nil {
log.ErrorE(ctx, "request failed", err)
log.ErrorE(ctx, "Request failed", err)
return
}

defer func() {
err = res.Body.Close()
if err != nil {
log.ErrorE(ctx, "response body closing failed", err)
log.ErrorE(ctx, "Response body closing failed", err)
}
}()

buf, err := io.ReadAll(res.Body)
if err != nil {
log.ErrorE(ctx, "request failed", err)
log.ErrorE(ctx, "Request failed", err)
return
}
if string(buf) == "ok" {
Expand Down
8 changes: 4 additions & 4 deletions cli/defradb/cmd/ping.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,26 @@ var pingCmd = &cobra.Command{

endpoint, err := httpapi.JoinPaths(dbaddr, httpapi.PingPath)
if err != nil {
log.ErrorE(ctx, "join paths failed", err)
log.ErrorE(ctx, "Join paths failed", err)
return
}

res, err := http.Get(endpoint.String())
if err != nil {
log.ErrorE(ctx, "request failed", err)
log.ErrorE(ctx, "Request failed", err)
return
}

defer func() {
err = res.Body.Close()
if err != nil {
log.ErrorE(ctx, "response body closing failed", err)
log.ErrorE(ctx, "Response body closing failed", err)
}
}()

buf, err := io.ReadAll(res.Body)
if err != nil {
log.ErrorE(ctx, "request failed", err)
log.ErrorE(ctx, "Request failed", err)
return
}
if string(buf) == "pong" {
Expand Down
12 changes: 6 additions & 6 deletions cli/defradb/cmd/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,17 @@ the additional documentation found at: https://hackmd.io/@source/BksQY6Qfw.
}

if len(args) != 1 {
log.Fatal(ctx, "needs a single query argument")
log.Fatal(ctx, "Needs a single query argument")
}
query := args[0]
if query == "" {
log.Error(ctx, "missing query")
log.Error(ctx, "Missing query")
return
}

endpoint, err := httpapi.JoinPaths(dbaddr, httpapi.GraphQLPath)
if err != nil {
log.ErrorE(ctx, "join paths failed", err)
log.ErrorE(ctx, "Join paths failed", err)
return
}

Expand All @@ -73,20 +73,20 @@ the additional documentation found at: https://hackmd.io/@source/BksQY6Qfw.

res, err := http.Get(endpoint.String())
if err != nil {
log.ErrorE(ctx, "request failed", err)
log.ErrorE(ctx, "Request failed", err)
return
}

defer func() {
err = res.Body.Close()
if err != nil {
log.ErrorE(ctx, "response body closing failed: ", err)
log.ErrorE(ctx, "Response body closing failed: ", err)
}
}()

buf, err := io.ReadAll(res.Body)
if err != nil {
log.ErrorE(ctx, "request failed", err)
log.ErrorE(ctx, "Request failed", err)
return
}

Expand Down
4 changes: 2 additions & 2 deletions cli/defradb/cmd/schema_add.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ var addCmd = &cobra.Command{
}
endpoint, err := httpapi.JoinPaths(dbaddr, httpapi.SchemaLoadPath)
if err != nil {
log.ErrorE(ctx, "join paths failed", err)
log.ErrorE(ctx, "Join paths failed", err)
return
}

Expand All @@ -68,7 +68,7 @@ var addCmd = &cobra.Command{
defer func() {
err = res.Body.Close()
if err != nil {
log.ErrorE(ctx, "response body closing failed", err)
log.ErrorE(ctx, "Response body closing failed", err)
}
}()

Expand Down
10 changes: 5 additions & 5 deletions cli/defradb/cmd/serverdump.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,26 +43,26 @@ var srvDumpCmd = &cobra.Command{
if config.Database.Store == databaseStoreName {
log.Info(
ctx,
"opening badger store",
"Opening badger store",
logging.NewKV("Path", config.Database.Badger.Path),
)
rootstore, err = badgerds.NewDatastore(
config.Database.Badger.Path,
config.Database.Badger.Options,
)
} else {
log.Fatal(ctx, "Server side dump is only supported for the Badger datastore")
log.Fatal(ctx, "Server-side dump is only supported for the Badger datastore")
}
if err != nil {
log.FatalE(ctx, "Failed to initiate datastore:", err)
log.FatalE(ctx, "Failed to initialize datastore", err)
}

db, err := db.NewDB(ctx, rootstore)
if err != nil {
log.FatalE(ctx, "Failed to initiate database:", err)
log.FatalE(ctx, "Failed to initialize database", err)
}

log.Info(ctx, "Dumping DB state:")
log.Info(ctx, "Dumping DB state...")
db.PrintDump(ctx)
},
}
Expand Down
18 changes: 9 additions & 9 deletions cli/defradb/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,15 @@ var startCmd = &cobra.Command{
if config.Database.Store == "badger" {
log.Info(
ctx,
"opening badger store",
"Opening badger store",
logging.NewKV("Path", config.Database.Badger.Path),
)
rootstore, err = badgerds.NewDatastore(
config.Database.Badger.Path,
config.Database.Badger.Options,
)
} else if config.Database.Store == "memory" {
log.Info(ctx, "building new memory store")
log.Info(ctx, "Building new memory store")
opts := badgerds.Options{Options: badger.DefaultOptions("").WithInMemory(true)}
rootstore, err = badgerds.NewDatastore("", &opts)
}
Expand All @@ -102,7 +102,7 @@ var startCmd = &cobra.Command{
// init the p2p node
var n *node.Node
if !config.Net.P2PDisabled {
log.Info(ctx, "Starting P2P node", logging.NewKV("tcp address", config.Net.TCPAddress))
log.Info(ctx, "Starting P2P node", logging.NewKV("TCP address", config.Net.TCPAddress))
n, err = node.NewNode(
ctx,
db,
Expand All @@ -111,7 +111,7 @@ var startCmd = &cobra.Command{
node.ListenP2PAddrStrings(config.Net.P2PAddress),
node.WithPubSub(true))
if err != nil {
log.ErrorE(ctx, "Failed to start p2p node", err)
log.ErrorE(ctx, "Failed to start P2P node", err)
n.Close() //nolint
db.Close(ctx)
os.Exit(1)
Expand All @@ -124,12 +124,12 @@ var startCmd = &cobra.Command{
if err != nil {
log.ErrorE(ctx, "Failed to parse bootstrap peers", err)
}
log.Debug(ctx, "Bootstraping with peers", logging.NewKV("Addresses", addrs))
log.Debug(ctx, "Bootstrapping with peers", logging.NewKV("Addresses", addrs))
n.Boostrap(addrs)
}

if err := n.Start(); err != nil {
log.ErrorE(ctx, "Failed to start p2p listeners", err)
log.ErrorE(ctx, "Failed to start P2P listeners", err)
n.Close() //nolint
db.Close(ctx)
os.Exit(1)
Expand Down Expand Up @@ -160,11 +160,11 @@ var startCmd = &cobra.Command{
netService := netapi.NewService(n.Peer)

go func() {
log.Info(ctx, "Started gRPC server", logging.NewKV("Address", addr))
log.Info(ctx, "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(ctx, "serve error", err)
log.FatalE(ctx, "Server error", err)
}
}()
}
Expand Down Expand Up @@ -194,7 +194,7 @@ var startCmd = &cobra.Command{

// wait for shutdown signal
<-signalCh
log.Info(ctx, "Received interrupt; closing db")
log.Info(ctx, "Received interrupt; closing database...")
if n != nil {
n.Close() //nolint
}
Expand Down
4 changes: 2 additions & 2 deletions datastore/badger/v3/datastore.go
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ func (d *Datastore) periodicGC() {
case ErrClosed:
return
default:
log.Errorf("error during a GC cycle: %s", err)
log.Errorf("Error during a GC cycle: %s", err)
// Not much we can do on a random error but log it and continue.
gcTimeout.Reset(d.gcInterval)
}
Expand Down Expand Up @@ -431,7 +431,7 @@ func (d *Datastore) Batch(ctx context.Context) (ds.Batch, error) {
// batch is abandoned.
runtime.SetFinalizer(b, func(b *batch) {
b.cancel()
log.Error("batch not committed or canceled")
log.Error("Batch not committed or canceled")
})

return b, nil
Expand Down
2 changes: 1 addition & 1 deletion datastore/blockstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func (bs *bstore) HashOnRead(enabled bool) {

func (bs *bstore) Get(ctx context.Context, k cid.Cid) (blocks.Block, error) {
if !k.Defined() {
log.Error(ctx, "Undefined cid in blockstore")
log.Error(ctx, "Undefined CID in blockstore")
return nil, ipld.ErrNotFound{Cid: k}
}
bdata, err := bs.store.Get(ctx, dshelp.MultihashToDsKey(k.Hash()))
Expand Down
4 changes: 2 additions & 2 deletions db/collection.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (db *db) CreateCollection(
ctx,
"Created collection",
logging.NewKV("Name", col.Name()),
logging.NewKV("Id", col.SchemaID),
logging.NewKV("ID", col.SchemaID),
)
return col, err
}
Expand Down Expand Up @@ -213,7 +213,7 @@ func (db *db) GetCollectionByName(ctx context.Context, name string) (client.Coll
ctx,
"Retrieved collection",
logging.NewKV("Name", desc.Name),
logging.NewKV("Id", sid),
logging.NewKV("ID", sid),
)

return &collection{
Expand Down
10 changes: 5 additions & 5 deletions db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,18 +80,18 @@ func NewDB(ctx context.Context, rootstore ds.Batching, options ...Option) (clien
}

func newDB(ctx context.Context, rootstore ds.Batching, options ...Option) (*db, error) {
log.Debug(ctx, "loading: internal datastores")
log.Debug(ctx, "Loading: internal datastores")
root := datastore.AsDSReaderWriter(rootstore)
multistore := datastore.MultiStoreFrom(root)
crdtFactory := crdt.DefaultFactory.WithStores(multistore)

log.Debug(ctx, "loading: schema manager")
log.Debug(ctx, "Loading: schema manager")
sm, err := schema.NewSchemaManager()
if err != nil {
return nil, err
}

log.Debug(ctx, "loading: query executor")
log.Debug(ctx, "Loading: query executor")
exec, err := planner.NewQueryExecutor(sm)
if err != nil {
return nil, err
Expand Down Expand Up @@ -147,15 +147,15 @@ func (db *db) initialize(ctx context.Context) error {
db.glock.Lock()
defer db.glock.Unlock()

log.Debug(ctx, "Checking if db has already been initialized...")
log.Debug(ctx, "Checking if DB has already been initialized...")
exists, err := db.systemstore().Has(ctx, ds.NewKey("init"))
if err != nil && err != ds.ErrNotFound {
return err
}
// if we're loading an existing database, just load the schema
// and finish initialization
if exists {
log.Debug(ctx, "DB has already been initialized, continuing.")
log.Debug(ctx, "DB has already been initialized, continuing")
return db.loadSchema(ctx)
}

Expand Down
6 changes: 3 additions & 3 deletions merkle/clock/clock.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func (mc *MerkleClock) ProcessNode(
node ipld.Node,
) ([]cid.Cid, error) {
current := node.Cid()
log.Debug(ctx, "Running ProcessNode", logging.NewKV("Cid", current))
log.Debug(ctx, "Running ProcessNode", logging.NewKV("CID", current))
err := mc.crdt.Merge(ctx, delta, dshelp.MultihashToDsKey(current.Hash()).String())
if err != nil {
return nil, fmt.Errorf("error merging delta from %s : %w", current, err)
Expand All @@ -146,7 +146,7 @@ func (mc *MerkleClock) ProcessNode(
hasHeads := false
log.Debug(ctx, "Stepping through node links")
for _, l := range links {
log.Debug(ctx, "checking link", logging.NewKV("Name", l.Name), logging.NewKV("Cid", l.Cid))
log.Debug(ctx, "Checking link", logging.NewKV("Name", l.Name), logging.NewKV("CID", l.Cid))
if l.Name == "_head" {
hasHeads = true
break
Expand Down Expand Up @@ -194,7 +194,7 @@ func (mc *MerkleClock) ProcessNode(
if err != nil {
log.ErrorE(
ctx,
"error adding head (when root is new head)",
"Failure adding head (when root is a new head)",
err,
logging.NewKV("Root", root),
)
Expand Down
4 changes: 2 additions & 2 deletions merkle/clock/heads.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func (hh *heads) Replace(ctx context.Context, h, c cid.Cid, height uint64) error
ctx,
"Replacing DAG head",
logging.NewKV("Old", h),
logging.NewKV("Cid", c),
logging.NewKV("CID", c),
logging.NewKV("Height", height))
var store ds.Write = hh.store
var err error
Expand Down Expand Up @@ -133,7 +133,7 @@ func (hh *heads) Replace(ctx context.Context, h, c cid.Cid, height uint64) error

func (hh *heads) Add(ctx context.Context, c cid.Cid, height uint64) error {
log.Info(ctx, "Adding new DAG head",
logging.NewKV("Cid", c),
logging.NewKV("CID", c),
logging.NewKV("Height", height))
return hh.write(ctx, hh.store, c, height)
}
Expand Down
Loading

0 comments on commit 3c91e36

Please sign in to comment.