苏州瑞玛APS项目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.

69 lines
1.5 KiB

3 years ago
3 years ago
3 years ago
  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. Default string `yaml:"Default"`
  29. }
  30. type console struct {
  31. Enable bool `yaml:"enable"`
  32. }
  33. type outputDriver struct {
  34. DriverType string `yaml:"drivertype"`
  35. Level string `yaml:"level"`
  36. DriverName string `yaml:"drivername"`
  37. }
  38. type outputSettings struct {
  39. Config map[string]map[string]outputDriver `yaml:"config"`
  40. }
  41. func InitConfig() error {
  42. var err error
  43. viper.SetConfigName("log_config")
  44. viper.SetConfigType("yaml")
  45. viper.AddConfigPath("./conf/")
  46. if err = viper.ReadInConfig(); err != nil {
  47. return err
  48. }
  49. if err = viper.Unmarshal(loggerConfig); err != nil {
  50. return err
  51. }
  52. if err = viper.Unmarshal(outputConfig); err != nil {
  53. return err
  54. }
  55. _, exist := loggerConfig.Config["root"]
  56. if !exist {
  57. return errors.New("未配置root默认配置")
  58. }
  59. return nil
  60. }