Enable logs in tests (#852)
Pass `-log` option to `go test` in order to change the log level. By default, it's `INFO`.
This commit is contained in:
parent
56292fb505
commit
5f075eeaba
|
@ -11,6 +11,8 @@ import (
|
||||||
"runtime"
|
"runtime"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
"github.com/status-im/status-go/logutils"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
"github.com/ethereum/go-ethereum/p2p/discv5"
|
"github.com/ethereum/go-ethereum/p2p/discv5"
|
||||||
"github.com/status-im/status-go/cmd/statusd/debug"
|
"github.com/status-im/status-go/cmd/statusd/debug"
|
||||||
|
@ -83,32 +85,6 @@ var (
|
||||||
// All general log messages in this package should be routed through this logger.
|
// All general log messages in this package should be routed through this logger.
|
||||||
var logger = log.New("package", "status-go/cmd/statusd")
|
var logger = log.New("package", "status-go/cmd/statusd")
|
||||||
|
|
||||||
func enhanceLogger(logger *log.Logger, config *params.NodeConfig) error {
|
|
||||||
var (
|
|
||||||
handler log.Handler
|
|
||||||
err error
|
|
||||||
)
|
|
||||||
|
|
||||||
if config.LogFile != "" {
|
|
||||||
handler, err = log.FileHandler(config.LogFile, log.LogfmtFormat())
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
handler = log.StreamHandler(os.Stderr, log.TerminalFormat(true))
|
|
||||||
}
|
|
||||||
|
|
||||||
level, err := log.LvlFromString(strings.ToLower(config.LogLevel))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
filteredHandler := log.LvlFilterHandler(level, handler)
|
|
||||||
log.Root().SetHandler(filteredHandler)
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
flag.Var(&searchTopics, "topic.search", "Topic that will be searched in discovery v5, e.g (mailserver=1,1)")
|
flag.Var(&searchTopics, "topic.search", "Topic that will be searched in discovery v5, e.g (mailserver=1,1)")
|
||||||
flag.Var(®isterTopics, "topic.register", "Topic that will be registered using discovery v5.")
|
flag.Var(®isterTopics, "topic.register", "Topic that will be registered using discovery v5.")
|
||||||
|
@ -126,7 +102,7 @@ func main() {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
if err := enhanceLogger(&logger, config); err != nil {
|
if err := logutils.OverrideRootLog(config.LogLevel, config.LogFile, true); err != nil {
|
||||||
stdlog.Fatalf("Error initializing logger: %s", err)
|
stdlog.Fatalf("Error initializing logger: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -9,6 +9,7 @@ import (
|
||||||
"github.com/NaySoftware/go-fcm"
|
"github.com/NaySoftware/go-fcm"
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
"github.com/status-im/status-go/geth/params"
|
"github.com/status-im/status-go/geth/params"
|
||||||
|
"github.com/status-im/status-go/logutils"
|
||||||
"github.com/status-im/status-go/profiling"
|
"github.com/status-im/status-go/profiling"
|
||||||
"gopkg.in/go-playground/validator.v9"
|
"gopkg.in/go-playground/validator.v9"
|
||||||
)
|
)
|
||||||
|
@ -32,43 +33,18 @@ func GenerateConfig(datadir *C.char, networkID C.int, devMode C.int) *C.char {
|
||||||
return C.CString(string(outBytes))
|
return C.CString(string(outBytes))
|
||||||
}
|
}
|
||||||
|
|
||||||
func initLogger(config *params.NodeConfig) {
|
|
||||||
var (
|
|
||||||
handler log.Handler
|
|
||||||
err error
|
|
||||||
)
|
|
||||||
|
|
||||||
if config.LogFile != "" {
|
|
||||||
handler, err = log.FileHandler(config.LogFile, log.LogfmtFormat())
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
handler = log.StreamHandler(os.Stderr, log.TerminalFormat(true))
|
|
||||||
}
|
|
||||||
|
|
||||||
// level, err := log.LvlFromString(strings.ToLower(config.LogLevel))
|
|
||||||
level, err := log.LvlFromString("trace")
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
filteredHandler := log.LvlFilterHandler(level, handler)
|
|
||||||
log.Root().SetHandler(filteredHandler)
|
|
||||||
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
//StartNode - start Status node
|
//StartNode - start Status node
|
||||||
//export StartNode
|
//export StartNode
|
||||||
func StartNode(configJSON *C.char) *C.char {
|
func StartNode(configJSON *C.char) *C.char {
|
||||||
config, err := params.LoadNodeConfig(C.GoString(configJSON))
|
config, err := params.LoadNodeConfig(C.GoString(configJSON))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return makeJSONResponse(err)
|
return makeJSONResponse(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
initLogger(config)
|
if err := logutils.OverrideRootLog(config.LogLevel, config.LogFile, false); err != nil {
|
||||||
|
return makeJSONResponse(err)
|
||||||
|
}
|
||||||
|
|
||||||
statusAPI.StartNodeAsync(config)
|
statusAPI.StartNodeAsync(config)
|
||||||
|
|
||||||
return makeJSONResponse(nil)
|
return makeJSONResponse(nil)
|
||||||
|
|
|
@ -0,0 +1,40 @@
|
||||||
|
package logutils
|
||||||
|
|
||||||
|
import (
|
||||||
|
"os"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/ethereum/go-ethereum/log"
|
||||||
|
)
|
||||||
|
|
||||||
|
// OverrideRootLog overrides root logger with file handler, if defined,
|
||||||
|
// and log level (defaults to INFO).
|
||||||
|
func OverrideRootLog(levelStr, logFile string, terminal bool) error {
|
||||||
|
var (
|
||||||
|
handler log.Handler
|
||||||
|
err error
|
||||||
|
)
|
||||||
|
|
||||||
|
if logFile != "" {
|
||||||
|
handler, err = log.FileHandler(logFile, log.LogfmtFormat())
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
handler = log.StreamHandler(os.Stderr, log.TerminalFormat(terminal))
|
||||||
|
}
|
||||||
|
|
||||||
|
if levelStr == "" {
|
||||||
|
levelStr = "INFO"
|
||||||
|
}
|
||||||
|
|
||||||
|
level, err := log.LvlFromString(strings.ToLower(levelStr))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
filteredHandler := log.LvlFilterHandler(level, handler)
|
||||||
|
log.Root().SetHandler(filteredHandler)
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
|
@ -17,6 +17,8 @@ import (
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/status-im/status-go/logutils"
|
||||||
|
|
||||||
"github.com/ethereum/go-ethereum/log"
|
"github.com/ethereum/go-ethereum/log"
|
||||||
"github.com/status-im/status-go/geth/params"
|
"github.com/status-im/status-go/geth/params"
|
||||||
"github.com/status-im/status-go/static"
|
"github.com/status-im/status-go/static"
|
||||||
|
@ -26,7 +28,10 @@ import (
|
||||||
|
|
||||||
var (
|
var (
|
||||||
networkSelected = flag.String("network", "statuschain", "-network=NETWORKID or -network=NETWORKNAME to select network used for tests")
|
networkSelected = flag.String("network", "statuschain", "-network=NETWORKID or -network=NETWORKNAME to select network used for tests")
|
||||||
|
logLevel = flag.String("log", "INFO", `Log level, one of: "ERROR", "WARN", "INFO", "DEBUG", and "TRACE"`)
|
||||||
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
// ErrNoRemoteURL is returned when network id has no associated url.
|
// ErrNoRemoteURL is returned when network id has no associated url.
|
||||||
ErrNoRemoteURL = errors.New("network id requires a remote URL")
|
ErrNoRemoteURL = errors.New("network id requires a remote URL")
|
||||||
|
|
||||||
|
@ -64,6 +69,11 @@ func init() {
|
||||||
|
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
|
// set up logger
|
||||||
|
if err := logutils.OverrideRootLog(*logLevel, "", true); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
|
||||||
// setup root directory
|
// setup root directory
|
||||||
const pathSeparator = string(os.PathSeparator)
|
const pathSeparator = string(os.PathSeparator)
|
||||||
RootDir = filepath.Dir(pwd)
|
RootDir = filepath.Dir(pwd)
|
||||||
|
|
Loading…
Reference in New Issue