Move log file rotation StreamHandler setup to utils

pull/1173/head
Kai Lee 5 years ago
parent f334a1d8fa
commit a856b1ae04
  1. 18
      cmd/bootnode/main.go
  2. 40
      cmd/harmony/main.go
  3. 18
      internal/utils/singleton.go

@ -32,19 +32,6 @@ func printVersion(me string) {
os.Exit(0)
}
func initLogFile(logFolder, ip, port string, logMaxSize int) {
// Setup a logger to stdout and log file.
if err := os.MkdirAll(logFolder, 0755); err != nil {
panic(err)
}
utils.AddLogHandler(
log.StreamHandler(&lumberjack.Logger{
Filename: fmt.Sprintf("./%v/bootnode-%v-%v.log", logFolder, ip, port),
MaxSize: logMaxSize,
Compress: true,
}, log.JSONFormat()))
}
func main() {
ip := flag.String("ip", "127.0.0.1", "IP of the node")
port := flag.String("port", "9876", "port of the node.")
@ -64,7 +51,10 @@ func main() {
// Logging setup
utils.SetLogContext(*port, *ip)
utils.SetLogVerbosity(log.Lvl(*verbosity))
initLogFile(*logFolder, *ip, *port, *logMaxSize)
filename := fmt.Sprintf("%v/bootnode-%v-%v.log", *logFolder, *ip, *port)
if err := utils.AddLogFile(filename, *logMaxSize); err != nil {
panic(err)
}
privKey, _, err := utils.LoadKeyFromFile(*keyFile)
if err != nil {

@ -30,7 +30,6 @@ import (
"github.com/harmony-one/harmony/node"
"github.com/harmony-one/harmony/p2p"
"github.com/harmony-one/harmony/p2p/p2pimpl"
"github.com/natefinch/lumberjack"
)
var (
@ -62,24 +61,6 @@ func printVersion() {
os.Exit(0)
}
func initLogFile(logFolder, role, ip, port string, onlyLogTps bool, logMaxSize int) {
// Setup a logger to stdout and log file.
if err := os.MkdirAll(logFolder, 0755); err != nil {
panic(err)
}
utils.AddLogHandler(
log.StreamHandler(&lumberjack.Logger{
Filename: fmt.Sprintf("./%v/%s-%v-%v.log", logFolder, role, ip, port),
MaxSize: logMaxSize,
Compress: true,
}, log.JSONFormat()))
if onlyLogTps {
matchFilterHandler := log.MatchFilterHandler("msg", "TPS Report", utils.GetLogInstance().GetHandler())
utils.GetLogInstance().SetHandler(matchFilterHandler)
}
}
var (
ip = flag.String("ip", "127.0.0.1", "ip of the node")
port = flag.String("port", "9000", "port of the node.")
@ -157,10 +138,6 @@ func initSetup() {
// Setup mem profiling.
memprofiling.GetMemProfiling().Config()
// Logging setup
utils.SetLogContext(*port, *ip)
utils.SetLogVerbosity(log.Lvl(*verbosity))
// Set default keystore Dir
hmykey.DefaultKeyStoreDir = *keystoreDir
@ -459,13 +436,20 @@ func main() {
}
blsPassphrase = passphrase
// Configure log parameters
utils.SetLogContext(*port, *ip)
utils.SetLogVerbosity(log.Lvl(*verbosity))
initSetup()
nodeConfig := createGlobalConfig()
initLogFile(*logFolder, nodeConfig.StringRole, *ip, *port, *onlyLogTps, *logMaxSize)
// Logging setup
utils.SetLogContext(*port, *ip)
utils.SetLogVerbosity(log.Lvl(*verbosity))
filename := fmt.Sprintf("%v/%s-%v-%v.log", *logFolder, nodeConfig.StringRole, *ip, *port)
if err := utils.AddLogFile(filename, *logMaxSize); err != nil {
panic(err)
}
if *onlyLogTps {
matchFilterHandler := log.MatchFilterHandler("msg", "TPS Report", utils.GetLogInstance().GetHandler())
utils.GetLogInstance().SetHandler(matchFilterHandler)
}
// Start Profiler for leader if profile argument is on
if nodeConfig.StringRole == "leader" && (*profile || *metricsReportURL != "") {

@ -5,10 +5,12 @@ package utils
import (
"io"
"os"
"path"
"sync"
"sync/atomic"
"github.com/ethereum/go-ethereum/log"
"github.com/natefinch/lumberjack"
)
var (
@ -41,6 +43,22 @@ func SetLogVerbosity(verbosity log.Lvl) {
}
}
// AddLogFile creates a StreamHandler that outputs JSON logs
// into rotating files with specified max file size
func AddLogFile(filepath string, maxSize int) error {
if err := os.MkdirAll(path.Dir(filepath), 0755); err != nil {
return err
}
AddLogHandler(log.StreamHandler(&lumberjack.Logger{
Filename: filepath,
MaxSize: maxSize,
Compress: true,
}, log.JSONFormat()))
return nil
}
// AddLogHandler add a log handler
func AddLogHandler(handler log.Handler) {
logHandlers = append(logHandlers, handler)

Loading…
Cancel
Save