package logger
import "sync"
type Config struct {
JSON bool `yaml:"json,omitempty"`
Level string `yaml:"level,omitempty"`
Sample bool `yaml:"sample,omitempty"`
ComponentLevels map[string]string `yaml:"component_levels,omitempty"`
SampleInitial int `yaml:"sample_initial,omitempty"`
SampleInterval int `yaml:"sample_interval,omitempty"`
ItemSampleSeconds int `yaml:"item_sample_seconds,omitempty"`
ItemSampleInitial int `yaml:"item_sample_initial,omitempty"`
ItemSampleInterval int `yaml:"item_sample_interval,omitempty"`
lock sync.Mutex `yaml:"-"`
onUpdatedCallbacks []ConfigObserver `yaml:"-"`
}
type ConfigObserver func(*Config) error
func (c *Config) Update(o *Config) error {
c.lock.Lock()
c.JSON = o.JSON
c.Level = o.Level
c.Sample = o.Sample
c.SampleInitial = o.SampleInitial
c.SampleInterval = o.SampleInterval
c.ItemSampleSeconds = o.ItemSampleSeconds
c.ItemSampleInitial = o.ItemSampleInitial
c.ItemSampleInterval = o.ItemSampleInterval
c.ComponentLevels = o.ComponentLevels
callbacks := c.onUpdatedCallbacks
c.lock.Unlock()
for _, cb := range callbacks {
if err := cb(c); err != nil {
return err
}
}
return nil
}
func (c *Config) AddUpdateObserver(cb ConfigObserver) {
c.lock.Lock()
defer c.lock.Unlock()
c.onUpdatedCallbacks = append(c.onUpdatedCallbacks, cb)
}