Skip to content

Commit c02e595

Browse files
authored
Merge pull request #1764 from 0chain/error-log-stderr
Error log stderr
2 parents d34e2f0 + 1b84a0e commit c02e595

File tree

9 files changed

+49
-39
lines changed

9 files changed

+49
-39
lines changed

core/client/http.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ package client
33
import (
44
"encoding/json"
55
"fmt"
6-
"log"
76
"net/http"
87
"net/url"
98
"sync"
109

1110
"github.com/0chain/errors"
1211
"github.com/0chain/gosdk/core/conf"
12+
"github.com/0chain/gosdk/core/logger"
1313
"github.com/0chain/gosdk/core/util"
1414
"github.com/shopspring/decimal"
1515
)
@@ -57,7 +57,7 @@ func MakeSCRestAPICallToSharder(scAddress string, relativePath string, params ma
5757
urlString := fmt.Sprintf("%v/%v%v%v", sharder, restApiUrl, scAddress, relativePath)
5858
urlObj, err := url.Parse(urlString)
5959
if err != nil {
60-
log.Println(err.Error())
60+
logger.GetLogger().Error("Error parsing URL: ", err.Error())
6161
return
6262
}
6363
q := urlObj.Query()
@@ -68,14 +68,14 @@ func MakeSCRestAPICallToSharder(scAddress string, relativePath string, params ma
6868

6969
req, err := util.NewHTTPGetRequest(urlObj.String())
7070
if err != nil {
71-
log.Println("Error creating request", err.Error())
71+
logger.GetLogger().Error("Error creating request: ", err.Error())
7272
return
7373
}
7474

7575
response, err := req.Get()
7676
if err != nil {
7777
nodeClient.sharders.Fail(sharder)
78-
log.Println("Error getting response", err.Error())
78+
logger.GetLogger().Error("Error getting response: ", err.Error())
7979
return
8080
}
8181

core/logger/logger.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ func (l *Logger) Init(lvl int, prefix string) {
4646
l.SetLevel(lvl)
4747
l.prefix = prefix
4848
l.logDebug = log.New(os.Stderr, prefix+": "+strDEBUG, log.Ldate|log.Ltime|log.Lmicroseconds|log.Lshortfile)
49-
l.logInfo = log.New(os.Stderr, prefix+": "+strINFO, log.Ldate|log.Ltime|log.Lmicroseconds|log.Lshortfile)
49+
l.logInfo = log.New(os.Stdout, prefix+": "+strINFO, log.Ldate|log.Ltime|log.Lmicroseconds|log.Lshortfile)
5050
l.logError = log.New(os.Stderr, prefix+": "+strERROR, log.Ldate|log.Ltime|log.Lmicroseconds|log.Lshortfile)
5151
l.logFatal = log.New(os.Stderr, prefix+": "+strFATAL, log.Ldate|log.Ltime|log.Lmicroseconds|log.Lshortfile)
5252
}
@@ -85,7 +85,7 @@ func (l *Logger) SetLogFile(logFile io.Writer, verbose bool) {
8585
fLogs := []io.Writer{logFile}
8686
if verbose {
8787
dLogs = append(dLogs, os.Stderr)
88-
iLogs = append(iLogs, os.Stderr)
88+
iLogs = append(iLogs, os.Stdout)
8989
eLogs = append(eLogs, os.Stderr)
9090
fLogs = append(fLogs, os.Stderr)
9191
}

zcnbridge/bridge.go

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ func (b *BridgeClient) AddEthereumAuthorizers(configDir string) {
159159
cfg.AddConfigPath(configDir)
160160
cfg.SetConfigName("authorizers")
161161
if err := cfg.ReadInConfig(); err != nil {
162-
fmt.Println(err)
162+
Logger.Error(err)
163163
return
164164
}
165165

@@ -168,32 +168,32 @@ func (b *BridgeClient) AddEthereumAuthorizers(configDir string) {
168168
for _, mnemonic := range mnemonics {
169169
wallet, err := hdw.NewFromMnemonic(mnemonic)
170170
if err != nil {
171-
fmt.Printf("failed to read mnemonic: %v", err)
171+
Logger.Error(fmt.Sprintf("failed to read mnemonic: %v", err))
172172
continue
173173
}
174174

175175
pathD := hdw.MustParseDerivationPath("m/44'/60'/0'/0/0")
176176
account, err := wallet.Derive(pathD, true)
177177
if err != nil {
178-
fmt.Println(err)
178+
Logger.Error(err)
179179
continue
180180
}
181181

182182
transaction, err := b.AddEthereumAuthorizer(context.TODO(), account.Address)
183183
if err != nil || transaction == nil {
184-
fmt.Printf("AddAuthorizer error: %v, Address: %s", err, account.Address.Hex())
184+
Logger.Error(fmt.Sprintf("AddAuthorizer error: %v, Address: %s", err, account.Address.Hex()))
185185
continue
186186
}
187187

188188
status, err := ConfirmEthereumTransaction(transaction.Hash().String(), 100, time.Second*10)
189189
if err != nil {
190-
fmt.Println(err)
190+
Logger.Error(err)
191191
}
192192

193193
if status == 1 {
194-
fmt.Printf("Authorizer has been added: %s\n", mnemonic)
194+
Logger.Info(fmt.Sprintf("Authorizer has been added: %s\n", mnemonic))
195195
} else {
196-
fmt.Printf("Authorizer has failed to be added: %s\n", mnemonic)
196+
Logger.Info(fmt.Sprintf("Authorizer has failed to be added: %s\n", mnemonic))
197197
}
198198
}
199199
}

zcnbridge/keystore.go

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ func DeleteAccount(homedir, address string) bool {
8484
})
8585

8686
if err != nil && wallet == nil {
87-
fmt.Printf("failed to find account %s, error: %s", address, err)
87+
Logger.Error(fmt.Sprintf("failed to find account %s, error: %s", address, err))
8888
return false
8989
}
9090

@@ -105,14 +105,16 @@ func AccountExists(homedir, address string) bool {
105105
})
106106

107107
if err != nil && wallet == nil {
108-
fmt.Printf("failed to find account %s, error: %s\n", address, err)
108+
Logger.Error(fmt.Sprintf("failed to find account %s, error: %s", address, err))
109109
return false
110110
}
111111

112112
status, _ := wallet.Status()
113113
url := wallet.URL()
114114

115-
fmt.Printf("Account exists. Status: %s, Path: %s\n", status, url)
115+
Logger.Info(
116+
fmt.Sprintf("Account exists. Status: %s, Path: %s", status, url),
117+
)
116118

117119
return true
118120
}
@@ -127,8 +129,10 @@ func CreateKeyStorage(homedir, password string) error {
127129
if err != nil {
128130
return errors.Wrap(err, "failed to create keystore")
129131
}
130-
fmt.Printf("Created account: %s", account.Address.Hex())
131-
132+
Logger.Info(
133+
fmt.Sprintf("Created account: %s", account.Address.Hex()),
134+
)
135+
132136
return nil
133137
}
134138

@@ -185,7 +189,7 @@ func ImportAccount(homedir, mnemonic, password string, accountAddrIndex ...Accou
185189

186190
acc, err := ks.Find(account)
187191
if err == nil {
188-
fmt.Printf("Account already exists: %s\nPath: %s\n\n", acc.Address.Hex(), acc.URL.Path)
192+
Logger.Error(fmt.Sprintf("Account already exists: %s\nPath: %s\n\n", acc.Address.Hex(), acc.URL.Path))
189193
return acc.Address.Hex(), nil
190194
}
191195

@@ -196,7 +200,7 @@ func ImportAccount(homedir, mnemonic, password string, accountAddrIndex ...Accou
196200
return "", errors.Wrap(err, "failed to get import private key")
197201
}
198202

199-
fmt.Printf("Imported account %s to path: %s\n", acc.Address.Hex(), acc.URL.Path)
200-
203+
Logger.Info(fmt.Sprintf("Imported account %s to path: %s\n", acc.Address.Hex(), acc.URL.Path))
204+
201205
return acc.Address.Hex(), nil
202206
}

zcnbridge/rest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ func getAuthorizers(active bool) ([]*AuthorizerNode, error) {
7373
}
7474

7575
if len(authorizers.Nodes) == 0 {
76-
fmt.Println("no authorizers found")
76+
Logger.Error("no authorizers found")
7777
return nil, err
7878
}
7979

zcncore/ethwallet_base.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"github.com/ethereum/go-ethereum/common/hexutil"
1010
"github.com/ethereum/go-ethereum/crypto"
1111
"golang.org/x/crypto/sha3"
12-
"log"
1312
"math"
1413
"math/big"
1514
"regexp"
@@ -251,16 +250,16 @@ func TransferEthTokens(fromPrivKey string, amountTokens, gasPrice int64) (string
251250
hash := sha3.NewLegacyKeccak256()
252251
hash.Write(transferFnSignature)
253252
methodID := hash.Sum(nil)[:4]
254-
fmt.Println(hexutil.Encode(methodID)) // 0xa9059cbb
253+
logging.Info(hexutil.Encode(methodID)) // 0xa9059cbb
255254

256255
paddedAddress := common.LeftPadBytes(toAddress.Bytes(), 32)
257-
fmt.Println(hexutil.Encode(paddedAddress)) // 0x0000000000000000000000004592d8f8d7b001e72cb26a73e4fa1806a51ac79d
256+
logging.Info(hexutil.Encode(paddedAddress)) // 0x0000000000000000000000004592d8f8d7b001e72cb26a73e4fa1806a51ac79d
258257

259258
amount := new(big.Int)
260259
amount.SetInt64(amountTokens)
261260

262261
paddedAmount := common.LeftPadBytes(amount.Bytes(), 32)
263-
fmt.Println(hexutil.Encode(paddedAmount)) // 0x00000000000000000000000000000000000000000000003635c9adc5dea00000
262+
logging.Info(hexutil.Encode(paddedAmount)) // 0x00000000000000000000000000000000000000000000003635c9adc5dea00000
264263

265264
var data []byte
266265
data = append(data, methodID...)
@@ -272,7 +271,7 @@ func TransferEthTokens(fromPrivKey string, amountTokens, gasPrice int64) (string
272271
Data: data,
273272
})
274273
if err != nil {
275-
log.Fatal(err)
274+
logging.Fatal(err)
276275
}
277276

278277
txData := &types.LegacyTx{

zcncore/get_data.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ func getWallet(walletStr string) (*zcncrypto.Wallet, error) {
177177
var w zcncrypto.Wallet
178178
err := json.Unmarshal([]byte(walletStr), &w)
179179
if err != nil {
180-
fmt.Printf("error while parsing wallet string.\n%v\n", err)
180+
logging.Error(fmt.Sprintf("error while parsing wallet string.\n%v\n", err))
181181
return nil, err
182182
}
183183

znft/keystore.go

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func DeleteAccount(homedir, address string) bool {
3030
})
3131

3232
if err != nil && wallet == nil {
33-
fmt.Printf("failed to find account %s, error: %s", address, err)
33+
Logger.Error(fmt.Sprintf("failed to find account %s, error: %s", address, err))``
3434
return false
3535
}
3636

@@ -54,15 +54,17 @@ func AccountExists(homedir, address string) bool {
5454
})
5555

5656
if err != nil && wallet == nil {
57-
fmt.Printf("failed to find account %s, error: %s\n", address, err)
57+
Logger.Error(fmt.Sprintf("failed to find account %s, error: %s", address, err))
5858
return false
5959
}
6060

6161
status, _ := wallet.Status()
6262
url := wallet.URL()
6363

64-
fmt.Printf("Account exists. Status: %s, Path: %s\n", status, url)
65-
64+
Logger.Info(
65+
fmt.Sprintf("Account exists. Status: %s, Path: %s", status, url),
66+
)
67+
6668
return true
6769
}
6870

