diff --git a/sdk/log/logger.go b/sdk/log/logger.go index 04c44ac5bb85..05bb5da218c6 100644 --- a/sdk/log/logger.go +++ b/sdk/log/logger.go @@ -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 { diff --git a/sdk/log/provider.go b/sdk/log/provider.go index eb1d98acf37c..2ba8b8f499a5 100644 --- a/sdk/log/provider.go +++ b/sdk/log/provider.go @@ -55,6 +55,30 @@ 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 { @@ -62,6 +86,7 @@ type LoggerProvider struct { resource *resource.Resource processors []Processor + enabledFn enabledFn attributeCountLimit int attributeValueLengthLimit int @@ -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, }