Skip to content

Commit

Permalink
Add and use flags to set log rollover sizes
Browse files Browse the repository at this point in the history
  • Loading branch information
Djadih committed Jun 27, 2024
1 parent d81bfc5 commit 931dbac
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 11 deletions.
7 changes: 7 additions & 0 deletions cmd/utils/flags.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ var GlobalFlags = []Flag{
DataDirFlag,
AncientDirFlag,
LogLevelFlag,
LogSizeFlag,
}

var NodeFlags = []Flag{
Expand Down Expand Up @@ -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 (
Expand Down
3 changes: 2 additions & 1 deletion cmd/utils/hierarchical_coordinator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
)
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion consensus/blake3pow/blake3pow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion consensus/progpow/progpow.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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) {
Expand Down Expand Up @@ -70,23 +70,23 @@ 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,
}).Info("Shard logger started")
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
}
Expand Down
2 changes: 1 addition & 1 deletion p2p/node/peerManager/peerManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
3 changes: 2 additions & 1 deletion p2p/protocol/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
}

0 comments on commit 931dbac

Please sign in to comment.