From 6f0128bed3f4fec68ac9db13af7e43ce7a634927 Mon Sep 17 00:00:00 2001 From: ioito Date: Tue, 11 Apr 2023 16:48:33 +0800 Subject: [PATCH] fix(common): log time zone and format --- pkg/cloudcommon/db/db_dispatcher.go | 2 +- pkg/cloudcommon/options/changes.go | 9 +++++++++ pkg/cloudcommon/options/options.go | 13 ++++++++----- pkg/httperrors/httperrors.go | 23 ++++++++++++++++++++--- 4 files changed, 38 insertions(+), 9 deletions(-) diff --git a/pkg/cloudcommon/db/db_dispatcher.go b/pkg/cloudcommon/db/db_dispatcher.go index cfdbf7a0b9..6f84d1964d 100644 --- a/pkg/cloudcommon/db/db_dispatcher.go +++ b/pkg/cloudcommon/db/db_dispatcher.go @@ -1237,7 +1237,7 @@ func _doCreateItem( if len(name) > 0 { err = NewNameValidator(manager, ownerId, name, uniqValues) if err != nil { - return nil, errors.Wrap(err, "NewNameValidator") + return nil, err } } } diff --git a/pkg/cloudcommon/options/changes.go b/pkg/cloudcommon/options/changes.go index 2b00a65668..8b0cfd4a1c 100644 --- a/pkg/cloudcommon/options/changes.go +++ b/pkg/cloudcommon/options/changes.go @@ -50,6 +50,15 @@ func OnBaseOptionsChange(oOpts, nOpts interface{}) bool { if oldOpts.EnableQuotaCheck != newOpts.EnableQuotaCheck { consts.SetEnableQuotaCheck(newOpts.EnableQuotaCheck) } + if oldOpts.LogLevel != newOpts.LogLevel { + log.SetLogLevelByString(log.Logger(), newOpts.LogLevel) + } + if oldOpts.LogWithTimeZone != newOpts.LogWithTimeZone || oldOpts.LogTimestampFormat != newOpts.LogTimestampFormat { + log.Logger().Formatter = &log.TextFormatter{ + TimeZone: newOpts.LogWithTimeZone, + TimestampFormat: newOpts.LogTimestampFormat, + } + } if oldOpts.ApiServer != newOpts.ApiServer { log.Debugf("api_server changed from %s to %s", oldOpts.ApiServer, newOpts.ApiServer) } diff --git a/pkg/cloudcommon/options/options.go b/pkg/cloudcommon/options/options.go index 711db15b8b..1f89ae933e 100644 --- a/pkg/cloudcommon/options/options.go +++ b/pkg/cloudcommon/options/options.go @@ -58,9 +58,11 @@ type BaseOptions struct { DebugClient bool `help:"Switch on/off mcclient debugs" default:"false"` - LogLevel string `help:"log level" default:"info" choices:"debug|info|warn|error"` - LogVerboseLevel int `help:"log verbosity level" default:"0"` - LogFilePrefix string `help:"prefix of log files"` + LogLevel string `help:"log level" default:"info" choices:"debug|info|warn|error"` + LogWithTimeZone string `help:"log time zone" default:"UTC"` + LogTimestampFormat string `help:"log time format" default:"2006-01-02 15:04:05"` + LogVerboseLevel int `help:"log verbosity level" default:"0"` + LogFilePrefix string `help:"prefix of log files"` CorsHosts []string `help:"List of hostname that allow CORS"` TempPath string `help:"Path for store temp file, at least 40G space" default:"/opt/yunion/tmp"` @@ -321,6 +323,7 @@ func ParseOptions(optStruct interface{}, args []string, configFileName string, s } consts.SetServiceName(optionsRef.ApplicationID) + httperrors.SetTimeZone(optionsRef.TimeZone) // log configuration log.SetVerboseLevel(int32(optionsRef.LogVerboseLevel)) @@ -330,8 +333,8 @@ func ParseOptions(optStruct interface{}, args []string, configFileName string, s } log.Infof("Set log level to %q", optionsRef.LogLevel) log.Logger().Formatter = &log.TextFormatter{ - TimeZone: optionsRef.TimeZone, - TimestampFormat: "2006-01-02 15:04:05", + TimeZone: optionsRef.LogWithTimeZone, + TimestampFormat: optionsRef.LogTimestampFormat, } if optionsRef.LogFilePrefix != "" { dir, name := filepath.Split(optionsRef.LogFilePrefix) diff --git a/pkg/httperrors/httperrors.go b/pkg/httperrors/httperrors.go index 3f1be2c4e3..5e393c40d9 100644 --- a/pkg/httperrors/httperrors.go +++ b/pkg/httperrors/httperrors.go @@ -18,6 +18,7 @@ import ( "context" "net/http" "runtime/debug" + "time" "yunion.io/x/jsonutils" "yunion.io/x/log" @@ -27,6 +28,20 @@ import ( "yunion.io/x/onecloud/pkg/i18n" ) +var ( + timeZone *time.Location +) + +func init() { + timeZone = time.Local +} + +func SetTimeZone(tzStr string) { + if tz, _ := time.LoadLocation(tzStr); tz != nil { + timeZone = tz + } +} + func SendHTTPErrorHeader(w http.ResponseWriter, statusCode int) { w.Header().Set("Content-Type", "application/json") w.WriteHeader(statusCode) @@ -37,9 +52,10 @@ func SetHTTPRedirectLocationHeader(w http.ResponseWriter, location string) { } type Error struct { - Code int `json:"code,omitzero"` - Class string `json:"class,omitempty"` - Details string `json:"details,omitempty"` + Code int `json:"code,omitzero"` + Class string `json:"class,omitempty"` + Details string `json:"details,omitempty"` + Time time.Time `json:"time,omitempty"` } func NewErrorFromJCError(ctx context.Context, je *httputils.JSONClientError) Error { @@ -84,6 +100,7 @@ func HTTPError(ctx context.Context, w http.ResponseWriter, msg string, statusCod Code: statusCode, Class: class, Details: details, + Time: time.Now().In(timeZone), } body := jsonutils.Marshal(err) w.Write([]byte(body.String()))