Skip to content

Commit

Permalink
ddtrace: make UseLogger public (#1466)
Browse files Browse the repository at this point in the history
Make the internal UseLogger function public so that users can specify a
single logger for all tracer and profiler logs. Right now the profiler
doesn't have a configurable logger, and the tracer has the WithLogger
option, which sets the global logger anyway.

Fixes #1331.
  • Loading branch information
nsrip-dd committed Sep 21, 2022
1 parent 448d06c commit dd30c5c
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 13 deletions.
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

0 comments on commit dd30c5c

Please sign in to comment.