From 683decf1bc35c4cbbd6d82973f29c59254a1105b Mon Sep 17 00:00:00 2001 From: Ryan Leung Date: Tue, 23 Apr 2024 15:12:57 +0800 Subject: [PATCH 1/2] fix log level Signed-off-by: Ryan Leung --- cmd/pd-server/main.go | 8 ++++---- pkg/mcs/resourcemanager/server/config.go | 6 ++---- pkg/mcs/scheduling/server/config/config.go | 6 ++---- pkg/mcs/tso/server/config.go | 6 ++---- pkg/mcs/utils/constant.go | 2 ++ server/config/config.go | 7 +++---- 6 files changed, 15 insertions(+), 20 deletions(-) diff --git a/cmd/pd-server/main.go b/cmd/pd-server/main.go index d0f4e458412..f338a3914ce 100644 --- a/cmd/pd-server/main.go +++ b/cmd/pd-server/main.go @@ -102,7 +102,7 @@ func NewTSOServiceCommand() *cobra.Command { cmd.Flags().StringP("cacert", "", "", "path of file that contains list of trusted TLS CAs") cmd.Flags().StringP("cert", "", "", "path of file that contains X509 certificate in PEM format") cmd.Flags().StringP("key", "", "", "path of file that contains X509 key in PEM format") - cmd.Flags().StringP("log-level", "L", "info", "log level: debug, info, warn, error, fatal (default 'info')") + cmd.Flags().StringP("log-level", "L", "", "log level: debug, info, warn, error, fatal (default 'info')") cmd.Flags().StringP("log-file", "", "", "log file path") return cmd } @@ -122,7 +122,7 @@ func NewSchedulingServiceCommand() *cobra.Command { cmd.Flags().StringP("cacert", "", "", "path of file that contains list of trusted TLS CAs") cmd.Flags().StringP("cert", "", "", "path of file that contains X509 certificate in PEM format") cmd.Flags().StringP("key", "", "", "path of file that contains X509 key in PEM format") - cmd.Flags().StringP("log-level", "L", "info", "log level: debug, info, warn, error, fatal (default 'info')") + cmd.Flags().StringP("log-level", "L", "", "log level: debug, info, warn, error, fatal (default 'info')") cmd.Flags().StringP("log-file", "", "", "log file path") return cmd } @@ -142,7 +142,7 @@ func NewResourceManagerServiceCommand() *cobra.Command { cmd.Flags().StringP("cacert", "", "", "path of file that contains list of trusted TLS CAs") cmd.Flags().StringP("cert", "", "", "path of file that contains X509 certificate in PEM format") cmd.Flags().StringP("key", "", "", "path of file that contains X509 key in PEM format") - cmd.Flags().StringP("log-level", "L", "info", "log level: debug, info, warn, error, fatal (default 'info')") + cmd.Flags().StringP("log-level", "L", "", "log level: debug, info, warn, error, fatal (default 'info')") cmd.Flags().StringP("log-file", "", "", "log file path") return cmd } @@ -171,7 +171,7 @@ func addFlags(cmd *cobra.Command) { cmd.Flags().StringP("initial-cluster", "", "", "initial cluster configuration for bootstrapping, e,g. pd=http://127.0.0.1:2380") cmd.Flags().StringP("join", "", "", "join to an existing cluster (usage: cluster's '${advertise-client-urls}'") cmd.Flags().StringP("metrics-addr", "", "", "prometheus pushgateway address, leaves it empty will disable prometheus push") - cmd.Flags().StringP("log-level", "L", "info", "log level: debug, info, warn, error, fatal (default 'info')") + cmd.Flags().StringP("log-level", "L", "", "log level: debug, info, warn, error, fatal (default 'info')") cmd.Flags().StringP("log-file", "", "", "log file path") cmd.Flags().StringP("cacert", "", "", "path of file that contains list of trusted TLS CAs") cmd.Flags().StringP("cert", "", "", "path of file that contains X509 certificate in PEM format") diff --git a/pkg/mcs/resourcemanager/server/config.go b/pkg/mcs/resourcemanager/server/config.go index c93478ddf26..d280799f2c5 100644 --- a/pkg/mcs/resourcemanager/server/config.go +++ b/pkg/mcs/resourcemanager/server/config.go @@ -237,10 +237,6 @@ func (c *Config) Adjust(meta *toml.MetaData, reloading bool) error { c.adjustLog(configMetaData.Child("log")) c.Security.Encryption.Adjust() - if len(c.Log.Format) == 0 { - c.Log.Format = utils.DefaultLogFormat - } - c.Controller.Adjust(configMetaData.Child("controller")) configutil.AdjustInt64(&c.LeaderLease, utils.DefaultLeaderLease) @@ -251,6 +247,8 @@ func (c *Config) adjustLog(meta *configutil.ConfigMetaData) { if !meta.IsDefined("disable-error-verbose") { c.Log.DisableErrorVerbose = utils.DefaultDisableErrorVerbose } + configutil.AdjustString(&c.Log.Format, utils.DefaultLogFormat) + configutil.AdjustString(&c.Log.Level, utils.DefaultLogLevel) } // GetTLSConfig returns the TLS config. diff --git a/pkg/mcs/scheduling/server/config/config.go b/pkg/mcs/scheduling/server/config/config.go index d462a9a58b5..39c320338c7 100644 --- a/pkg/mcs/scheduling/server/config/config.go +++ b/pkg/mcs/scheduling/server/config/config.go @@ -145,10 +145,6 @@ func (c *Config) adjust(meta *toml.MetaData) error { c.adjustLog(configMetaData.Child("log")) c.Security.Encryption.Adjust() - if len(c.Log.Format) == 0 { - c.Log.Format = utils.DefaultLogFormat - } - configutil.AdjustInt64(&c.LeaderLease, utils.DefaultLeaderLease) if err := c.Schedule.Adjust(configMetaData.Child("schedule"), false); err != nil { @@ -161,6 +157,8 @@ func (c *Config) adjustLog(meta *configutil.ConfigMetaData) { if !meta.IsDefined("disable-error-verbose") { c.Log.DisableErrorVerbose = utils.DefaultDisableErrorVerbose } + configutil.AdjustString(&c.Log.Format, utils.DefaultLogFormat) + configutil.AdjustString(&c.Log.Level, utils.DefaultLogLevel) } // GetListenAddr returns the ListenAddr diff --git a/pkg/mcs/tso/server/config.go b/pkg/mcs/tso/server/config.go index eedf3a2f1b1..165ff904dc4 100644 --- a/pkg/mcs/tso/server/config.go +++ b/pkg/mcs/tso/server/config.go @@ -226,10 +226,6 @@ func (c *Config) Adjust(meta *toml.MetaData, reloading bool) error { c.adjustLog(configMetaData.Child("log")) c.Security.Encryption.Adjust() - if len(c.Log.Format) == 0 { - c.Log.Format = utils.DefaultLogFormat - } - return nil } @@ -237,6 +233,8 @@ func (c *Config) adjustLog(meta *configutil.ConfigMetaData) { if !meta.IsDefined("disable-error-verbose") { c.Log.DisableErrorVerbose = utils.DefaultDisableErrorVerbose } + configutil.AdjustString(&c.Log.Format, utils.DefaultLogFormat) + configutil.AdjustString(&c.Log.Level, utils.DefaultLogLevel) } // Validate is used to validate if some configurations are right. diff --git a/pkg/mcs/utils/constant.go b/pkg/mcs/utils/constant.go index 6174852d89f..98549274fcc 100644 --- a/pkg/mcs/utils/constant.go +++ b/pkg/mcs/utils/constant.go @@ -34,6 +34,8 @@ const ( DefaultHTTPGracefulShutdownTimeout = 5 * time.Second // DefaultLogFormat is the default log format DefaultLogFormat = "text" + // DefaultLogLevel is the default log level + DefaultLogLevel = "info" // DefaultDisableErrorVerbose is the default value of DisableErrorVerbose DefaultDisableErrorVerbose = true // DefaultLeaderLease is the default value of LeaderLease diff --git a/server/config/config.go b/server/config/config.go index 86306de7cc9..c12aea1768c 100644 --- a/server/config/config.go +++ b/server/config/config.go @@ -233,6 +233,7 @@ const ( minTSOUpdatePhysicalInterval = 1 * time.Millisecond defaultLogFormat = "text" + defaultLogLevel = "info" defaultServerMemoryLimit = 0 minServerMemoryLimit = 0 @@ -463,10 +464,6 @@ func (c *Config) Adjust(meta *toml.MetaData, reloading bool) error { c.Security.Encryption.Adjust() - if len(c.Log.Format) == 0 { - c.Log.Format = defaultLogFormat - } - c.Controller.Adjust(configMetaData.Child("controller")) return nil @@ -476,6 +473,8 @@ func (c *Config) adjustLog(meta *configutil.ConfigMetaData) { if !meta.IsDefined("disable-error-verbose") { c.Log.DisableErrorVerbose = defaultDisableErrorVerbose } + configutil.AdjustString(&c.Log.Format, defaultLogFormat) + configutil.AdjustString(&c.Log.Level, defaultLogLevel) } // Clone returns a cloned configuration. From 9802c1f13f8e2e3283a62dafed162c7619e852b9 Mon Sep 17 00:00:00 2001 From: Ryan Leung Date: Tue, 23 Apr 2024 15:28:15 +0800 Subject: [PATCH 2/2] add test Signed-off-by: Ryan Leung --- server/config/config_test.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/server/config/config_test.go b/server/config/config_test.go index 75e69c26d5c..07c14a8ab94 100644 --- a/server/config/config_test.go +++ b/server/config/config_test.go @@ -245,6 +245,22 @@ tso-update-physical-interval = "15s" re.NoError(err) re.Equal(maxTSOUpdatePhysicalInterval, cfg.TSOUpdatePhysicalInterval.Duration) + + cfgData = ` +[log] +level = "debug" +` + flagSet = pflag.NewFlagSet("testlog", pflag.ContinueOnError) + flagSet.StringP("log-level", "L", "info", "log level: debug, info, warn, error, fatal (default 'info')") + flagSet.Parse(nil) + cfg = NewConfig() + err = cfg.Parse(flagSet) + re.NoError(err) + meta, err = toml.Decode(cfgData, &cfg) + re.NoError(err) + err = cfg.Adjust(&meta, false) + re.NoError(err) + re.Equal("debug", cfg.Log.Level) } func TestMigrateFlags(t *testing.T) {