Skip to content
This repository has been archived by the owner on May 23, 2024. It is now read-only.

Add zap logger support #93

Merged
merged 4 commits into from
Feb 17, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
3 changes: 2 additions & 1 deletion config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (
"github.com/uber/jaeger-lib/metrics"

"github.com/uber/jaeger-client-go"
"github.com/uber/jaeger-client-go/log"
)

func TestNewSamplerConst(t *testing.T) {
Expand Down Expand Up @@ -82,7 +83,7 @@ func TestInvalidSamplerType(t *testing.T) {

func TestDefaultConfig(t *testing.T) {
cfg := Configuration{}
_, _, err := cfg.New("", Metrics(metrics.NullFactory), Logger(jaeger.NullLogger))
_, _, err := cfg.New("", Metrics(metrics.NullFactory), Logger(log.NullLogger))
require.EqualError(t, err, "no service name provided")

_, closer, err := cfg.New("testService")
Expand Down
3 changes: 2 additions & 1 deletion crossdock/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/uber/jaeger-client-go/crossdock/common"
"github.com/uber/jaeger-client-go/crossdock/log"
"github.com/uber/jaeger-client-go/crossdock/server"
jlog "github.com/uber/jaeger-client-go/log"
)

func TestCrossdock(t *testing.T) {
Expand All @@ -40,7 +41,7 @@ func TestCrossdock(t *testing.T) {

var reporter jaeger.Reporter
if log.Enabled {
reporter = jaeger.NewLoggingReporter(jaeger.StdLogger)
reporter = jaeger.NewLoggingReporter(jlog.StdLogger)
} else {
reporter = jaeger.NewNullReporter()
}
Expand Down
3 changes: 2 additions & 1 deletion crossdock/endtoend/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (

"github.com/uber/jaeger-client-go"
"github.com/uber/jaeger-client-go/config"
"github.com/uber/jaeger-client-go/log"
)

var (
Expand Down Expand Up @@ -99,7 +100,7 @@ func newInMemoryTracer() (opentracing.Tracer, *jaeger.InMemoryReporter) {
jaeger.NewConstSampler(true),
inMemoryReporter,
jaeger.TracerOptions.Metrics(jaeger.NewNullMetrics()),
jaeger.TracerOptions.Logger(jaeger.NullLogger))
jaeger.TracerOptions.Logger(log.NullLogger))
return tracer, inMemoryReporter
}

Expand Down
3 changes: 2 additions & 1 deletion crossdock/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/uber/jaeger-client-go/crossdock/common"
"github.com/uber/jaeger-client-go/crossdock/log"
"github.com/uber/jaeger-client-go/crossdock/server"
jlog "github.com/uber/jaeger-client-go/log"
)

func main() {
Expand All @@ -54,6 +55,6 @@ func initTracer() (opentracing.Tracer, io.Closer) {
t, c := jaeger.NewTracer(
common.DefaultTracerServiceName,
jaeger.NewConstSampler(false),
jaeger.NewLoggingReporter(jaeger.StdLogger))
jaeger.NewLoggingReporter(jlog.StdLogger))
return t, c
}
14 changes: 12 additions & 2 deletions glide.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

57 changes: 57 additions & 0 deletions log/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// Copyright (c) 2016 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package log

import "log"

// Logger provides an abstract interface for logging from Reporters.
// Applications can provide their own implementation of this interface to adapt
// reporters logging to whatever logging library they prefer (stdlib log,
// logrus, go-logging, etc).
type Logger interface {
// Error logs a message at error priority
Error(msg string)

// Infof logs a message at info priority
Infof(msg string, args ...interface{})
}

// StdLogger is implementation of the Logger interface that delegates to default `log` package
var StdLogger = &stdLogger{}

type stdLogger struct{}

func (l *stdLogger) Error(msg string) {
log.Printf("ERROR: %s", msg)
}

// Infof logs a message at info priority
func (l *stdLogger) Infof(msg string, args ...interface{}) {
log.Printf(msg, args...)
}

// NullLogger is implementation of the Logger interface that delegates to default `log` package
var NullLogger = &nullLogger{}

type nullLogger struct{}

func (l *nullLogger) Error(msg string) {}
func (l *nullLogger) Infof(msg string, args ...interface{}) {}
32 changes: 32 additions & 0 deletions log/logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package log

import (
"testing"
)

func TestLogger(t *testing.T) {
for _, logger := range []Logger{StdLogger, NullLogger} {
logger.Infof("Hi %s", "there")
logger.Error("Bad wolf")
}
}
47 changes: 47 additions & 0 deletions log/zap/logger.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package zap

import (
"fmt"

"go.uber.org/zap"
)

// Logger is an adapter from zap Logger to jaeger-lib Logger.
type Logger struct {
logger zap.Logger
}

// NewLogger creates a new Logger.
func NewLogger(logger zap.Logger) *Logger {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The exported function should take a *zap.Logger, since that's what all of zap's public APIs return.

return &Logger{logger: logger}
}

// Error logs a message at error priority
func (l *Logger) Error(msg string) {
l.logger.Error(msg)
}

// Infof logs a message at info priority
func (l *Logger) Infof(msg string, args ...interface{}) {
l.logger.Info(fmt.Sprintf(msg, args))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please first check if Info is enabled, since we don't always want to incur the overhead of string formatting:

if l.logger.Facility().Enabled(zap.InfoLevel) {
  l.logger.Info(fmt.Sprintf(msg, args))
}

In general, if you're going to do formatted logging, you're better off wrapping *zap.SugaredLogger, which already has an efficient version of this API. Accepting that type also makes it clear to users that you're planning to do printf-style logging. Every *zap.Logger can easily and cheaply be converted to a *zap.SugaredLogger.

}
34 changes: 34 additions & 0 deletions log/zap/logger_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.

package zap

import (
"testing"

"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)

func TestLogger(t *testing.T) {
logger := NewLogger(*zap.New(zapcore.NewNopCore()))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

zap.New(nil) instead.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(Or, after uber-go/zap#326 lands, NewNop.)

logger.Infof("Hi %s", "there")
logger.Error("Bad wolf")
}
2 changes: 2 additions & 0 deletions logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ package jaeger

import "log"

// NB This will be deprecated in 3.0.0, please use jaeger-client-go/log/logger instead.

// Logger provides an abstract interface for logging from Reporters.
// Applications can provide their own implementation of this interface to adapt
// reporters logging to whatever logging library they prefer (stdlib log,
Expand Down
3 changes: 2 additions & 1 deletion reporter.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (

"github.com/opentracing/opentracing-go"

"github.com/uber/jaeger-client-go/log"
"github.com/uber/jaeger-client-go/thrift-gen/zipkincore"
"github.com/uber/jaeger-client-go/transport"
)
Expand Down Expand Up @@ -182,7 +183,7 @@ func NewRemoteReporter(sender transport.Transport, opts ...ReporterOption) Repor
options.bufferFlushInterval = defaultBufferFlushInterval
}
if options.logger == nil {
options.logger = NullLogger
options.logger = log.NullLogger
}
if options.metrics == nil {
options.metrics = NewNullMetrics()
Expand Down
4 changes: 3 additions & 1 deletion reporter_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

package jaeger

import "time"
import (
"time"
)

// ReporterOption is a function that sets some option on the reporter.
type ReporterOption func(c *reporterOptions)
Expand Down
3 changes: 2 additions & 1 deletion sampler.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"sync"
"time"

"github.com/uber/jaeger-client-go/log"
"github.com/uber/jaeger-client-go/thrift-gen/sampling"
"github.com/uber/jaeger-client-go/utils"
)
Expand Down Expand Up @@ -428,7 +429,7 @@ func applySamplerOptions(opts ...SamplerOption) samplerOptions {
options.sampler = initialSampler
}
if options.logger == nil {
options.logger = NullLogger
options.logger = log.NullLogger
}
if options.maxOperations <= 0 {
options.maxOperations = defaultMaxOperations
Expand Down
4 changes: 3 additions & 1 deletion sampler_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

package jaeger

import "time"
import (
"time"
)

// SamplerOption is a function that sets some option on the sampler
type SamplerOption func(options *samplerOptions)
Expand Down
3 changes: 2 additions & 1 deletion sampler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/uber/jaeger-lib/metrics"
mTestutils "github.com/uber/jaeger-lib/metrics/testutils"

"github.com/uber/jaeger-client-go/log"
"github.com/uber/jaeger-client-go/testutils"
"github.com/uber/jaeger-client-go/thrift-gen/sampling"
"github.com/uber/jaeger-client-go/utils"
Expand Down Expand Up @@ -293,7 +294,7 @@ func initAgent(t *testing.T) (*testutils.MockAgent, *RemotelyControlledSampler,
SamplerOptions.SamplingServerURL("http://"+agent.SamplingServerAddr()),
SamplerOptions.MaxOperations(testDefaultMaxOperations),
SamplerOptions.InitialSampler(initialSampler),
SamplerOptions.Logger(NullLogger),
SamplerOptions.Logger(log.NullLogger),
SamplerOptions.SamplingRefreshInterval(time.Minute),
)
sampler.Close() // stop timer-based updates, we want to call them manually
Expand Down
5 changes: 3 additions & 2 deletions tracer.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
"github.com/opentracing/opentracing-go"
"github.com/opentracing/opentracing-go/ext"

"github.com/uber/jaeger-client-go/log"
"github.com/uber/jaeger-client-go/utils"
)

Expand All @@ -41,7 +42,7 @@ type tracer struct {
sampler Sampler
reporter Reporter
metrics Metrics
logger Logger
logger log.Logger

timeNow func() time.Time
randomNumber func() uint64
Expand Down Expand Up @@ -117,7 +118,7 @@ func NewTracer(
t.timeNow = time.Now
}
if t.logger == nil {
t.logger = NullLogger
t.logger = log.NullLogger
}
// TODO once on the new data model, support both v4 and v6 IPs
if t.hostIPv4 == 0 {
Expand Down
4 changes: 3 additions & 1 deletion tracer_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,9 @@

package jaeger

import "time"
import (
"time"
)

// TracerOption is a function that sets some option on the tracer
type TracerOption func(tracer *tracer)
Expand Down
Loading