Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ddtrace: make UseLogger public #1466

Merged
merged 4 commits into from
Sep 21, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion ddtrace/ddtrace.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ package ddtrace // import "gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
import (
"context"
"time"

"gopkg.in/DataDog/dd-trace-go.v1/internal/log"
)

// Tracer specifies an implementation of the Datadog tracer which allows starting
Expand Down Expand Up @@ -130,8 +132,13 @@ type StartSpanConfig struct {
Context context.Context
}

// Logger implementations are able to log given messages that the tracer might output.
// Logger implementations are able to log given messages that the tracer or profiler might output.
type Logger interface {
// Log prints the given message.
Log(msg string)
}

// UseLogger sets l as the logger for all tracer and profiler logs.
func UseLogger(l Logger) {
log.UseLogger(l)
}
21 changes: 14 additions & 7 deletions internal/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import (
"sync"
"time"

"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"
"gopkg.in/DataDog/dd-trace-go.v1/internal/version"
)

Expand All @@ -30,15 +29,23 @@ const (

var prefixMsg = fmt.Sprintf("Datadog Tracer %s", version.Tag)

// Logger implementations are able to log given messages that the tracer might
// output. This interface is duplicated here to avoid a cyclic dependency
// between this package and ddtrace
type Logger interface {
// Log prints the given message.
Log(msg string)
}

var (
mu sync.RWMutex // guards below fields
level = LevelWarn
logger ddtrace.Logger = &defaultLogger{l: log.New(os.Stderr, "", log.LstdFlags)}
mu sync.RWMutex // guards below fields
level = LevelWarn
logger Logger = &defaultLogger{l: log.New(os.Stderr, "", log.LstdFlags)}
)

// UseLogger sets l as the active logger and returns a function to restore the
// previous logger. The return value is mostly useful when testing.
func UseLogger(l ddtrace.Logger) (undo func()) {
func UseLogger(l Logger) (undo func()) {
Flush()
mu.Lock()
defer mu.Unlock()
Expand Down Expand Up @@ -187,7 +194,7 @@ func (p *defaultLogger) Log(msg string) { p.l.Print(msg) }
// DiscardLogger discards every call to Log().
type DiscardLogger struct{}

// Log implements ddtrace.Logger.
// Log implements Logger.
func (d DiscardLogger) Log(msg string) {}

// RecordLogger records every call to Log() and makes it available via Logs().
Expand All @@ -196,7 +203,7 @@ type RecordLogger struct {
logs []string
}

// Log implements ddtrace.Logger.
// Log implements Logger.
func (r *RecordLogger) Log(msg string) {
r.m.Lock()
defer r.m.Unlock()
Expand Down
8 changes: 3 additions & 5 deletions internal/log/log_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,16 @@ import (
"testing"
"time"

"gopkg.in/DataDog/dd-trace-go.v1/ddtrace"

"github.com/stretchr/testify/assert"
)

// testLogger implements a mock ddtrace.Logger.
// testLogger implements a mock Logger.
type testLogger struct {
mu sync.RWMutex
lines []string
}

// Print implements ddtrace.Logger.
// Print implements Logger.
func (tp *testLogger) Log(msg string) {
tp.mu.Lock()
defer tp.mu.Unlock()
Expand All @@ -45,7 +43,7 @@ func (tp *testLogger) Reset() {
}

func TestLog(t *testing.T) {
defer func(old ddtrace.Logger) { UseLogger(old) }(logger)
defer func(old Logger) { UseLogger(old) }(logger)
tp := &testLogger{}
UseLogger(tp)

Expand Down