2017-05-16 12:09:52 +00:00
|
|
|
package common_test
|
2017-03-18 19:00:40 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2017-05-02 14:30:11 +00:00
|
|
|
"strings"
|
2017-03-18 19:00:40 +00:00
|
|
|
"testing"
|
|
|
|
|
2017-05-02 14:30:11 +00:00
|
|
|
"github.com/ethereum/go-ethereum/log"
|
2017-05-16 12:09:52 +00:00
|
|
|
"github.com/status-im/status-go/geth/common"
|
2017-05-25 13:14:52 +00:00
|
|
|
"github.com/status-im/status-go/geth/params"
|
2017-03-18 19:00:40 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestLogger(t *testing.T) {
|
|
|
|
tmpDir, err := ioutil.TempDir(os.TempDir(), "geth-logger-tests")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-05-02 14:30:11 +00:00
|
|
|
//defer os.RemoveAll(tmpDir)
|
2017-03-18 19:00:40 +00:00
|
|
|
|
2017-05-16 03:24:56 +00:00
|
|
|
nodeConfig, err := params.NewNodeConfig(tmpDir, params.RopstenNetworkID, true)
|
2017-03-18 19:00:40 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("cannot create config object")
|
|
|
|
}
|
2017-05-16 12:09:52 +00:00
|
|
|
nodeLogger, err := common.SetupLogger(nodeConfig)
|
2017-03-18 19:00:40 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("cannot create logger object")
|
|
|
|
}
|
|
|
|
if nodeLogger != nil {
|
|
|
|
t.Fatalf("logger is not empty (while logs are disabled): %v", nodeLogger)
|
|
|
|
}
|
|
|
|
|
|
|
|
nodeConfig.LogEnabled = true
|
2017-05-02 14:30:11 +00:00
|
|
|
nodeConfig.LogToStderr = false // just capture logs to file
|
2017-05-16 12:09:52 +00:00
|
|
|
nodeLogger, err = common.SetupLogger(nodeConfig)
|
2017-03-18 19:00:40 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal("cannot create logger object")
|
|
|
|
}
|
|
|
|
if nodeLogger == nil {
|
|
|
|
t.Fatal("logger is empty (while logs are enabled)")
|
|
|
|
}
|
|
|
|
|
2017-05-02 14:30:11 +00:00
|
|
|
validateLogText := func(expectedLogText string) {
|
|
|
|
logFilePath := filepath.Join(nodeConfig.DataDir, nodeConfig.LogFile)
|
|
|
|
logBytes, err := ioutil.ReadFile(logFilePath)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
2017-03-18 19:00:40 +00:00
|
|
|
}
|
2017-05-02 14:30:11 +00:00
|
|
|
logText := string(logBytes)
|
|
|
|
logText = strings.Trim(logText, "\n")
|
|
|
|
logText = logText[len(logText)-len(expectedLogText):] // as logs can be prepended with log info
|
2017-03-18 19:00:40 +00:00
|
|
|
|
2017-05-02 14:30:11 +00:00
|
|
|
if expectedLogText != logText {
|
|
|
|
t.Fatalf("invalid log, expected: [%s], got: [%s]", expectedLogText, string(logText))
|
|
|
|
} else {
|
|
|
|
t.Logf("log match found, expected: [%s], got: [%s]", expectedLogText, string(logText))
|
2017-03-18 19:00:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-02 14:30:11 +00:00
|
|
|
// sample log message
|
|
|
|
log.Info("use log package")
|
|
|
|
validateLogText(`msg="use log package"`)
|
2017-03-18 19:00:40 +00:00
|
|
|
|
2017-05-02 14:30:11 +00:00
|
|
|
// log using DEBUG log level (with appropriate level set)
|
|
|
|
nodeLogger.SetV("DEBUG")
|
|
|
|
log.Info("logged DEBUG log level message")
|
|
|
|
validateLogText(`msg="logged DEBUG log level message"`)
|
2017-03-18 19:00:40 +00:00
|
|
|
|
2017-05-02 14:30:11 +00:00
|
|
|
// log using DEBUG log level (with appropriate level set)
|
|
|
|
nodeLogger.SetV("INFO")
|
|
|
|
log.Info("logged INFO log level message")
|
|
|
|
validateLogText(`msg="logged INFO log level message"`)
|
|
|
|
log.Debug("logged DEBUG log level message")
|
|
|
|
validateLogText(`msg="logged INFO log level message"`) // debug level message is NOT logged
|
2017-03-18 19:00:40 +00:00
|
|
|
|
2017-05-02 14:30:11 +00:00
|
|
|
// stop logger and see if os.Stderr and gethlog continue functioning
|
2017-05-03 14:24:48 +00:00
|
|
|
if err = nodeLogger.Stop(); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2017-03-18 19:00:40 +00:00
|
|
|
|
2017-05-02 14:30:11 +00:00
|
|
|
log.Info("logging message: this message happens after custom logger has been stopped")
|
2017-03-18 19:00:40 +00:00
|
|
|
}
|