[logging] use GlogHandler as the base handler

pull/844/head
Peter Chung 6 years ago
parent 01f69f2e7a
commit b632fd277a
  1. 17
      cmd/bootnode/main.go
  2. 4
      cmd/client/txgen/main.go
  3. 26
      cmd/harmony/main.go
  4. 58
      internal/utils/singleton.go

@ -32,17 +32,14 @@ func printVersion(me string) {
os.Exit(0) os.Exit(0)
} }
func loggingInit(logFolder, ip, port string, verbosity log.Lvl) { func initLogFile(logFolder, ip, port string) {
// Setup a logger to stdout and log file. // Setup a logger to stdout and log file.
if err := os.MkdirAll(logFolder, 0755); err != nil { if err := os.MkdirAll(logFolder, 0755); err != nil {
panic(err) panic(err)
} }
logFileName := fmt.Sprintf("./%v/bootnode-%v-%v.log", logFolder, ip, port) logFileName := fmt.Sprintf("./%v/bootnode-%v-%v.log", logFolder, ip, port)
h := log.LvlFilterHandler(verbosity, log.MultiHandler( fileHandler := log.Must.FileHandler(logFileName, log.JSONFormat()) // Log to file
log.StreamHandler(os.Stdout, log.TerminalFormat(false)), utils.AddLogHandler(fileHandler)
log.Must.FileHandler(logFileName, log.JSONFormat()), // Log to file
))
log.Root().SetHandler(h)
} }
func main() { func main() {
@ -60,11 +57,9 @@ func main() {
} }
// Logging setup // Logging setup
utils.SetPortAndIP(*port, *ip) utils.SetLogContext(*port, *ip)
utils.SetVerbosity(log.Lvl(*verbosity)) utils.SetLogVerbosity(log.Lvl(*verbosity))
initLogFile(*logFolder, *ip, *port)
// Init logging.
loggingInit(*logFolder, *ip, *port, log.Lvl(*verbosity))
privKey, _, err := utils.LoadKeyFromFile(*keyFile) privKey, _, err := utils.LoadKeyFromFile(*keyFile)
if err != nil { if err != nil {

@ -128,8 +128,8 @@ func main() {
// Add GOMAXPROCS to achieve max performance. // Add GOMAXPROCS to achieve max performance.
runtime.GOMAXPROCS(1024) runtime.GOMAXPROCS(1024)
// Logging setup // Logging setup
utils.SetPortAndIP(*port, *ip) utils.SetLogContext(*port, *ip)
utils.SetVerbosity(log.Lvl(*verbosity)) utils.SetLogVerbosity(log.Lvl(*verbosity))
if len(utils.BootNodes) == 0 { if len(utils.BootNodes) == 0 {
bootNodeAddrs, err := utils.StringsToAddrs(utils.DefaultBootNodeAddrStrings) bootNodeAddrs, err := utils.StringsToAddrs(utils.DefaultBootNodeAddrStrings)
if err != nil { if err != nil {

@ -58,21 +58,23 @@ func printVersion(me string) {
os.Exit(0) os.Exit(0)
} }
func loggingInit(logFolder, role, ip, port string, onlyLogTps bool, verbosity log.Lvl) { func initLogFile(logFolder, role, ip, port string, onlyLogTps bool) {
// Setup a logger to stdout and log file. // Setup a logger to stdout and log file.
if err := os.MkdirAll(logFolder, 0755); err != nil {
panic(err)
}
logFileName := fmt.Sprintf("./%v/%s-%v-%v.log", logFolder, role, ip, port) logFileName := fmt.Sprintf("./%v/%s-%v-%v.log", logFolder, role, ip, port)
h := log.LvlFilterHandler(verbosity, log.MultiHandler( fileHandler := log.Must.FileHandler(logFileName, log.JSONFormat())
log.StreamHandler(os.Stdout, log.TerminalFormat(false)), utils.AddLogHandler(fileHandler)
log.Must.FileHandler(logFileName, log.JSONFormat()), // Log to file
))
if onlyLogTps { if onlyLogTps {
h = log.MatchFilterHandler("msg", "TPS Report", h) matchFilterHandler := log.MatchFilterHandler("msg", "TPS Report", utils.GetLogInstance().GetHandler())
utils.GetLogInstance().SetHandler(matchFilterHandler)
} }
log.Root().SetHandler(h)
} }
var ( var (
ip = flag.String("ip", "127.0.0.1", "IP of the node") ip = flag.String("ip", "127.0.0.1", "ip of the node")
port = flag.String("port", "9000", "port of the node.") port = flag.String("port", "9000", "port of the node.")
logFolder = flag.String("log_folder", "latest", "the folder collecting the logs of this execution") logFolder = flag.String("log_folder", "latest", "the folder collecting the logs of this execution")
freshDB = flag.Bool("fresh_db", false, "true means the existing disk based db will be removed") freshDB = flag.Bool("fresh_db", false, "true means the existing disk based db will be removed")
@ -372,13 +374,15 @@ func main() {
flag.Var(&utils.BootNodes, "bootnodes", "a list of bootnode multiaddress (delimited by ,)") flag.Var(&utils.BootNodes, "bootnodes", "a list of bootnode multiaddress (delimited by ,)")
flag.Parse() flag.Parse()
// Configure log parameters
utils.SetLogContext(*port, *ip)
utils.SetLogVerbosity(log.Lvl(*verbosity))
initSetup() initSetup()
var currentNode *node.Node var currentNode *node.Node
var consensus *consensus.Consensus var consensus *consensus.Consensus
nodeConfig := createGlobalConfig() nodeConfig := createGlobalConfig()
initLogFile(*logFolder, nodeConfig.StringRole, *ip, *port, *onlyLogTps)
// Init logging.
loggingInit(*logFolder, nodeConfig.StringRole, *ip, *port, *onlyLogTps, log.Lvl(*verbosity))
// Start Profiler for leader if profile argument is on // Start Profiler for leader if profile argument is on
if nodeConfig.StringRole == "leader" && (*profile || *metricsReportURL != "") { if nodeConfig.StringRole == "leader" && (*profile || *metricsReportURL != "") {

@ -3,6 +3,7 @@
package utils package utils
import ( import (
"io"
"os" "os"
"sync" "sync"
"sync/atomic" "sync/atomic"
@ -10,23 +11,43 @@ import (
"github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/log"
) )
// Global Port and IP for logging.
var ( var (
Port string // Validator ID
IP string validatorIDInstance *UniqueValidatorID
Verbosity = log.LvlInfo onceForUniqueValidatorID sync.Once
// Global port and ip for logging.
port string
ip string
// Logging
logInstance log.Logger
glogger *log.GlogHandler // top-level handler
logHandlers []log.Handler // sub handlers of glogger
logVerbosity log.Lvl
onceForLog sync.Once
) )
// SetPortAndIP used to print out loggings of node with Port and IP. // SetLogContext used to print out loggings of node with port and ip.
// Every instance (node, txgen, etc..) needs to set this for logging. // Every instance (node, txgen, etc..) needs to set this for logging.
func SetPortAndIP(port, ip string) { func SetLogContext(_port, _ip string) {
Port = port port = _port
IP = ip ip = _ip
} }
// SetVerbosity specifies the verbosity of global logger // SetLogVerbosity specifies the verbosity of global logger
func SetVerbosity(verbosity log.Lvl) { func SetLogVerbosity(verbosity log.Lvl) {
Verbosity = verbosity logVerbosity = verbosity
if glogger != nil {
glogger.Verbosity(logVerbosity)
}
}
// AddLogHandler add a log handler
func AddLogHandler(handler log.Handler) {
logHandlers = append(logHandlers, handler)
if glogger != nil {
multiHandler := log.MultiHandler(logHandlers...)
glogger.SetHandler(multiHandler)
}
} }
// UniqueValidatorID defines the structure of unique validator ID // UniqueValidatorID defines the structure of unique validator ID
@ -34,11 +55,6 @@ type UniqueValidatorID struct {
uniqueID uint32 uniqueID uint32
} }
var validatorIDInstance *UniqueValidatorID
var logInstance log.Logger
var onceForUniqueValidatorID sync.Once
var onceForLog sync.Once
// GetUniqueValidatorIDInstance returns a singleton instance // GetUniqueValidatorIDInstance returns a singleton instance
func GetUniqueValidatorIDInstance() *UniqueValidatorID { func GetUniqueValidatorIDInstance() *UniqueValidatorID {
onceForUniqueValidatorID.Do(func() { onceForUniqueValidatorID.Do(func() {
@ -57,8 +73,14 @@ func (s *UniqueValidatorID) GetUniqueID() uint32 {
// GetLogInstance returns logging singleton. // GetLogInstance returns logging singleton.
func GetLogInstance() log.Logger { func GetLogInstance() log.Logger {
onceForLog.Do(func() { onceForLog.Do(func() {
logInstance = log.New("port", Port, "ip", IP) ostream := log.StreamHandler(io.Writer(os.Stdout), log.TerminalFormat(false))
logInstance.SetHandler(log.LvlFilterHandler(Verbosity, log.StreamHandler(os.Stdout, log.TerminalFormat(false)))) logHandlers = append(logHandlers, ostream)
multiHandler := log.MultiHandler(logHandlers...)
glogger = log.NewGlogHandler(multiHandler)
glogger.Verbosity(logVerbosity)
logInstance = log.New("port", port, "ip", ip)
logInstance.SetHandler(glogger)
log.Root().SetHandler(glogger)
}) })
return logInstance return logInstance
} }

Loading…
Cancel
Save