Skip to content

Commit

Permalink
feat: add SetFactory protected by mutex
Browse files Browse the repository at this point in the history
  • Loading branch information
Crocmagnon authored and fsamin committed Dec 16, 2024
1 parent d5f7e5e commit 5bfe858
Showing 1 changed file with 41 additions and 17 deletions.
58 changes: 41 additions & 17 deletions log.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,19 @@ const (
)

var global *Logger
var Factory WrapperFactoryFunc = NewLogrusWrapper(logrus.StandardLogger())
var Factory = NewLogrusWrapper(logrus.StandardLogger())

func init() {
global = New()
global.callerFrameToSkip = 3
}

type Logger struct {
registeredFields []Field
registeredFieldsMutex sync.RWMutex
excludeRules []ExcludeRule
excludeRulesMutex sync.RWMutex
factory WrapperFactoryFunc
callerFrameToSkip int
registeredFields []Field
excludeRules []ExcludeRule
factory WrapperFactoryFunc
callerFrameToSkip int
mutex sync.RWMutex
}

func New() *Logger {
Expand All @@ -46,16 +45,22 @@ func NewWithFactory(factory WrapperFactoryFunc) *Logger {
}

func (l *Logger) GetFramesToSkip() int {
l.mutex.RLock()
defer l.mutex.RUnlock()

return l.callerFrameToSkip
}

func (l *Logger) SetFramesToSkip(s int) {
l.mutex.Lock()
defer l.mutex.Unlock()

l.callerFrameToSkip = s
}

func (l *Logger) RegisterField(fields ...Field) {
l.registeredFieldsMutex.Lock()
defer l.registeredFieldsMutex.Unlock()
l.mutex.Lock()
defer l.mutex.Unlock()

for _, f := range fields {
var exist bool
Expand All @@ -76,8 +81,8 @@ func (l *Logger) RegisterField(fields ...Field) {
}

func (l *Logger) UnregisterField(fields ...Field) {
l.registeredFieldsMutex.Lock()
defer l.registeredFieldsMutex.Unlock()
l.mutex.Lock()
defer l.mutex.Unlock()

loop:
for _, f := range fields {
Expand All @@ -95,16 +100,18 @@ loop:
}

func (l *Logger) GetRegisteredFields() []Field {
l.registeredFieldsMutex.RLock()
defer l.registeredFieldsMutex.RUnlock()
l.mutex.RLock()
defer l.mutex.RUnlock()

fields := make([]Field, len(l.registeredFields))
copy(fields, l.registeredFields)
return fields
}

func (l *Logger) GetExcludeRules() []ExcludeRule {
l.excludeRulesMutex.RLock()
defer l.excludeRulesMutex.RUnlock()
l.mutex.RLock()
defer l.mutex.RUnlock()

excludeRules := make([]ExcludeRule, len(l.excludeRules))
copy(excludeRules, l.excludeRules)
return excludeRules
Expand All @@ -115,8 +122,9 @@ func (l *Logger) RegisterDefaultFields() {
}

func (l *Logger) Skip(field Field, value interface{}) {
l.excludeRulesMutex.Lock()
defer l.excludeRulesMutex.Unlock()
l.mutex.Lock()
defer l.mutex.Unlock()

for i := range l.excludeRules {
if l.excludeRules[i].Field == field {
l.excludeRules[i].Value = value
Expand All @@ -126,6 +134,13 @@ func (l *Logger) Skip(field Field, value interface{}) {
l.excludeRules = append(l.excludeRules, ExcludeRule{field, value})
}

func (l *Logger) SetFactory(factory WrapperFactoryFunc) {
l.mutex.Lock()
defer l.mutex.Unlock()

l.factory = factory
}

func (l *Logger) Debug(ctx context.Context, format string, args ...interface{}) {
l.call(ctx, LevelDebug, format, args...)
}
Expand All @@ -152,12 +167,17 @@ func (l *Logger) Panic(ctx context.Context, format string, args ...interface{})

func (l *Logger) call(ctx context.Context, level Level, format string, args ...interface{}) {
var entry Wrapper

l.mutex.RLock()

if l.factory == nil {
entry = Factory()
} else {
entry = l.factory()
}

l.mutex.RUnlock()

if level < entry.GetLevel() {
return
}
Expand Down Expand Up @@ -266,6 +286,10 @@ func Skip(field Field, value interface{}) {
global.Skip(field, value)
}

func SetFactory(factory WrapperFactoryFunc) {
global.SetFactory(factory)
}

func Debug(ctx context.Context, format string, args ...interface{}) {
global.Debug(ctx, format, args...)
}
Expand Down

0 comments on commit 5bfe858

Please sign in to comment.