Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions cmd/diagnostics/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,12 @@ var (
insecure bool
maxNodeSessions int
maxUISessions int
logDirPath string //path of directory to save log file
logFileName string //name of log file with format
logFileSizeMax int //maximum file size for 1 log file
logFilesMax int //maximum number of backup log files the specified directory can have
logFilesAgeMax int //maximum number of days a log file will persist in file
logCompress bool //whether to compress old log files

rootCmd = &cobra.Command{
Use: "diagnostics",
Expand All @@ -44,6 +50,12 @@ func init() {
rootCmd.Flags().BoolVar(&insecure, "insecure", false, "whether to use insecure PIN generation for testing purposes (default is false)")
rootCmd.Flags().IntVar(&maxNodeSessions, "node.sessions", 5000, "maximum number of node sessions to allow")
rootCmd.Flags().IntVar(&maxUISessions, "ui.sessions", 5000, "maximum number of UI sessions to allow")
rootCmd.Flags().StringVar(&logDirPath, "log.dir.path", "./logs", "directory path to store logs data")
rootCmd.Flags().StringVar(&logFileName, "log.file.name", "diagnostics.log", "directory path to store logs data")
rootCmd.Flags().IntVar(&logFileSizeMax, "log.file.size.max", 100, "maximum size of log file in mega bytes to allow")
rootCmd.Flags().IntVar(&logFilesAgeMax, "log.file.age.max", 28, "maximum age in days a log file can persist in system")
rootCmd.Flags().IntVar(&logFilesMax, "log.max.backup", 5, "maximum number of log files that can persist")
rootCmd.Flags().BoolVar(&logCompress, "log.compress", false, "whether to compress historical log files or not")
}

func initConfig() {
Expand Down
4 changes: 4 additions & 0 deletions cmd/diagnostics/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/ledgerwatch/diagnostics/api"
"github.com/ledgerwatch/diagnostics/assets"
"github.com/ledgerwatch/diagnostics/internal/erigon_node"
"github.com/ledgerwatch/diagnostics/internal/logging"
"github.com/ledgerwatch/diagnostics/internal/sessions"
)

Expand All @@ -26,6 +27,9 @@ func main() {
os.Exit(1)
}

//set up logger to implement log rotation
logging.SetupLogger(logDirPath, logFileName, logFileSizeMax, logFilesAgeMax, logFilesMax, logCompress)

// Use of system calls SIGINT and SIGTERM signals that cause a gracefully stop.
signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, syscall.SIGTERM, syscall.SIGINT)
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ require (
github.com/spf13/viper v1.15.0
github.com/stretchr/testify v1.8.1
golang.org/x/exp v0.0.0-20230420155640-133eef4313cb
gopkg.in/natefinch/lumberjack.v2 v2.2.1
)

require (
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,8 @@ gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127/go.mod h1:Co6ibVJAznAaIkqp8
gopkg.in/errgo.v2 v2.1.0/go.mod h1:hNsd1EY+bozCKY1Ytp96fpM3vjJbqLJn88ws8XvfDNI=
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gopkg.in/natefinch/lumberjack.v2 v2.2.1 h1:bBRl1b0OH9s/DuPhuXpNl+VtCaJXFZ5/uEFST95x9zc=
gopkg.in/natefinch/lumberjack.v2 v2.2.1/go.mod h1:YD8tP3GAjkrDg1eZH7EGmyESg/lsYskCTPBJVb9jqSc=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
18 changes: 18 additions & 0 deletions internal/logging/logging.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package logging

import (
"log"

"gopkg.in/natefinch/lumberjack.v2"
)

// update defaut log package's io writer to lumberjack's io writer
func SetupLogger(logDirPath string, logFileName string, logFileSizeMax int, logFilesAgeMax int, logFilesMax int, logCompress bool) {
log.SetOutput(&lumberjack.Logger{
Filename: logDirPath + "/" + logFileName, //name of log file
MaxSize: logFileSizeMax, //maximum size of 1 log file
MaxBackups: logFilesMax, //maximum nuber of log file the specified directory can have
MaxAge: logFilesAgeMax, //maximum age in days that a file can persist
Compress: logCompress, //should historical log files be compressed
})
}