广汽安道拓Acura项目MES后台
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.

73 lines
1.5 KiB

3 years ago
3 years ago
  1. package logger
  2. import (
  3. "LAPP_ACURA_MOM_BACKEND/utils"
  4. "errors"
  5. "github.com/spf13/viper"
  6. )
  7. var loggerConfig = new(outputSettings)
  8. var outputConfig = new(output)
  9. type output struct {
  10. DB map[string]db `yaml:"db"`
  11. File map[string]file `yaml:"file"`
  12. Console console `yaml:"console"`
  13. }
  14. type db struct {
  15. DBType string `yaml:"dbtype"`
  16. Host string `yaml:"host"`
  17. Port int `yaml:"port"`
  18. Database string `yaml:"database"`
  19. Table string `yaml:"table"`
  20. User string `yaml:"user"`
  21. Pwd string `yaml:"pwd"`
  22. }
  23. type file struct {
  24. Filename string `yaml:"filename"`
  25. //MaxSize int `yaml:"maxSize"`
  26. MaxAge int `yaml:"maxAge"`
  27. //MaxBackups int `yaml:"maxBackups"`
  28. //Compress bool `yaml:"compress"`
  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. baseDir, err := utils.GetCurrentPath("conf")
  44. if err != nil {
  45. return err
  46. }
  47. viper.SetConfigName("log_config")
  48. viper.SetConfigType("yaml")
  49. viper.AddConfigPath(baseDir)
  50. if err = viper.ReadInConfig(); err != nil {
  51. return err
  52. }
  53. if err = viper.Unmarshal(loggerConfig); err != nil {
  54. return err
  55. }
  56. if err = viper.Unmarshal(outputConfig); err != nil {
  57. return err
  58. }
  59. _, exist := loggerConfig.Config["root"]
  60. if !exist {
  61. return errors.New("未配置root默认配置")
  62. }
  63. return nil
  64. }