Skip to content

Commit 9184249

Browse files
karalabeobscuren
authored andcommitted
Logger updates 3 (bnb-chain#3730)
* accounts, cmd, eth, ethdb: port logs over to new system * ethdb: drop concept of cache distribution between dbs * eth: fix some log nitpicks to make them nicer
1 parent 82e7c1d commit 9184249

22 files changed

Lines changed: 256 additions & 292 deletions

File tree

‎accounts/url.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,15 @@ func (u URL) String() string {
6060
return u.Path
6161
}
6262

63+
// TerminalString implements the log.TerminalStringer interface.
64+
func (u URL) TerminalString() string {
65+
url := u.String()
66+
if len(url) > 32 {
67+
return url[:31] + "…"
68+
}
69+
return url
70+
}
71+
6372
// MarshalJSON implements the json.Marshaller interface.
6473
func (u URL) MarshalJSON() ([]byte, error) {
6574
return json.Marshal(u.String())

‎cmd/geth/chaincmd.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ func initGenesis(ctx *cli.Context) error {
117117
if err != nil {
118118
utils.Fatalf("failed to write genesis block: %v", err)
119119
}
120-
log.Info(fmt.Sprintf("successfully wrote genesis block and/or chain rule set: %x", block.Hash()))
120+
log.Info("Successfully wrote genesis state", "hash", block.Hash())
121121
return nil
122122
}
123123

‎cmd/geth/main.go‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/ethereum/go-ethereum/accounts/keystore"
3030
"github.com/ethereum/go-ethereum/cmd/utils"
3131
"github.com/ethereum/go-ethereum/common"
32+
"github.com/ethereum/go-ethereum/common/hexutil"
3233
"github.com/ethereum/go-ethereum/console"
3334
"github.com/ethereum/go-ethereum/contracts/release"
3435
"github.com/ethereum/go-ethereum/eth"
@@ -202,11 +203,10 @@ func makeFullNode(ctx *cli.Context) *node.Node {
202203
}{uint(params.VersionMajor<<16 | params.VersionMinor<<8 | params.VersionPatch), clientIdentifier, runtime.Version(), runtime.GOOS}
203204
extra, err := rlp.EncodeToBytes(clientInfo)
204205
if err != nil {
205-
log.Warn(fmt.Sprint("error setting canonical miner information:", err))
206+
log.Warn("Failed to set canonical miner information", "err", err)
206207
}
207208
if uint64(len(extra)) > params.MaximumExtraDataSize {
208-
log.Warn(fmt.Sprint("error setting canonical miner information: extra exceeds", params.MaximumExtraDataSize))
209-
log.Debug(fmt.Sprintf("extra: %x\n", extra))
209+
log.Warn("Miner extra data exceed limit", "extra", hexutil.Bytes(extra), "limit", params.MaximumExtraDataSize)
210210
extra = nil
211211
}
212212
stack := utils.MakeNode(ctx, clientIdentifier, gitCommit)
@@ -271,7 +271,7 @@ func startNode(ctx *cli.Context, stack *node.Node) {
271271
// Open and self derive any wallets already attached
272272
for _, wallet := range stack.AccountManager().Wallets() {
273273
if err := wallet.Open(""); err != nil {
274-
log.Warn(fmt.Sprintf("Failed to open wallet %s: %v", wallet.URL(), err))
274+
log.Warn("Failed to open wallet", "url", wallet.URL(), "err", err)
275275
} else {
276276
wallet.SelfDerive(accounts.DefaultBaseDerivationPath, stateReader)
277277
}
@@ -280,13 +280,13 @@ func startNode(ctx *cli.Context, stack *node.Node) {
280280
for event := range events {
281281
if event.Arrive {
282282
if err := event.Wallet.Open(""); err != nil {
283-
log.Info(fmt.Sprintf("New wallet appeared: %s, failed to open: %s", event.Wallet.URL(), err))
283+
log.Warn("New wallet appeared, failed to open", "url", event.Wallet.URL(), "err", err)
284284
} else {
285-
log.Info(fmt.Sprintf("New wallet appeared: %s, %s", event.Wallet.URL(), event.Wallet.Status()))
285+
log.Info("New wallet appeared", "url", event.Wallet.URL(), "status", event.Wallet.Status())
286286
event.Wallet.SelfDerive(accounts.DefaultBaseDerivationPath, stateReader)
287287
}
288288
} else {
289-
log.Info(fmt.Sprintf("Old wallet dropped: %s", event.Wallet.URL()))
289+
log.Info("Old wallet dropped", "url", event.Wallet.URL())
290290
event.Wallet.Close()
291291
}
292292
}

‎cmd/swarm/cleandb.go‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,22 @@
1717
package main
1818

1919
import (
20-
"log"
21-
20+
"github.com/ethereum/go-ethereum/cmd/utils"
2221
"github.com/ethereum/go-ethereum/swarm/storage"
2322
"gopkg.in/urfave/cli.v1"
2423
)
2524

2625
func cleandb(ctx *cli.Context) {
2726
args := ctx.Args()
2827
if len(args) != 1 {
29-
log.Fatal("need path to chunks database as the first and only argument")
28+
utils.Fatalf("Need path to chunks database as the first and only argument")
3029
}
3130

3231
chunkDbPath := args[0]
3332
hash := storage.MakeHashFunc("SHA3")
3433
dbStore, err := storage.NewDbStore(chunkDbPath, hash, 10000000, 0)
3534
if err != nil {
36-
log.Fatalf("cannot initialise dbstore: %v", err)
35+
utils.Fatalf("Cannot initialise dbstore: %v", err)
3736
}
3837
dbStore.Cleanup()
3938
}

‎cmd/swarm/hash.go‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,30 +19,29 @@ package main
1919

2020
import (
2121
"fmt"
22-
"log"
2322
"os"
2423

24+
"github.com/ethereum/go-ethereum/cmd/utils"
2525
"github.com/ethereum/go-ethereum/swarm/storage"
2626
"gopkg.in/urfave/cli.v1"
2727
)
2828

2929
func hash(ctx *cli.Context) {
3030
args := ctx.Args()
3131
if len(args) < 1 {
32-
log.Fatal("Usage: swarm hash <file name>")
32+
utils.Fatalf("Usage: swarm hash <file name>")
3333
}
3434
f, err := os.Open(args[0])
3535
if err != nil {
36-
fmt.Println("Error opening file " + args[1])
37-
os.Exit(1)
36+
utils.Fatalf("Error opening file " + args[1])
3837
}
3938
defer f.Close()
4039

4140
stat, _ := f.Stat()
4241
chunker := storage.NewTreeChunker(storage.NewChunkerParams())
4342
key, err := chunker.Split(f, stat.Size(), nil, nil, nil)
4443
if err != nil {
45-
log.Fatalf("%v\n", err)
44+
utils.Fatalf("%v\n", err)
4645
} else {
4746
fmt.Printf("%v\n", key)
4847
}

‎cmd/swarm/main.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,7 @@ func bzzd(ctx *cli.Context) error {
277277
signal.Notify(sigc, syscall.SIGTERM)
278278
defer signal.Stop(sigc)
279279
<-sigc
280-
log.Info(fmt.Sprint("Got sigterm, shutting down..."))
280+
log.Info("Got sigterm, shutting swarm down...")
281281
stack.Stop()
282282
}()
283283
networkId := ctx.GlobalUint64(SwarmNetworkIdFlag.Name)
@@ -342,7 +342,7 @@ func getAccount(ctx *cli.Context, stack *node.Node) *ecdsa.PrivateKey {
342342
}
343343
// Try to load the arg as a hex key file.
344344
if key, err := crypto.LoadECDSA(keyid); err == nil {
345-
log.Info(fmt.Sprintf("swarm account key loaded: %#x", crypto.PubkeyToAddress(key.PublicKey)))
345+
log.Info("Swarm account key loaded", "address", crypto.PubkeyToAddress(key.PublicKey))
346346
return key
347347
}
348348
// Otherwise try getting it from the keystore.
@@ -399,7 +399,7 @@ func injectBootnodes(srv *p2p.Server, nodes []string) {
399399
for _, url := range nodes {
400400
n, err := discover.ParseNode(url)
401401
if err != nil {
402-
log.Error(fmt.Sprintf("invalid bootnode %q", err))
402+
log.Error("Invalid swarm bootnode", "err", err)
403403
continue
404404
}
405405
srv.AddPeer(n)

‎cmd/swarm/manifest.go‎

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,18 @@ package main
2020
import (
2121
"encoding/json"
2222
"fmt"
23-
"log"
2423
"mime"
2524
"path/filepath"
2625
"strings"
2726

27+
"github.com/ethereum/go-ethereum/cmd/utils"
2828
"gopkg.in/urfave/cli.v1"
2929
)
3030

3131
func add(ctx *cli.Context) {
32-
3332
args := ctx.Args()
3433
if len(args) < 3 {
35-
log.Fatal("need atleast three arguments <MHASH> <path> <HASH> [<content-type>]")
34+
utils.Fatalf("Need atleast three arguments <MHASH> <path> <HASH> [<content-type>]")
3635
}
3736

3837
var (
@@ -66,7 +65,7 @@ func update(ctx *cli.Context) {
6665

6766
args := ctx.Args()
6867
if len(args) < 3 {
69-
log.Fatal("need atleast three arguments <MHASH> <path> <HASH>")
68+
utils.Fatalf("Need atleast three arguments <MHASH> <path> <HASH>")
7069
}
7170

7271
var (
@@ -98,7 +97,7 @@ func update(ctx *cli.Context) {
9897
func remove(ctx *cli.Context) {
9998
args := ctx.Args()
10099
if len(args) < 2 {
101-
log.Fatal("need atleast two arguments <MHASH> <path>")
100+
utils.Fatalf("Need atleast two arguments <MHASH> <path>")
102101
}
103102

104103
var (
@@ -134,19 +133,19 @@ func addEntryToManifest(ctx *cli.Context, mhash, path, hash, ctype string) strin
134133

135134
mroot, err := client.downloadManifest(mhash)
136135
if err != nil {
137-
log.Fatalln("manifest download failed:", err)
136+
utils.Fatalf("Manifest download failed: %v", err)
138137
}
139138

140139
//TODO: check if the "hash" to add is valid and present in swarm
141140
_, err = client.downloadManifest(hash)
142141
if err != nil {
143-
log.Fatalln("hash to add is not present:", err)
142+
utils.Fatalf("Hash to add is not present: %v", err)
144143
}
145144

146145
// See if we path is in this Manifest or do we have to dig deeper
147146
for _, entry := range mroot.Entries {
148147
if path == entry.Path {
149-
log.Fatal(path, "Already present, not adding anything")
148+
utils.Fatalf("Path %s already present, not adding anything", path)
150149
} else {
151150
if entry.ContentType == "application/bzz-manifest+json" {
152151
prfxlen := strings.HasPrefix(path, entry.Path)
@@ -183,7 +182,7 @@ func addEntryToManifest(ctx *cli.Context, mhash, path, hash, ctype string) strin
183182

184183
newManifestHash, err := client.uploadManifest(mroot)
185184
if err != nil {
186-
log.Fatalln("manifest upload failed:", err)
185+
utils.Fatalf("Manifest upload failed: %v", err)
187186
}
188187
return newManifestHash
189188

@@ -208,7 +207,7 @@ func updateEntryInManifest(ctx *cli.Context, mhash, path, hash, ctype string) st
208207

209208
mroot, err := client.downloadManifest(mhash)
210209
if err != nil {
211-
log.Fatalln("manifest download failed:", err)
210+
utils.Fatalf("Manifest download failed: %v", err)
212211
}
213212

214213
//TODO: check if the "hash" with which to update is valid and present in swarm
@@ -228,7 +227,7 @@ func updateEntryInManifest(ctx *cli.Context, mhash, path, hash, ctype string) st
228227
}
229228

230229
if longestPathEntry.Path == "" && newEntry.Path == "" {
231-
log.Fatal(path, " Path not present in the Manifest, not setting anything")
230+
utils.Fatalf("Path %s not present in the Manifest, not setting anything", path)
232231
}
233232

234233
if longestPathEntry.Path != "" {
@@ -268,7 +267,7 @@ func updateEntryInManifest(ctx *cli.Context, mhash, path, hash, ctype string) st
268267

269268
newManifestHash, err := client.uploadManifest(mroot)
270269
if err != nil {
271-
log.Fatalln("manifest upload failed:", err)
270+
utils.Fatalf("Manifest upload failed: %v", err)
272271
}
273272
return newManifestHash
274273
}
@@ -292,7 +291,7 @@ func removeEntryFromManifest(ctx *cli.Context, mhash, path string) string {
292291

293292
mroot, err := client.downloadManifest(mhash)
294293
if err != nil {
295-
log.Fatalln("manifest download failed:", err)
294+
utils.Fatalf("Manifest download failed: %v", err)
296295
}
297296

298297
// See if we path is in this Manifest or do we have to dig deeper
@@ -310,7 +309,7 @@ func removeEntryFromManifest(ctx *cli.Context, mhash, path string) string {
310309
}
311310

312311
if longestPathEntry.Path == "" && entryToRemove.Path == "" {
313-
log.Fatal(path, "Path not present in the Manifest, not removing anything")
312+
utils.Fatalf("Path %s not present in the Manifest, not removing anything", path)
314313
}
315314

316315
if longestPathEntry.Path != "" {
@@ -342,8 +341,7 @@ func removeEntryFromManifest(ctx *cli.Context, mhash, path string) string {
342341

343342
newManifestHash, err := client.uploadManifest(mroot)
344343
if err != nil {
345-
log.Fatalln("manifest upload failed:", err)
344+
utils.Fatalf("Manifest upload failed: %v", err)
346345
}
347346
return newManifestHash
348-
349347
}

‎cmd/swarm/upload.go‎

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
"fmt"
2424
"io"
2525
"io/ioutil"
26-
"log"
2726
"mime"
2827
"net/http"
2928
"os"
@@ -32,6 +31,8 @@ import (
3231
"path/filepath"
3332
"strings"
3433

34+
"github.com/ethereum/go-ethereum/cmd/utils"
35+
"github.com/ethereum/go-ethereum/log"
3536
"gopkg.in/urfave/cli.v1"
3637
)
3738

@@ -44,7 +45,7 @@ func upload(ctx *cli.Context) {
4445
defaultPath = ctx.GlobalString(SwarmUploadDefaultPath.Name)
4546
)
4647
if len(args) != 1 {
47-
log.Fatal("need filename as the first and only argument")
48+
utils.Fatalf("Need filename as the first and only argument")
4849
}
4950

5051
var (
@@ -53,25 +54,25 @@ func upload(ctx *cli.Context) {
5354
)
5455
fi, err := os.Stat(expandPath(file))
5556
if err != nil {
56-
log.Fatal(err)
57+
utils.Fatalf("Failed to stat file: %v", err)
5758
}
5859
if fi.IsDir() {
5960
if !recursive {
60-
log.Fatal("argument is a directory and recursive upload is disabled")
61+
utils.Fatalf("Argument is a directory and recursive upload is disabled")
6162
}
6263
if !wantManifest {
63-
log.Fatal("manifest is required for directory uploads")
64+
utils.Fatalf("Manifest is required for directory uploads")
6465
}
6566
mhash, err := client.uploadDirectory(file, defaultPath)
6667
if err != nil {
67-
log.Fatal(err)
68+
utils.Fatalf("Failed to upload directory: %v", err)
6869
}
6970
fmt.Println(mhash)
7071
return
7172
}
7273
entry, err := client.uploadFile(file, fi)
7374
if err != nil {
74-
log.Fatalln("upload failed:", err)
75+
utils.Fatalf("Upload failed: %v", err)
7576
}
7677
mroot := manifest{[]manifestEntry{entry}}
7778
if !wantManifest {
@@ -82,7 +83,7 @@ func upload(ctx *cli.Context) {
8283
}
8384
hash, err := client.uploadManifest(mroot)
8485
if err != nil {
85-
log.Fatalln("manifest upload failed:", err)
86+
utils.Fatalf("Manifest upload failed: %v", err)
8687
}
8788
fmt.Println(hash)
8889
}
@@ -173,7 +174,7 @@ func (c *client) uploadFileContent(file string, fi os.FileInfo) (string, error)
173174
return "", err
174175
}
175176
defer fd.Close()
176-
log.Printf("uploading file %s (%d bytes)", file, fi.Size())
177+
log.Info("Uploading swarm content", "file", file, "bytes", fi.Size())
177178
return c.postRaw("application/octet-stream", fi.Size(), fd)
178179
}
179180

@@ -182,7 +183,7 @@ func (c *client) uploadManifest(m manifest) (string, error) {
182183
if err != nil {
183184
panic(err)
184185
}
185-
log.Println("uploading manifest")
186+
log.Info("Uploading swarm manifest")
186187
return c.postRaw("application/json", int64(len(jsm)), ioutil.NopCloser(bytes.NewReader(jsm)))
187188
}
188189

@@ -192,7 +193,7 @@ func (c *client) uploadToManifest(mhash string, path string, fpath string, fi os
192193
return "", err
193194
}
194195
defer fd.Close()
195-
log.Printf("uploading file %s (%d bytes) and adding path %v", fpath, fi.Size(), path)
196+
log.Info("Uploading swarm content and path", "file", fpath, "bytes", fi.Size(), "path", path)
196197
req, err := http.NewRequest("PUT", c.api+"/bzz:/"+mhash+"/"+path, fd)
197198
if err != nil {
198199
return "", err

0 commit comments

Comments
 (0)