| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071 |
- package subconfigs
-
- import (
- "fmt"
- "strings"
- )
-
- // LogConfig 日志配置
- type LogConfig struct {
- BaseConfig
- Level string `yaml:"level"`
- Output string `yaml:"output"`
- FilePath string `yaml:"file_path"`
- MaxSize int `yaml:"max_size"`
- MaxBackups int `yaml:"max_backups"`
- MaxAge int `yaml:"max_age"`
- Compress bool `yaml:"compress"`
- ESEnabled bool `yaml:"es_enabled"`
- ESPath string `yaml:"es_path"`
- JSONFormat bool `yaml:"json_format"`
- }
-
- // NewLogConfig 创建日志配置实例
- func NewLogConfig() *LogConfig {
- return &LogConfig{}
- }
-
- // SetDefaults 设置默认值
- func (c *LogConfig) SetDefaults() {
- c.Level = "info"
- c.Output = "both"
- c.FilePath = "logs/service.log"
- c.MaxSize = 100
- c.MaxBackups = 3
- c.MaxAge = 30
- c.Compress = true
- c.ESEnabled = true
- c.ESPath = "logs/es-service.log"
- c.JSONFormat = true
- }
-
- // Load 从yaml数据加载
- func (c *LogConfig) Load(data map[string]interface{}) error {
- return c.LoadFromYAML(data, c)
- }
-
- // Validate 验证配置
- func (c *LogConfig) Validate() error {
- // 验证日志级别
- validLevels := map[string]bool{
- "debug": true, "info": true, "warn": true, "error": true,
- }
- if !validLevels[strings.ToLower(c.Level)] {
- return fmt.Errorf("invalid log level: %s", c.Level)
- }
-
- // 验证输出类型
- validOutputs := map[string]bool{
- "console": true, "file": true, "both": true,
- }
- if !validOutputs[c.Output] {
- return fmt.Errorf("invalid log output: %s", c.Output)
- }
-
- return nil
- }
-
- // 自动注册
- func init() {
- Register("log", &LogConfig{})
- }
|