name string
bufferLength int64
queue chan *Event
- mutex sync.Mutex
+ rwmutex sync.RWMutex
loggers map[string]EventLogger
flush chan bool
close chan bool
// AddLogger adds a logger to this MultiChannelledLog
func (m *MultiChannelledLog) AddLogger(logger EventLogger) error {
- m.mutex.Lock()
+ m.rwmutex.Lock()
name := logger.GetName()
if _, has := m.loggers[name]; has {
- m.mutex.Unlock()
+ m.rwmutex.Unlock()
return ErrDuplicateName{name}
}
m.loggers[name] = logger
if logger.GetStacktraceLevel() < m.stacktraceLevel {
m.stacktraceLevel = logger.GetStacktraceLevel()
}
- m.mutex.Unlock()
+ m.rwmutex.Unlock()
go m.Start()
return nil
}
// NB: If you delete the last sublogger this logger will simply drop
// log events
func (m *MultiChannelledLog) DelLogger(name string) bool {
- m.mutex.Lock()
+ m.rwmutex.Lock()
logger, has := m.loggers[name]
if !has {
- m.mutex.Unlock()
+ m.rwmutex.Unlock()
return false
}
delete(m.loggers, name)
m.internalResetLevel()
- m.mutex.Unlock()
+ m.rwmutex.Unlock()
logger.Flush()
logger.Close()
return true
// GetEventLogger returns a sub logger from this MultiChannelledLog
func (m *MultiChannelledLog) GetEventLogger(name string) EventLogger {
- m.mutex.Lock()
- defer m.mutex.Unlock()
+ m.rwmutex.RLock()
+ defer m.rwmutex.RUnlock()
return m.loggers[name]
}
// GetEventLoggerNames returns a list of names
func (m *MultiChannelledLog) GetEventLoggerNames() []string {
- m.mutex.Lock()
- defer m.mutex.Unlock()
+ m.rwmutex.RLock()
+ defer m.rwmutex.RUnlock()
var keys []string
for k := range m.loggers {
keys = append(keys, k)
}
func (m *MultiChannelledLog) closeLoggers() {
- m.mutex.Lock()
+ m.rwmutex.Lock()
for _, logger := range m.loggers {
logger.Flush()
logger.Close()
}
- m.mutex.Unlock()
+ m.rwmutex.Unlock()
m.closed <- true
}
// ReleaseReopen causes this logger to tell its subloggers to release and reopen
func (m *MultiChannelledLog) ReleaseReopen() error {
- m.mutex.Lock()
- defer m.mutex.Unlock()
+ m.rwmutex.Lock()
+ defer m.rwmutex.Unlock()
var accumulatedErr error
for _, logger := range m.loggers {
if err := logger.ReleaseReopen(); err != nil {
// Start processing the MultiChannelledLog
func (m *MultiChannelledLog) Start() {
- m.mutex.Lock()
+ m.rwmutex.Lock()
if m.started {
- m.mutex.Unlock()
+ m.rwmutex.Unlock()
return
}
m.started = true
- m.mutex.Unlock()
+ m.rwmutex.Unlock()
paused := false
for {
if paused {
m.closeLoggers()
return
}
- m.mutex.Lock()
+ m.rwmutex.RLock()
for _, logger := range m.loggers {
logger.Flush()
}
- m.mutex.Unlock()
+ m.rwmutex.RUnlock()
case <-m.close:
m.closeLoggers()
return
m.closeLoggers()
return
}
- m.mutex.Lock()
+ m.rwmutex.RLock()
for _, logger := range m.loggers {
err := logger.LogEvent(event)
if err != nil {
fmt.Println(err)
}
}
- m.mutex.Unlock()
+ m.rwmutex.RUnlock()
case _, ok := <-m.flush:
if !ok {
m.closeLoggers()
return
}
- m.mutex.Lock()
+ m.rwmutex.RLock()
for _, logger := range m.loggers {
logger.Flush()
}
- m.mutex.Unlock()
+ m.rwmutex.RUnlock()
case <-m.close:
m.closeLoggers()
return
// GetLevel gets the level of this MultiChannelledLog
func (m *MultiChannelledLog) GetLevel() Level {
+ m.rwmutex.RLock()
+ defer m.rwmutex.RUnlock()
return m.level
}
// GetStacktraceLevel gets the level of this MultiChannelledLog
func (m *MultiChannelledLog) GetStacktraceLevel() Level {
+ m.rwmutex.RLock()
+ defer m.rwmutex.RUnlock()
return m.stacktraceLevel
}
// ResetLevel will reset the level of this MultiChannelledLog
func (m *MultiChannelledLog) ResetLevel() Level {
- m.mutex.Lock()
- defer m.mutex.Unlock()
+ m.rwmutex.Lock()
+ defer m.rwmutex.Unlock()
return m.internalResetLevel()
}