Skip to content

Commit

Permalink
Implement Jaeger-client Logger with go-kit level logger (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
sagikazarmark authored and yurishkuro committed Sep 20, 2017
1 parent 5b29ee5 commit 21a3da6
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 1 deletion.
64 changes: 64 additions & 0 deletions client/log/go-kit/logger.go
@@ -0,0 +1,64 @@
// Copyright (c) 2017 Uber Technologies, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package xkit

import (
"fmt"

"github.com/go-kit/kit/log"
"github.com/go-kit/kit/log/level"
)

// LoggerOption sets a parameter for the Logger.
type LoggerOption func(*Logger)

// MessageKey sets the key for the actual log message. By default, it's "msg".
func MessageKey(key string) LoggerOption {
return func(l *Logger) { l.messageKey = key }
}

// Logger wraps a go-kit logger instance in a Jaeger client compatible one.
type Logger struct {
infoLogger log.Logger
errorLogger log.Logger

messageKey string
}

// NewLogger creates a new Jaeger client logger from a go-kit one.
func NewLogger(kitlogger log.Logger, options ...LoggerOption) *Logger {
logger := &Logger{
infoLogger: level.Info(kitlogger),
errorLogger: level.Error(kitlogger),

messageKey: "msg",
}

for _, option := range options {
option(logger)
}

return logger
}

// Error implements the github.com/uber/jaeger-client-go/log.Logger interface.
func (l *Logger) Error(msg string) {
l.errorLogger.Log(l.messageKey, msg)
}

// Infof implements the github.com/uber/jaeger-client-go/log.Logger interface.
func (l *Logger) Infof(msg string, args ...interface{}) {
l.infoLogger.Log(l.messageKey, fmt.Sprintf(msg, args...))
}
36 changes: 36 additions & 0 deletions client/log/go-kit/logger_test.go
@@ -0,0 +1,36 @@
package xkit

import (
"bytes"
"testing"

"github.com/go-kit/kit/log"
"github.com/stretchr/testify/assert"
)

func TestLogger_Infof(t *testing.T) {
buf := &bytes.Buffer{}
logger := NewLogger(log.NewLogfmtLogger(buf))

logger.Infof("Formatted %s", "string")

assert.Equal(t, "level=info msg=\"Formatted string\"\n", buf.String())
}

func TestLogger_Error(t *testing.T) {
buf := &bytes.Buffer{}
logger := NewLogger(log.NewLogfmtLogger(buf))

logger.Error("Something really bad happened")

assert.Equal(t, "level=error msg=\"Something really bad happened\"\n", buf.String())
}

func TestMessageKey(t *testing.T) {
buf := &bytes.Buffer{}
logger := NewLogger(log.NewLogfmtLogger(buf), MessageKey("message"))

logger.Error("Something really bad happened")

assert.Equal(t, "level=error message=\"Something really bad happened\"\n", buf.String())
}
3 changes: 2 additions & 1 deletion glide.lock

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

0 comments on commit 21a3da6

Please sign in to comment.