Skip to content

Commit

Permalink
Replace kit/log with log (#1173)
Browse files Browse the repository at this point in the history
* Implement log/... packages with github.com/go-kit/log

* Use github.com/go-kit/log/... in all the other packages
  • Loading branch information
ChrisHines committed Aug 18, 2021
1 parent 3220134 commit 801da84
Show file tree
Hide file tree
Showing 112 changed files with 217 additions and 3,170 deletions.
2 changes: 1 addition & 1 deletion auth/jwt/README.md
Expand Up @@ -95,7 +95,7 @@ import (
"context"

"github.com/go-kit/kit/auth/jwt"
"github.com/go-kit/kit/log"
"github.com/go-kit/log"
grpctransport "github.com/go-kit/kit/transport/grpc"
)

Expand Down
3 changes: 1 addition & 2 deletions go.mod
Expand Up @@ -16,8 +16,7 @@ require (
github.com/edsrzf/mmap-go v1.0.0 // indirect
github.com/franela/goblin v0.0.0-20200105215937-c9ffbefa60db // indirect
github.com/franela/goreq v0.0.0-20171204163338-bcd34c9993f8 // indirect
github.com/go-logfmt/logfmt v0.5.0
github.com/go-stack/stack v1.8.0
github.com/go-kit/log v0.1.0
github.com/go-zookeeper/zk v1.0.2
github.com/hashicorp/consul/api v1.8.1
github.com/hudl/fargo v1.3.0
Expand Down
1 change: 1 addition & 0 deletions go.sum
Expand Up @@ -79,6 +79,7 @@ github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMo
github.com/ghodss/yaml v1.0.0/go.mod h1:4dBDuWmgqj2HViK6kFavaiC9ZROes6MMH2rRYeMEF04=
github.com/go-kit/kit v0.8.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-kit/kit v0.9.0/go.mod h1:xBxKIO96dXMWWy0MnWVtmwkA9/13aqxPnvrjFYMA2as=
github.com/go-kit/log v0.1.0 h1:DGJh0Sm43HbOeYDNnVZFl8BvcYVvjD5bqYJvp0REbwQ=
github.com/go-kit/log v0.1.0/go.mod h1:zbhenjAZHb184qTLMA9ZjW7ThYL0H2mk7Q6pNt4vbaY=
github.com/go-logfmt/logfmt v0.3.0/go.mod h1:Qt1PoO58o5twSAckw1HlFXLmHsOX5/0LbT9GBnD5lWE=
github.com/go-logfmt/logfmt v0.4.0/go.mod h1:3RMwSq7FuexP4Kalkev3ejPJsZTpXXBr9+V4qmtdjCk=
Expand Down
9 changes: 9 additions & 0 deletions log/README.md
@@ -1,5 +1,14 @@
# package log

**Deprecation notice:** The core Go kit log packages (log, log/level, log/term, and
log/syslog) have been moved to their own repository at github.com/go-kit/log.
The corresponding packages in this directory remain for backwards compatibility.
Their types alias the types and their functions call the functions provided by
the new repository. Using either import path should be equivalent. Prefer the
new import path when practical.

______

`package log` provides a minimal interface for structured logging in services.
It may be wrapped to encode conventions, enforce type-safety, provide leveled
logging, and so on. It can be used for both typical application log events,
Expand Down
21 changes: 0 additions & 21 deletions log/benchmark_test.go

This file was deleted.

40 changes: 0 additions & 40 deletions log/concurrency_test.go

This file was deleted.

5 changes: 4 additions & 1 deletion log/deprecated_levels/levels.go
@@ -1,6 +1,9 @@
// Package levels implements leveled logging on top of Go kit's log package.
//
// Deprecated: Use github.com/go-kit/log/level instead.
package levels

import "github.com/go-kit/kit/log"
import "github.com/go-kit/log"

// Levels provides a leveled logging wrapper around a logger. It has five
// levels: debug, info, warning (warn), error, and critical (crit). If you
Expand Down
2 changes: 1 addition & 1 deletion log/deprecated_levels/levels_test.go
Expand Up @@ -5,8 +5,8 @@ import (
"os"
"testing"

"github.com/go-kit/kit/log"
levels "github.com/go-kit/kit/log/deprecated_levels"
"github.com/go-kit/log"
)

func TestDefaultLevels(t *testing.T) {
Expand Down
2 changes: 2 additions & 0 deletions log/doc.go
@@ -1,5 +1,7 @@
// Package log provides a structured logger.
//
// Deprecated: Use github.com/go-kit/log instead.
//
// Structured logging produces logs easily consumed later by humans or
// machines. Humans might be interested in debugging errors, or tracing
// specific requests. Machines might be interested in counting interesting
Expand Down
82 changes: 3 additions & 79 deletions log/json_logger.go
@@ -1,91 +1,15 @@
package log

import (
"encoding"
"encoding/json"
"fmt"
"io"
"reflect"
)

type jsonLogger struct {
io.Writer
}
"github.com/go-kit/log"
)

// NewJSONLogger returns a Logger that encodes keyvals to the Writer as a
// single JSON object. Each log event produces no more than one call to
// w.Write. The passed Writer must be safe for concurrent use by multiple
// goroutines if the returned Logger will be used concurrently.
func NewJSONLogger(w io.Writer) Logger {
return &jsonLogger{w}
}

func (l *jsonLogger) Log(keyvals ...interface{}) error {
n := (len(keyvals) + 1) / 2 // +1 to handle case when len is odd
m := make(map[string]interface{}, n)
for i := 0; i < len(keyvals); i += 2 {
k := keyvals[i]
var v interface{} = ErrMissingValue
if i+1 < len(keyvals) {
v = keyvals[i+1]
}
merge(m, k, v)
}
enc := json.NewEncoder(l.Writer)
enc.SetEscapeHTML(false)
return enc.Encode(m)
}

func merge(dst map[string]interface{}, k, v interface{}) {
var key string
switch x := k.(type) {
case string:
key = x
case fmt.Stringer:
key = safeString(x)
default:
key = fmt.Sprint(x)
}

// We want json.Marshaler and encoding.TextMarshaller to take priority over
// err.Error() and v.String(). But json.Marshall (called later) does that by
// default so we force a no-op if it's one of those 2 case.
switch x := v.(type) {
case json.Marshaler:
case encoding.TextMarshaler:
case error:
v = safeError(x)
case fmt.Stringer:
v = safeString(x)
}

dst[key] = v
}

func safeString(str fmt.Stringer) (s string) {
defer func() {
if panicVal := recover(); panicVal != nil {
if v := reflect.ValueOf(str); v.Kind() == reflect.Ptr && v.IsNil() {
s = "NULL"
} else {
panic(panicVal)
}
}
}()
s = str.String()
return
}

func safeError(err error) (s interface{}) {
defer func() {
if panicVal := recover(); panicVal != nil {
if v := reflect.ValueOf(err); v.Kind() == reflect.Ptr && v.IsNil() {
s = nil
} else {
panic(panicVal)
}
}
}()
s = err.Error()
return
return log.NewJSONLogger(w)
}

0 comments on commit 801da84

Please sign in to comment.