config_json.go 993 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package log
  2. import (
  3. "encoding/json"
  4. "errors"
  5. "strings"
  6. )
  7. func (this *Config) UnmarshalJSON(data []byte) error {
  8. type JsonLogConfig struct {
  9. AccessLog string `json:"access"`
  10. ErrorLog string `json:"error"`
  11. LogLevel string `json:"loglevel"`
  12. }
  13. jsonConfig := new(JsonLogConfig)
  14. if err := json.Unmarshal(data, jsonConfig); err != nil {
  15. return errors.New("Log: Failed to parse log config: " + err.Error())
  16. }
  17. if len(jsonConfig.AccessLog) > 0 {
  18. this.AccessLogPath = jsonConfig.AccessLog
  19. this.AccessLogType = LogType_File
  20. }
  21. if len(jsonConfig.ErrorLog) > 0 {
  22. this.ErrorLogPath = jsonConfig.ErrorLog
  23. this.ErrorLogType = LogType_File
  24. }
  25. level := strings.ToLower(jsonConfig.LogLevel)
  26. switch level {
  27. case "debug":
  28. this.ErrorLogLevel = LogLevel_Debug
  29. case "info":
  30. this.ErrorLogLevel = LogLevel_Info
  31. case "error":
  32. this.ErrorLogLevel = LogLevel_Error
  33. case "none":
  34. this.ErrorLogType = LogType_None
  35. default:
  36. this.ErrorLogLevel = LogLevel_Warning
  37. }
  38. return nil
  39. }