diff --git a/core/client/init_node.go b/core/client/init_node.go index f7a2ad901..444b33834 100644 --- a/core/client/init_node.go +++ b/core/client/init_node.go @@ -18,14 +18,10 @@ import ( ) var ( - logging logger.Logger + logging = logger.GetLogger() nodeClient *Node ) -func init() { - logging.Init(logger.DEBUG, "0chain-core") -} - // Node Maintains central states of SDK (client's context, network). // Initialized through [Init] function. // Use client.GetNode() to get its instance after Init is called. diff --git a/core/logger/logger.go b/core/logger/logger.go index fbe517017..4af6e8692 100644 --- a/core/logger/logger.go +++ b/core/logger/logger.go @@ -3,6 +3,8 @@ package logger import ( "fmt" + "github.com/0chain/gosdk/core/version" + "gopkg.in/natefinch/lumberjack.v2" "io" "log" "os" @@ -142,3 +144,39 @@ func (l *Logger) Close() { } } } + +// Initialize common logger +var logging Logger + +func GetLogger() *Logger { + return &logging +} + +func CloseLog() { + logging.Close() +} + +func init() { + logging.Init(DEBUG, "0chain-core-sdk") +} + +// SetLogLevel set the log level. +// lvl - 0 disabled; higher number (upto 4) more verbosity +func SetLogLevel(lvl int) { + logging.SetLevel(lvl) +} + +// SetLogFile - sets file path to write log +// verbose - true - console output; false - no console output +func SetLogFile(logFile string, verbose bool) { + ioWriter := &lumberjack.Logger{ + Filename: logFile, + MaxSize: 100, // MB + MaxBackups: 5, // number of backups + MaxAge: 28, //days + LocalTime: false, + Compress: false, // disabled by default + } + logging.SetLogFile(ioWriter, verbose) + logging.Info("******* Wallet SDK Version:", version.VERSIONSTR, " ******* (SetLogFile)") +} diff --git a/core/transaction/entity.go b/core/transaction/entity.go index eee839b5a..995a45954 100644 --- a/core/transaction/entity.go +++ b/core/transaction/entity.go @@ -23,7 +23,7 @@ import ( lru "github.com/hashicorp/golang-lru" ) -var Logger logger.Logger +var Logger = logger.GetLogger() const STORAGE_SCADDRESS = "6dba10422e368813802877a85039d3985d96760ed844092319743fb3a76712d7" const MINERSC_SCADDRESS = "6dba10422e368813802877a85039d3985d96760ed844092319743fb3a76712d9" @@ -270,7 +270,7 @@ func (t *Transaction) VerifySigWith(pubkey string, verifyHandler VerifyFunc) (bo } func SendTransactionSync(txn *Transaction, miners []string) error { - const requestTimeout = 30 * time.Second // Timeout for each request + const requestTimeout = 3 * time.Second // Timeout for each request fails := make(chan error, len(miners)) var wg sync.WaitGroup diff --git a/zboxcore/logger/logger.go b/zboxcore/logger/logger.go index 37a89ee44..64134eb35 100644 --- a/zboxcore/logger/logger.go +++ b/zboxcore/logger/logger.go @@ -3,11 +3,5 @@ package logger import "github.com/0chain/gosdk/core/logger" -var defaultLogLevel = logger.DEBUG - // Logger global logger instance -var Logger logger.Logger - -func init() { - Logger.Init(defaultLogLevel, "0box-sdk") -} +var Logger = logger.GetLogger() diff --git a/zboxcore/sdk/sdk.go b/zboxcore/sdk/sdk.go index 260cd57cc..adb6eddbe 100644 --- a/zboxcore/sdk/sdk.go +++ b/zboxcore/sdk/sdk.go @@ -97,7 +97,7 @@ func SetLogFile(logFile string, verbose bool) { // GetLogger retrieves logger instance func GetLogger() *logger.Logger { - return &l.Logger + return l.Logger } type BackPool struct { diff --git a/zcncore/wallet_base.go b/zcncore/wallet_base.go index f4ead5968..89111ee59 100644 --- a/zcncore/wallet_base.go +++ b/zcncore/wallet_base.go @@ -79,11 +79,11 @@ const ( ) var defaultLogLevel = logger.DEBUG -var logging logger.Logger +var logging = logger.GetLogger() // GetLogger returns the logger instance func GetLogger() *logger.Logger { - return &logging + return logging } // CloseLog closes log file @@ -141,10 +141,6 @@ type AuthCallback interface { OnSetupComplete(status int, err string) } -func init() { - logging.Init(defaultLogLevel, "0chain-core-sdk") -} - func checkSdkInit() error { _, err := client.GetNode() if err != nil {