1
0
mirror of https://github.com/open-telemetry/opentelemetry-go.git synced 2025-02-19 19:10:13 +02:00

167 lines
3.6 KiB
Go
Raw Normal View History

logtest: Add Recorder (#5134) * introduce in-memory log exporter * add changelog entry * move logtest into a recorder within the api * rename GetRecords to Result * rename InMemoryRecorder to Recorder * name the struct r * ensure Logger creates a struct copy * replace severity with enabledFn * Update CHANGELOG.md Co-authored-by: Robert Pająk <pellared@hotmail.com> * kUpdate log/logtest/config.go Co-authored-by: Robert Pająk <pellared@hotmail.com> * store all scope records, so we can retrieve everything with `Result()` * store child loggers instead of all scope records * no need to explicitly create a new slice * add concurrent safe test * handle default enabled function if the struct was manually created * rename WithEnabledFn to WithEnabledFunc * test result/reset with child loggers * add enabled to concurrent safe * fix lint missing period * rename defaultEnabledFn to defaultEnabledFunc * merge recorder.go and config.go * Update log/logtest/recorder_test.go Co-authored-by: Robert Pająk <pellared@hotmail.com> * create empty recorder in concurrent safe test * Update log/logtest/recorder_test.go Co-authored-by: Robert Pająk <pellared@hotmail.com> * fix lint * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * make enabledFunc callable from outside the package * replace expected with want --------- Co-authored-by: Robert Pająk <pellared@hotmail.com> Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> Co-authored-by: Sam Xie <sam@samxie.me>
2024-04-09 15:45:25 +02:00
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package logtest // import "go.opentelemetry.io/otel/log/logtest"
import (
"context"
"sync"
"go.opentelemetry.io/otel/log"
"go.opentelemetry.io/otel/log/embedded"
)
type enabledFn func(context.Context, log.Record) bool
var defaultEnabledFunc = func(context.Context, log.Record) bool {
return true
}
type config struct {
enabledFn enabledFn
}
func newConfig(options []Option) config {
var c config
for _, opt := range options {
c = opt.apply(c)
}
return c
}
// Option configures a [Recorder].
type Option interface {
apply(config) config
}
type optFunc func(config) config
func (f optFunc) apply(c config) config { return f(c) }
// WithEnabledFunc allows configuring whether the [Recorder] is enabled for specific log entries or not.
//
// By default, the Recorder is enabled for every log entry.
func WithEnabledFunc(fn func(context.Context, log.Record) bool) Option {
return optFunc(func(c config) config {
c.enabledFn = fn
return c
})
}
// NewRecorder returns a new [Recorder].
func NewRecorder(options ...Option) *Recorder {
cfg := newConfig(options)
return &Recorder{
enabledFn: cfg.enabledFn,
logtest: Add Recorder (#5134) * introduce in-memory log exporter * add changelog entry * move logtest into a recorder within the api * rename GetRecords to Result * rename InMemoryRecorder to Recorder * name the struct r * ensure Logger creates a struct copy * replace severity with enabledFn * Update CHANGELOG.md Co-authored-by: Robert Pająk <pellared@hotmail.com> * kUpdate log/logtest/config.go Co-authored-by: Robert Pająk <pellared@hotmail.com> * store all scope records, so we can retrieve everything with `Result()` * store child loggers instead of all scope records * no need to explicitly create a new slice * add concurrent safe test * handle default enabled function if the struct was manually created * rename WithEnabledFn to WithEnabledFunc * test result/reset with child loggers * add enabled to concurrent safe * fix lint missing period * rename defaultEnabledFn to defaultEnabledFunc * merge recorder.go and config.go * Update log/logtest/recorder_test.go Co-authored-by: Robert Pająk <pellared@hotmail.com> * create empty recorder in concurrent safe test * Update log/logtest/recorder_test.go Co-authored-by: Robert Pająk <pellared@hotmail.com> * fix lint * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * make enabledFunc callable from outside the package * replace expected with want --------- Co-authored-by: Robert Pająk <pellared@hotmail.com> Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> Co-authored-by: Sam Xie <sam@samxie.me>
2024-04-09 15:45:25 +02:00
}
}
// ScopeRecords represents the records for a single instrumentation scope.
type ScopeRecords struct {
// Name is the name of the instrumentation scope.
Name string
// Version is the version of the instrumentation scope.
Version string
// SchemaURL of the telemetry emitted by the scope.
SchemaURL string
// Records are the log records this instrumentation scope recorded.
Records []log.Record
}
// Recorder is a recorder that stores all received log records
// in-memory.
type Recorder struct {
embedded.LoggerProvider
mu sync.Mutex
loggers []*logger
logtest: Add Recorder (#5134) * introduce in-memory log exporter * add changelog entry * move logtest into a recorder within the api * rename GetRecords to Result * rename InMemoryRecorder to Recorder * name the struct r * ensure Logger creates a struct copy * replace severity with enabledFn * Update CHANGELOG.md Co-authored-by: Robert Pająk <pellared@hotmail.com> * kUpdate log/logtest/config.go Co-authored-by: Robert Pająk <pellared@hotmail.com> * store all scope records, so we can retrieve everything with `Result()` * store child loggers instead of all scope records * no need to explicitly create a new slice * add concurrent safe test * handle default enabled function if the struct was manually created * rename WithEnabledFn to WithEnabledFunc * test result/reset with child loggers * add enabled to concurrent safe * fix lint missing period * rename defaultEnabledFn to defaultEnabledFunc * merge recorder.go and config.go * Update log/logtest/recorder_test.go Co-authored-by: Robert Pająk <pellared@hotmail.com> * create empty recorder in concurrent safe test * Update log/logtest/recorder_test.go Co-authored-by: Robert Pająk <pellared@hotmail.com> * fix lint * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * make enabledFunc callable from outside the package * replace expected with want --------- Co-authored-by: Robert Pająk <pellared@hotmail.com> Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> Co-authored-by: Sam Xie <sam@samxie.me>
2024-04-09 15:45:25 +02:00
// enabledFn decides whether the recorder should enable logging of a record or not
enabledFn enabledFn
}
// Logger returns a copy of Recorder as a [log.Logger] with the provided scope
// information.
func (r *Recorder) Logger(name string, opts ...log.LoggerOption) log.Logger {
cfg := log.NewLoggerConfig(opts...)
nl := &logger{
scopeRecord: &ScopeRecords{
logtest: Add Recorder (#5134) * introduce in-memory log exporter * add changelog entry * move logtest into a recorder within the api * rename GetRecords to Result * rename InMemoryRecorder to Recorder * name the struct r * ensure Logger creates a struct copy * replace severity with enabledFn * Update CHANGELOG.md Co-authored-by: Robert Pająk <pellared@hotmail.com> * kUpdate log/logtest/config.go Co-authored-by: Robert Pająk <pellared@hotmail.com> * store all scope records, so we can retrieve everything with `Result()` * store child loggers instead of all scope records * no need to explicitly create a new slice * add concurrent safe test * handle default enabled function if the struct was manually created * rename WithEnabledFn to WithEnabledFunc * test result/reset with child loggers * add enabled to concurrent safe * fix lint missing period * rename defaultEnabledFn to defaultEnabledFunc * merge recorder.go and config.go * Update log/logtest/recorder_test.go Co-authored-by: Robert Pająk <pellared@hotmail.com> * create empty recorder in concurrent safe test * Update log/logtest/recorder_test.go Co-authored-by: Robert Pająk <pellared@hotmail.com> * fix lint * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * make enabledFunc callable from outside the package * replace expected with want --------- Co-authored-by: Robert Pająk <pellared@hotmail.com> Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> Co-authored-by: Sam Xie <sam@samxie.me>
2024-04-09 15:45:25 +02:00
Name: name,
Version: cfg.InstrumentationVersion(),
SchemaURL: cfg.SchemaURL(),
},
enabledFn: r.enabledFn,
}
r.addChildLogger(nl)
logtest: Add Recorder (#5134) * introduce in-memory log exporter * add changelog entry * move logtest into a recorder within the api * rename GetRecords to Result * rename InMemoryRecorder to Recorder * name the struct r * ensure Logger creates a struct copy * replace severity with enabledFn * Update CHANGELOG.md Co-authored-by: Robert Pająk <pellared@hotmail.com> * kUpdate log/logtest/config.go Co-authored-by: Robert Pająk <pellared@hotmail.com> * store all scope records, so we can retrieve everything with `Result()` * store child loggers instead of all scope records * no need to explicitly create a new slice * add concurrent safe test * handle default enabled function if the struct was manually created * rename WithEnabledFn to WithEnabledFunc * test result/reset with child loggers * add enabled to concurrent safe * fix lint missing period * rename defaultEnabledFn to defaultEnabledFunc * merge recorder.go and config.go * Update log/logtest/recorder_test.go Co-authored-by: Robert Pająk <pellared@hotmail.com> * create empty recorder in concurrent safe test * Update log/logtest/recorder_test.go Co-authored-by: Robert Pająk <pellared@hotmail.com> * fix lint * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * make enabledFunc callable from outside the package * replace expected with want --------- Co-authored-by: Robert Pająk <pellared@hotmail.com> Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> Co-authored-by: Sam Xie <sam@samxie.me>
2024-04-09 15:45:25 +02:00
return nl
logtest: Add Recorder (#5134) * introduce in-memory log exporter * add changelog entry * move logtest into a recorder within the api * rename GetRecords to Result * rename InMemoryRecorder to Recorder * name the struct r * ensure Logger creates a struct copy * replace severity with enabledFn * Update CHANGELOG.md Co-authored-by: Robert Pająk <pellared@hotmail.com> * kUpdate log/logtest/config.go Co-authored-by: Robert Pająk <pellared@hotmail.com> * store all scope records, so we can retrieve everything with `Result()` * store child loggers instead of all scope records * no need to explicitly create a new slice * add concurrent safe test * handle default enabled function if the struct was manually created * rename WithEnabledFn to WithEnabledFunc * test result/reset with child loggers * add enabled to concurrent safe * fix lint missing period * rename defaultEnabledFn to defaultEnabledFunc * merge recorder.go and config.go * Update log/logtest/recorder_test.go Co-authored-by: Robert Pająk <pellared@hotmail.com> * create empty recorder in concurrent safe test * Update log/logtest/recorder_test.go Co-authored-by: Robert Pająk <pellared@hotmail.com> * fix lint * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * make enabledFunc callable from outside the package * replace expected with want --------- Co-authored-by: Robert Pająk <pellared@hotmail.com> Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> Co-authored-by: Sam Xie <sam@samxie.me>
2024-04-09 15:45:25 +02:00
}
func (r *Recorder) addChildLogger(nl *logger) {
logtest: Add Recorder (#5134) * introduce in-memory log exporter * add changelog entry * move logtest into a recorder within the api * rename GetRecords to Result * rename InMemoryRecorder to Recorder * name the struct r * ensure Logger creates a struct copy * replace severity with enabledFn * Update CHANGELOG.md Co-authored-by: Robert Pająk <pellared@hotmail.com> * kUpdate log/logtest/config.go Co-authored-by: Robert Pająk <pellared@hotmail.com> * store all scope records, so we can retrieve everything with `Result()` * store child loggers instead of all scope records * no need to explicitly create a new slice * add concurrent safe test * handle default enabled function if the struct was manually created * rename WithEnabledFn to WithEnabledFunc * test result/reset with child loggers * add enabled to concurrent safe * fix lint missing period * rename defaultEnabledFn to defaultEnabledFunc * merge recorder.go and config.go * Update log/logtest/recorder_test.go Co-authored-by: Robert Pająk <pellared@hotmail.com> * create empty recorder in concurrent safe test * Update log/logtest/recorder_test.go Co-authored-by: Robert Pająk <pellared@hotmail.com> * fix lint * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * make enabledFunc callable from outside the package * replace expected with want --------- Co-authored-by: Robert Pająk <pellared@hotmail.com> Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> Co-authored-by: Sam Xie <sam@samxie.me>
2024-04-09 15:45:25 +02:00
r.mu.Lock()
defer r.mu.Unlock()
r.loggers = append(r.loggers, nl)
logtest: Add Recorder (#5134) * introduce in-memory log exporter * add changelog entry * move logtest into a recorder within the api * rename GetRecords to Result * rename InMemoryRecorder to Recorder * name the struct r * ensure Logger creates a struct copy * replace severity with enabledFn * Update CHANGELOG.md Co-authored-by: Robert Pająk <pellared@hotmail.com> * kUpdate log/logtest/config.go Co-authored-by: Robert Pająk <pellared@hotmail.com> * store all scope records, so we can retrieve everything with `Result()` * store child loggers instead of all scope records * no need to explicitly create a new slice * add concurrent safe test * handle default enabled function if the struct was manually created * rename WithEnabledFn to WithEnabledFunc * test result/reset with child loggers * add enabled to concurrent safe * fix lint missing period * rename defaultEnabledFn to defaultEnabledFunc * merge recorder.go and config.go * Update log/logtest/recorder_test.go Co-authored-by: Robert Pająk <pellared@hotmail.com> * create empty recorder in concurrent safe test * Update log/logtest/recorder_test.go Co-authored-by: Robert Pająk <pellared@hotmail.com> * fix lint * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * make enabledFunc callable from outside the package * replace expected with want --------- Co-authored-by: Robert Pająk <pellared@hotmail.com> Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> Co-authored-by: Sam Xie <sam@samxie.me>
2024-04-09 15:45:25 +02:00
}
// Result returns the current in-memory recorder log records.
func (r *Recorder) Result() []*ScopeRecords {
r.mu.Lock()
defer r.mu.Unlock()
ret := []*ScopeRecords{}
for _, l := range r.loggers {
ret = append(ret, l.scopeRecord)
logtest: Add Recorder (#5134) * introduce in-memory log exporter * add changelog entry * move logtest into a recorder within the api * rename GetRecords to Result * rename InMemoryRecorder to Recorder * name the struct r * ensure Logger creates a struct copy * replace severity with enabledFn * Update CHANGELOG.md Co-authored-by: Robert Pająk <pellared@hotmail.com> * kUpdate log/logtest/config.go Co-authored-by: Robert Pająk <pellared@hotmail.com> * store all scope records, so we can retrieve everything with `Result()` * store child loggers instead of all scope records * no need to explicitly create a new slice * add concurrent safe test * handle default enabled function if the struct was manually created * rename WithEnabledFn to WithEnabledFunc * test result/reset with child loggers * add enabled to concurrent safe * fix lint missing period * rename defaultEnabledFn to defaultEnabledFunc * merge recorder.go and config.go * Update log/logtest/recorder_test.go Co-authored-by: Robert Pająk <pellared@hotmail.com> * create empty recorder in concurrent safe test * Update log/logtest/recorder_test.go Co-authored-by: Robert Pająk <pellared@hotmail.com> * fix lint * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * make enabledFunc callable from outside the package * replace expected with want --------- Co-authored-by: Robert Pająk <pellared@hotmail.com> Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> Co-authored-by: Sam Xie <sam@samxie.me>
2024-04-09 15:45:25 +02:00
}
return ret
}
// Reset clears the in-memory log records for all loggers.
logtest: Add Recorder (#5134) * introduce in-memory log exporter * add changelog entry * move logtest into a recorder within the api * rename GetRecords to Result * rename InMemoryRecorder to Recorder * name the struct r * ensure Logger creates a struct copy * replace severity with enabledFn * Update CHANGELOG.md Co-authored-by: Robert Pająk <pellared@hotmail.com> * kUpdate log/logtest/config.go Co-authored-by: Robert Pająk <pellared@hotmail.com> * store all scope records, so we can retrieve everything with `Result()` * store child loggers instead of all scope records * no need to explicitly create a new slice * add concurrent safe test * handle default enabled function if the struct was manually created * rename WithEnabledFn to WithEnabledFunc * test result/reset with child loggers * add enabled to concurrent safe * fix lint missing period * rename defaultEnabledFn to defaultEnabledFunc * merge recorder.go and config.go * Update log/logtest/recorder_test.go Co-authored-by: Robert Pająk <pellared@hotmail.com> * create empty recorder in concurrent safe test * Update log/logtest/recorder_test.go Co-authored-by: Robert Pająk <pellared@hotmail.com> * fix lint * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * Update log/logtest/recorder.go Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> * make enabledFunc callable from outside the package * replace expected with want --------- Co-authored-by: Robert Pająk <pellared@hotmail.com> Co-authored-by: Tyler Yahn <MrAlias@users.noreply.github.com> Co-authored-by: Sam Xie <sam@samxie.me>
2024-04-09 15:45:25 +02:00
func (r *Recorder) Reset() {
r.mu.Lock()
defer r.mu.Unlock()
for _, l := range r.loggers {
l.Reset()
}
}
type logger struct {
embedded.Logger
mu sync.Mutex
scopeRecord *ScopeRecords
// enabledFn decides whether the recorder should enable logging of a record or not.
enabledFn enabledFn
}
// Enabled indicates whether a specific record should be stored.
func (l *logger) Enabled(ctx context.Context, record log.Record) bool {
if l.enabledFn == nil {
return defaultEnabledFunc(ctx, record)
}
return l.enabledFn(ctx, record)
}
// Emit stores the log record.
func (l *logger) Emit(_ context.Context, record log.Record) {
l.mu.Lock()
defer l.mu.Unlock()
l.scopeRecord.Records = append(l.scopeRecord.Records, record)
}
// Reset clears the in-memory log records.
func (l *logger) Reset() {
l.mu.Lock()
defer l.mu.Unlock()
l.scopeRecord.Records = nil
}