Skip to content

Commit

Permalink
Update the LoggerProvider and Logger
Browse files Browse the repository at this point in the history
  • Loading branch information
MrAlias committed Aug 7, 2024
1 parent 3521db9 commit 542fda9
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 7 deletions.
12 changes: 5 additions & 7 deletions sdk/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,12 @@ func (l *logger) Emit(ctx context.Context, r log.Record) {
}
}

func (l *logger) Enabled(ctx context.Context, r log.Record) bool {
newRecord := l.newRecord(ctx, r)
for _, p := range l.provider.processors {
if enabled := p.Enabled(ctx, newRecord); enabled {
return true
}
func (l *logger) Enabled(ctx context.Context, record log.Record) bool {
if l.provider.enabledFn != nil {
r := l.newRecord(ctx, record)
return l.provider.enabledFn(ctx, r)
}
return false
return len(l.provider.processors) > 0
}

func (l *logger) newRecord(ctx context.Context, r log.Record) Record {
Expand Down
26 changes: 26 additions & 0 deletions sdk/log/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,38 @@ func newProviderConfig(opts []LoggerProviderOption) providerConfig {
return c
}

type enabledFn func(context.Context, Record) bool

func (c providerConfig) enabledFn() enabledFn {
var filters []enabledFn
for _, p := range c.processors {
if f, ok := p.(FilterProcessor); ok {
filters = append(filters, f.Enabled)
}
}

if len(filters) <= 0 {
return nil
}

return func(ctx context.Context, r Record) bool {
for _, f := range filters {
if !f(ctx, r) {
return false
}
}
return true
}
}

// LoggerProvider handles the creation and coordination of Loggers. All Loggers
// created by a LoggerProvider will be associated with the same Resource.
type LoggerProvider struct {
embedded.LoggerProvider

resource *resource.Resource
processors []Processor
enabledFn enabledFn
attributeCountLimit int
attributeValueLengthLimit int

Expand All @@ -85,6 +110,7 @@ func NewLoggerProvider(opts ...LoggerProviderOption) *LoggerProvider {
return &LoggerProvider{
resource: cfg.resource,
processors: cfg.processors,
enabledFn: cfg.enabledFn(),
attributeCountLimit: cfg.attrCntLim.Value,
attributeValueLengthLimit: cfg.attrValLenLim.Value,
}
Expand Down

0 comments on commit 542fda9

Please sign in to comment.