From a928fe4ab73f897929cf5abdac2f4ad380c8287b Mon Sep 17 00:00:00 2001 From: Jason Yi <90701258+jasonyic@users.noreply.github.com> Date: Mon, 1 Aug 2022 01:48:09 +0800 Subject: [PATCH] feat: make log to console as optional (#4208) * fix: add option for console only Signed-off-by: Jason Yi * fix: add console option for node & harmony Signed-off-by: Jason Yi * fix: add console to default config Signed-off-by: Jason Yi * fix: remove debugging code Signed-off-by: Jason Yi * fix: add log.console to cmd line options Signed-off-by: Jason Yi * fix: linting & revert back msg.pg.go Signed-off-by: Jason Yi * undo changes in go.sum Signed-off-by: Jason Yi * update: config migration Signed-off-by: Jason Yi --- cmd/bootnode/main.go | 5 ++++- cmd/harmony/config_migrations.go | 8 ++++++++ cmd/harmony/config_test.go | 1 + cmd/harmony/default.go | 1 + cmd/harmony/flags.go | 10 ++++++++++ cmd/harmony/flags_test.go | 2 ++ cmd/harmony/main.go | 8 ++++---- internal/configs/harmony/harmony.go | 1 + rosetta/infra/harmony-mainnet.conf | 1 + rosetta/infra/harmony-pstn.conf | 1 + 10 files changed, 33 insertions(+), 5 deletions(-) diff --git a/cmd/bootnode/main.go b/cmd/bootnode/main.go index 245d7a44f..ad5005e33 100644 --- a/cmd/bootnode/main.go +++ b/cmd/bootnode/main.go @@ -92,6 +92,7 @@ func printVersion(me string) { func main() { ip := flag.String("ip", "127.0.0.1", "IP of the node") port := flag.String("port", "9876", "port of the node.") + console := flag.Bool("console_only", false, "Output to console only") logFolder := flag.String("log_folder", "latest", "the folder collecting the logs of this execution") logMaxSize := flag.Int("log_max_size", 100, "the max size in megabytes of the log file before it gets rotated") logRotateCount := flag.Int("log_rotate_count", 0, "the number of rotated logs to keep. If set to 0 rotation is disabled") @@ -111,7 +112,9 @@ func main() { // Logging setup utils.SetLogContext(*port, *ip) utils.SetLogVerbosity(log.Lvl(*verbosity)) - utils.AddLogFile(fmt.Sprintf("%v/bootnode-%v-%v.log", *logFolder, *ip, *port), *logMaxSize, *logRotateCount, *logRotateMaxAge) + if *console != true { + utils.AddLogFile(fmt.Sprintf("%v/bootnode-%v-%v.log", *logFolder, *ip, *port), *logMaxSize, *logRotateCount, *logRotateMaxAge) + } privKey, _, err := utils.LoadKeyFromFile(*keyFile) if err != nil { diff --git a/cmd/harmony/config_migrations.go b/cmd/harmony/config_migrations.go index d2fe015d2..9a4f59716 100644 --- a/cmd/harmony/config_migrations.go +++ b/cmd/harmony/config_migrations.go @@ -274,4 +274,12 @@ func init() { return confTree } + migrations["2.5.3"] = func(confTree *toml.Tree) *toml.Tree { + if confTree.Get("Log.Console") == nil { + confTree.Set("Log.Console", defaultConfig.Log.Console) + } + + confTree.Set("Version", "2.5.4") + return confTree + } } diff --git a/cmd/harmony/config_test.go b/cmd/harmony/config_test.go index ae88089f7..0d5879700 100644 --- a/cmd/harmony/config_test.go +++ b/cmd/harmony/config_test.go @@ -59,6 +59,7 @@ Version = "1.0.4" Port = 9500 [Log] + Console = false FileName = "harmony.log" Folder = "./latest" RotateSize = 100 diff --git a/cmd/harmony/default.go b/cmd/harmony/default.go index 12778c8e5..2d2ad4fe4 100644 --- a/cmd/harmony/default.go +++ b/cmd/harmony/default.go @@ -86,6 +86,7 @@ var defaultConfig = harmonyconfig.HarmonyConfig{ ProfileDebugValues: []int{0}, }, Log: harmonyconfig.LogConfig{ + Console: false, Folder: "./latest", FileName: "harmony.log", RotateSize: 100, diff --git a/cmd/harmony/flags.go b/cmd/harmony/flags.go index 316f5d318..ade66df63 100644 --- a/cmd/harmony/flags.go +++ b/cmd/harmony/flags.go @@ -159,6 +159,7 @@ var ( logVerbosityFlag, logVerbosePrintsFlag, legacyVerbosityFlag, + logConsoleFlag, legacyLogFolderFlag, legacyLogRotateSizeFlag, @@ -1229,6 +1230,11 @@ var ( Usage: "debugging feature. to print verbose internal objects as JSON in log file. available internal objects: config", DefValue: []string{"config"}, } + logConsoleFlag = cli.BoolFlag{ + Name: "log.console", + Usage: "output log to console only", + DefValue: defaultConfig.Log.Console, + } // TODO: remove context (this shall not be in the log) logContextIPFlag = cli.StringFlag{ Name: "log.ctx.ip", @@ -1298,6 +1304,10 @@ func applyLogFlags(cmd *cobra.Command, config *harmonyconfig.HarmonyConfig) { config.Log.VerbosePrints = harmonyconfig.FlagSliceToLogVerbosePrints(verbosePrintsFlagSlice) } + if cli.IsFlagChanged(cmd, logConsoleFlag) { + config.Log.Console = cli.GetBoolFlagValue(cmd, logConsoleFlag) + } + if cli.HasFlagsChanged(cmd, []cli.Flag{logContextIPFlag, logContextPortFlag}) { ctx := getDefaultLogContextCopy() config.Log.Context = &ctx diff --git a/cmd/harmony/flags_test.go b/cmd/harmony/flags_test.go index b739a0da6..d269dabc6 100644 --- a/cmd/harmony/flags_test.go +++ b/cmd/harmony/flags_test.go @@ -121,6 +121,7 @@ func TestHarmonyFlags(t *testing.T) { ProfileDebugValues: []int{0}, }, Log: harmonyconfig.LogConfig{ + Console: false, Folder: "./latest", FileName: "validator-8.8.8.8-9000.log", RotateSize: 100, @@ -1082,6 +1083,7 @@ func TestLogFlags(t *testing.T) { { args: []string{"--log.ctx.ip", "8.8.8.8", "--log.ctx.port", "9001"}, expConfig: harmonyconfig.LogConfig{ + Console: defaultConfig.Log.Console, Folder: defaultConfig.Log.Folder, FileName: defaultConfig.Log.FileName, RotateSize: defaultConfig.Log.RotateSize, diff --git a/cmd/harmony/main.go b/cmd/harmony/main.go index 845a9234e..695ae47dd 100644 --- a/cmd/harmony/main.go +++ b/cmd/harmony/main.go @@ -246,18 +246,18 @@ func applyRootFlags(cmd *cobra.Command, config *harmonyconfig.HarmonyConfig) { func setupNodeLog(config harmonyconfig.HarmonyConfig) { logPath := filepath.Join(config.Log.Folder, config.Log.FileName) - rotateSize := config.Log.RotateSize - rotateCount := config.Log.RotateCount - rotateMaxAge := config.Log.RotateMaxAge verbosity := config.Log.Verbosity - utils.AddLogFile(logPath, rotateSize, rotateCount, rotateMaxAge) utils.SetLogVerbosity(log.Lvl(verbosity)) if config.Log.Context != nil { ip := config.Log.Context.IP port := config.Log.Context.Port utils.SetLogContext(ip, strconv.Itoa(port)) } + + if config.Log.Console != true { + utils.AddLogFile(logPath, config.Log.RotateSize, config.Log.RotateCount, config.Log.RotateMaxAge) + } } func setupNodeAndRun(hc harmonyconfig.HarmonyConfig) { diff --git a/internal/configs/harmony/harmony.go b/internal/configs/harmony/harmony.go index 1ad5f0e72..60265781c 100644 --- a/internal/configs/harmony/harmony.go +++ b/internal/configs/harmony/harmony.go @@ -128,6 +128,7 @@ type PprofConfig struct { } type LogConfig struct { + Console bool Folder string FileName string RotateSize int diff --git a/rosetta/infra/harmony-mainnet.conf b/rosetta/infra/harmony-mainnet.conf index d8fd61b68..475dbcc59 100644 --- a/rosetta/infra/harmony-mainnet.conf +++ b/rosetta/infra/harmony-mainnet.conf @@ -45,6 +45,7 @@ Version = "2.5.5" RosettaPort = 9700 [Log] + Console = false FileName = "harmony.log" Folder = "./latest" RotateCount = 0 diff --git a/rosetta/infra/harmony-pstn.conf b/rosetta/infra/harmony-pstn.conf index 32e0db2d7..730a3278c 100644 --- a/rosetta/infra/harmony-pstn.conf +++ b/rosetta/infra/harmony-pstn.conf @@ -45,6 +45,7 @@ Version = "2.5.4" RosettaPort = 9700 [Log] + Console = false FileName = "harmony.log" Folder = "./latest" RotateCount = 0