@@ -74,8 +76,10 @@ func CreateKeyStorage(homedir, password string) error {
7476
if err != nil {
7577
return errors.Wrap(err, "failed to create keystore")
7678
}
77-
fmt.Printf("Created account: %s", account.Address.Hex())
78-
79+
Logger.Info(
80+
fmt.Sprintf("Created account: %s", account.Address.Hex()),
81+
)
82+
7983
return nil
8084
}
8185

@@ -140,7 +144,9 @@ func ImportAccount(homedir, mnemonic, password string) (string, error) {
140144
// 3. Find key
141145
acc, err := ks.Find(account)
142146
if err == nil {
143-
fmt.Printf("Account already exists: %s\nPath: %s\n\n", acc.Address.Hex(), acc.URL.Path)
147+
Logger.Info(
148+
fmt.Sprintf("Account already exists: %s\nPath: %s\n\n", acc.Address.Hex(), acc.URL.Path),
149+
)
144150
return acc.Address.Hex(), nil
145151
}
146152

@@ -150,7 +156,9 @@ func ImportAccount(homedir, mnemonic, password string) (string, error) {
150156
return "", errors.Wrap(err, "failed to get import private key")
151157
}
152158

153-
fmt.Printf("Imported account %s to path: %s\n", acc.Address.Hex(), acc.URL.Path)
154-
159+
Logger.Info(
160+
fmt.Sprintf("Imported account %s to path: %s\n", acc.Address.Hex(), acc.URL.Path),
161+
)
162+
155163
return acc.Address.Hex(), nil
156164
}

znft/znft.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,8 +59,7 @@ func GetConfigDir() string {
5959
var configDir string
6060
home, err := os.UserHomeDir()
6161
if err != nil {
62-
fmt.Println(err)
63-
os.Exit(1)
62+
logger.GetLogger().Fatal(err)
6463
}
6564
configDir = home + "/.zcn"
6665
return configDir

0 commit comments

Comments
 (0)