mirror of
https://github.com/yunionio/cloudpods.git
synced 2026-09-24 16:03:43 +08:00
Merge pull request #16417 from ioito/hotfix/qx-log-with-time-zone
fix(common): log time zone and format
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
|
||||
@@ -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)
|
||||
|
||||
@@ -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()))
|
||||
|
||||
Reference in New Issue
Block a user