diff --git a/cmd/utils/flags.go b/cmd/utils/flags.go index 21a517f4ca..807f2ecaa9 100644 --- a/cmd/utils/flags.go +++ b/cmd/utils/flags.go @@ -61,6 +61,7 @@ var GlobalFlags = []Flag{ DataDirFlag, AncientDirFlag, LogLevelFlag, + LogSizeFlag, } var NodeFlags = []Flag{ @@ -187,6 +188,12 @@ var ( Value: "info", Usage: "log level (trace, debug, info, warn, error, fatal, panic)" + generateEnvDoc(c_GlobalFlagPrefix+"log-level"), } + + LogSizeFlag = Flag{ + Name: c_GlobalFlagPrefix + "log-size", + Value: 500, + Usage: "maximum log file size in MB before rolling (default = no limit)" + generateEnvDoc(c_GlobalFlagPrefix+"log-size"), + } ) var ( diff --git a/cmd/utils/hierarchical_coordinator.go b/cmd/utils/hierarchical_coordinator.go index 2251a8f6f5..fabaf6577e 100644 --- a/cmd/utils/hierarchical_coordinator.go +++ b/cmd/utils/hierarchical_coordinator.go @@ -12,6 +12,7 @@ import ( "github.com/dominant-strategies/go-quai/event" "github.com/dominant-strategies/go-quai/log" "github.com/dominant-strategies/go-quai/quai" + "github.com/spf13/viper" "github.com/syndtr/goleveldb/leveldb" "google.golang.org/protobuf/proto" ) @@ -146,7 +147,7 @@ func (hc *HierarchicalCoordinator) StartQuaiBackend() (*quai.QuaiBackend, error) func (hc *HierarchicalCoordinator) startNode(logPath string, quaiBackend quai.ConsensusAPI, location common.Location, genesisBlock *types.WorkObject) { hc.wg.Add(1) - logger := log.NewLogger(logPath, hc.logLevel) + logger := log.NewLogger(logPath, hc.logLevel, viper.GetInt(LogSizeFlag.Name)) logger.Info("Starting Node at location", "location", location) stack, apiBackend := makeFullNode(hc.p2p, location, hc.slicesRunning, hc.currentExpansionNumber, genesisBlock, logger) quaiBackend.SetApiBackend(&apiBackend, location) diff --git a/consensus/blake3pow/blake3pow.go b/consensus/blake3pow/blake3pow.go index a0f520668e..2af58a8a3a 100644 --- a/consensus/blake3pow/blake3pow.go +++ b/consensus/blake3pow/blake3pow.go @@ -86,7 +86,7 @@ func New(config Config, notify []string, noverify bool, logger *log.Logger) *Bla // NewTester creates a small sized blake3pow PoW scheme useful only for testing // purposes. func NewTester(notify []string, noverify bool) *Blake3pow { - return New(Config{PowMode: ModeTest}, notify, noverify, log.NewLogger("test-blake3pow.log", "info")) + return New(Config{PowMode: ModeTest}, notify, noverify, log.NewLogger("test-blake3pow.log", "info", 500)) } // NewFaker creates a blake3pow consensus engine with a fake PoW scheme that accepts diff --git a/consensus/progpow/progpow.go b/consensus/progpow/progpow.go index 990ee5f87b..6a63e995af 100644 --- a/consensus/progpow/progpow.go +++ b/consensus/progpow/progpow.go @@ -227,7 +227,7 @@ func New(config Config, notify []string, noverify bool, logger *log.Logger) *Pro // NewTester creates a small sized progpow PoW scheme useful only for testing // purposes. func NewTester(notify []string, noverify bool) *Progpow { - return New(Config{PowMode: ModeTest}, notify, noverify, log.NewLogger("test-progpow.log", "info")) + return New(Config{PowMode: ModeTest}, notify, noverify, log.NewLogger("test-progpow.log", "info", 500)) } // NewFaker creates a progpow consensus engine with a fake PoW scheme that accepts diff --git a/log/logger.go b/log/logger.go index 6d4c85f460..7cdd182298 100644 --- a/log/logger.go +++ b/log/logger.go @@ -15,7 +15,7 @@ type Logger = logrus.Logger const ( // default log level defaultLogLevel = logrus.InfoLevel - DebugLogLevel = logrus.DebugLevel + defaultLogSize = 500 // megabytes // log file name globalLogFileName = "global.log" @@ -38,7 +38,7 @@ var ( ) func init() { - Global = createStandardLogger(defaultLogFilePath, defaultLogLevel.String(), true) + Global = createStandardLogger(defaultLogFilePath, defaultLogLevel.String(), 500, true) } func SetGlobalLogger(logFilename string, logLevel string) { @@ -70,11 +70,11 @@ func SetGlobalLogger(logFilename string, logLevel string) { }).Info("Global logger started") } -func NewLogger(logFilename string, logLevel string) *logrus.Logger { +func NewLogger(logFilename string, logLevel string, logSize int) *logrus.Logger { if logFilename == "" { logFilename = defaultLogFilePath } - shardLogger := createStandardLogger(filepath.Join(logDir, logFilename), logLevel, false) + shardLogger := createStandardLogger(filepath.Join(logDir, logFilename), logLevel, logSize, false) shardLogger.WithFields(Fields{ "path": logFilename, "level": logLevel, @@ -82,11 +82,11 @@ func NewLogger(logFilename string, logLevel string) *logrus.Logger { return shardLogger } -func createStandardLogger(logFilename string, logLevel string, stdOut bool) *logrus.Logger { +func createStandardLogger(logFilename string, logLevel string, logSize int, stdOut bool) *logrus.Logger { logger := logrus.New() output := &lumberjack.Logger{ Filename: logFilename, - MaxSize: 500, // megabytes + MaxSize: logSize, // megabytes MaxBackups: 3, MaxAge: 28, //days } diff --git a/p2p/node/peerManager/peerManager.go b/p2p/node/peerManager/peerManager.go index 2c547f1c8c..48f5b2994a 100644 --- a/p2p/node/peerManager/peerManager.go +++ b/p2p/node/peerManager/peerManager.go @@ -177,7 +177,7 @@ func NewManager(ctx context.Context, low int, high int, datastore datastore.Data ctx, cancel := context.WithCancel(ctx) - logger := log.NewLogger("peers.log", viper.GetString(utils.PeersLogLevelFlag.Name)) + logger := log.NewLogger("peers.log", viper.GetString(utils.PeersLogLevelFlag.Name), viper.GetInt(utils.LogSizeFlag.Name)) go func() { defer func() { diff --git a/p2p/protocol/main_test.go b/p2p/protocol/main_test.go index 6fd302d717..c5ea624bdc 100644 --- a/p2p/protocol/main_test.go +++ b/p2p/protocol/main_test.go @@ -5,11 +5,12 @@ import ( "testing" "github.com/dominant-strategies/go-quai/log" + "github.com/sirupsen/logrus" ) func TestMain(m *testing.M) { // Comment / un comment below to see log output while testing // log.ConfigureLogger(log.WithNullLogger()) - log.Global.SetLevel(log.DebugLogLevel) + log.Global.SetLevel(logrus.DebugLevel) os.Exit(m.Run()) }