GAAS GFrame项目web后台
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

68 lines
1.4 KiB

  1. package logger
  2. import (
  3. "errors"
  4. "github.com/spf13/viper"
  5. )
  6. var loggerConfig = new(outputSettings)
  7. var outputConfig = new(output)
  8. type output struct {
  9. DB map[string]db `yaml:"db"`
  10. File map[string]file `yaml:"file"`
  11. Console console `yaml:"console"`
  12. }
  13. type db struct {
  14. DBType string `yaml:"dbtype"`
  15. Host string `yaml:"host"`
  16. Port int `yaml:"port"`
  17. Database string `yaml:"database"`
  18. Table string `yaml:"table"`
  19. User string `yaml:"user"`
  20. Pwd string `yaml:"pwd"`
  21. }
  22. type file struct {
  23. Filename string `yaml:"filename"`
  24. //MaxSize int `yaml:"maxSize"`
  25. MaxAge int `yaml:"maxAge"`
  26. //MaxBackups int `yaml:"maxBackups"`
  27. //Compress bool `yaml:"compress"`
  28. }
  29. type console struct {
  30. Enable bool `yaml:"enable"`
  31. }
  32. type outputDriver struct {
  33. DriverType string `yaml:"drivertype"`
  34. Level string `yaml:"level"`
  35. DriverName string `yaml:"drivername"`
  36. }
  37. type outputSettings struct {
  38. Config map[string]map[string]outputDriver `yaml:"config"`
  39. }
  40. func InitConfig() error {
  41. var err error
  42. viper.SetConfigName("log_config")
  43. viper.SetConfigType("yaml")
  44. viper.AddConfigPath("./conf/")
  45. if err = viper.ReadInConfig(); err != nil {
  46. return err
  47. }
  48. if err = viper.Unmarshal(loggerConfig); err != nil {
  49. return err
  50. }
  51. if err = viper.Unmarshal(outputConfig); err != nil {
  52. return err
  53. }
  54. _, exist := loggerConfig.Config["root"]
  55. if !exist {
  56. return errors.New("未配置root默认配置")
  57. }
  58. return nil
  59. }