Files
gocryptotrader/log/sublogger.go
Ryan O'Hara-Reid ac692b04f4 log: fix bug, expand test coverage and slightly optimize (#847)
* log: fix bugs expand coverage and optimise

* log: fix linter issues

* log: fix linter issue and pack methods in same file

* log: drop defer

* logger: move global check inside getfields  and remove unused test function

* logger: Increase note thanks @gloriouscode

* logger: wrap error with writer type

* logger: change variable name

* logger: change variable names and remove validsublogger func as it doesn't add functionality over a standard map call

* logs: error when unsupported output is applied on setup calls

* logs: add glorious suggestion

* logger: add protection to reduce olympic gold medal races

* logger: fix linter issues

* log: glorious niterinos
2021-11-30 16:43:27 +11:00

67 lines
1.3 KiB
Go

package log
import (
"io"
"strings"
)
// NewSubLogger allows for a new sub logger to be registered.
func NewSubLogger(name string) (*SubLogger, error) {
if name == "" {
return nil, errEmptyLoggerName
}
name = strings.ToUpper(name)
RWM.RLock()
if _, ok := SubLoggers[name]; ok {
RWM.RUnlock()
return nil, errSubLoggerAlreadyregistered
}
RWM.RUnlock()
return registerNewSubLogger(name), nil
}
// SetOutput overrides the default output with a new writer
func (sl *SubLogger) SetOutput(o io.Writer) {
sl.mtx.Lock()
sl.output = o
sl.mtx.Unlock()
}
// SetLevels overrides the default levels with new levels; levelception
func (sl *SubLogger) SetLevels(newLevels Levels) {
sl.mtx.Lock()
sl.levels = newLevels
sl.mtx.Unlock()
}
// GetLevels returns current functional log levels
func (sl *SubLogger) GetLevels() Levels {
sl.mtx.RLock()
defer sl.mtx.RUnlock()
return sl.levels
}
func (sl *SubLogger) getFields() *logFields {
RWM.RLock()
defer RWM.RUnlock()
if sl == nil ||
(GlobalLogConfig != nil &&
GlobalLogConfig.Enabled != nil &&
!*GlobalLogConfig.Enabled) {
return nil
}
sl.mtx.RLock()
defer sl.mtx.RUnlock()
return &logFields{
info: sl.levels.Info,
warn: sl.levels.Warn,
debug: sl.levels.Debug,
error: sl.levels.Error,
name: sl.name,
output: sl.output,
logger: logger,
}
}