説明なし
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

log_config.go 1.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package subconfigs
  2. import (
  3. "fmt"
  4. "strings"
  5. )
  6. // LogConfig 日志配置
  7. type LogConfig struct {
  8. BaseConfig
  9. Level string `yaml:"level"`
  10. Output string `yaml:"output"`
  11. FilePath string `yaml:"file_path"`
  12. MaxSize int `yaml:"max_size"`
  13. MaxBackups int `yaml:"max_backups"`
  14. MaxAge int `yaml:"max_age"`
  15. Compress bool `yaml:"compress"`
  16. ESEnabled bool `yaml:"es_enabled"`
  17. ESPath string `yaml:"es_path"`
  18. JSONFormat bool `yaml:"json_format"`
  19. }
  20. // NewLogConfig 创建日志配置实例
  21. func NewLogConfig() *LogConfig {
  22. return &LogConfig{}
  23. }
  24. // SetDefaults 设置默认值
  25. func (c *LogConfig) SetDefaults() {
  26. c.Level = "info"
  27. c.Output = "both"
  28. c.FilePath = "logs/service.log"
  29. c.MaxSize = 100
  30. c.MaxBackups = 3
  31. c.MaxAge = 30
  32. c.Compress = true
  33. c.ESEnabled = true
  34. c.ESPath = "logs/es-service.log"
  35. c.JSONFormat = true
  36. }
  37. // Load 从yaml数据加载
  38. func (c *LogConfig) Load(data map[string]interface{}) error {
  39. return c.LoadFromYAML(data, c)
  40. }
  41. // Validate 验证配置
  42. func (c *LogConfig) Validate() error {
  43. // 验证日志级别
  44. validLevels := map[string]bool{
  45. "debug": true, "info": true, "warn": true, "error": true,
  46. }
  47. if !validLevels[strings.ToLower(c.Level)] {
  48. return fmt.Errorf("invalid log level: %s", c.Level)
  49. }
  50. // 验证输出类型
  51. validOutputs := map[string]bool{
  52. "console": true, "file": true, "both": true,
  53. }
  54. if !validOutputs[c.Output] {
  55. return fmt.Errorf("invalid log output: %s", c.Output)
  56. }
  57. return nil
  58. }
  59. // 自动注册
  60. func init() {
  61. Register("log", &LogConfig{})
  62. }