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

ctxloggers for zap and logrus should live with logging #128

Merged
merged 3 commits into from
Feb 28, 2018
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: 5 additions & 4 deletions logging/logrus/DOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ Please see examples and tests for examples of use.
- [github.com/golang/protobuf/proto](https://godoc.org/github.com/golang/protobuf/proto)
- [github.com/grpc-ecosystem/go-grpc-middleware](./../..)
- [github.com/grpc-ecosystem/go-grpc-middleware/logging](./..)
- [github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus](./ctxlogrus)
- [github.com/grpc-ecosystem/go-grpc-middleware/tags/logrus](./../../tags/logrus)
- [github.com/sirupsen/logrus](https://godoc.org/github.com/sirupsen/logrus)
- [golang.org/x/net/context](https://godoc.org/golang.org/x/net/context)
Expand Down Expand Up @@ -146,7 +147,7 @@ var (
func AddFields(ctx context.Context, fields logrus.Fields)
```
AddFields adds logrus fields to the logger.
Deprecated: should use the ctx_logrus.Extract instead
Deprecated: should use the ctxlogrus.Extract instead

## <a name="DefaultClientCodeToLevel">func</a> [DefaultClientCodeToLevel](./options.go#L129)
``` go
Expand Down Expand Up @@ -177,7 +178,7 @@ DurationToTimeMillisField converts the duration to milliseconds and uses the key
func Extract(ctx context.Context) *logrus.Entry
```
Extract takes the call-scoped logrus.Entry from grpc_logrus middleware.
Deprecated: should use the ctx_logrus.Extract instead
Deprecated: should use the ctxlogrus.Extract instead

## <a name="PayloadStreamClientInterceptor">func</a> [PayloadStreamClientInterceptor](./payload_interceptors.go#L74)
``` go
Expand Down Expand Up @@ -222,7 +223,7 @@ func StreamClientInterceptor(entry *logrus.Entry, opts ...Option) grpc.StreamCli
```
StreamServerInterceptor returns a new streaming client interceptor that optionally logs the execution of external gRPC calls.

## <a name="StreamServerInterceptor">func</a> [StreamServerInterceptor](./server_interceptors.go#L57)
## <a name="StreamServerInterceptor">func</a> [StreamServerInterceptor](./server_interceptors.go#L58)
``` go
func StreamServerInterceptor(entry *logrus.Entry, opts ...Option) grpc.StreamServerInterceptor
```
Expand All @@ -234,7 +235,7 @@ func UnaryClientInterceptor(entry *logrus.Entry, opts ...Option) grpc.UnaryClien
```
UnaryClientInterceptor returns a new unary client interceptor that optionally logs the execution of external gRPC calls.

## <a name="UnaryServerInterceptor">func</a> [UnaryServerInterceptor](./server_interceptors.go#L25)
## <a name="UnaryServerInterceptor">func</a> [UnaryServerInterceptor](./server_interceptors.go#L26)
``` go
func UnaryServerInterceptor(entry *logrus.Entry, opts ...Option) grpc.UnaryServerInterceptor
```
Expand Down
10 changes: 5 additions & 5 deletions logging/logrus/context.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
package grpc_logrus

import (
"github.com/grpc-ecosystem/go-grpc-middleware/tags/logrus"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus"
"github.com/sirupsen/logrus"
"golang.org/x/net/context"
)

// AddFields adds logrus fields to the logger.
// Deprecated: should use the ctx_logrus.Extract instead
// Deprecated: should use the ctxlogrus.Extract instead
func AddFields(ctx context.Context, fields logrus.Fields) {
ctx_logrus.AddFields(ctx, fields)
ctxlogrus.AddFields(ctx, fields)
}

// Extract takes the call-scoped logrus.Entry from grpc_logrus middleware.
// Deprecated: should use the ctx_logrus.Extract instead
// Deprecated: should use the ctxlogrus.Extract instead
func Extract(ctx context.Context) *logrus.Entry {
return ctx_logrus.Extract(ctx)
return ctxlogrus.Extract(ctx)
}
58 changes: 58 additions & 0 deletions logging/logrus/ctxlogrus/DOC.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
# ctxlogrus
`import "github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus"`

* [Overview](#pkg-overview)
* [Imported Packages](#pkg-imports)
* [Index](#pkg-index)

## <a name="pkg-overview">Overview</a>
`ctxlogrus` is a ctxlogger that is backed by logrus

It accepts a user-configured `logrus.Logger` that will be used for logging. The same `logrus.Logger` will
be populated into the `context.Context` passed into gRPC handler code.

You can use `ctx_logrus.Extract` to log into a request-scoped `logrus.Logger` instance in your handler code.

As `ctx_logrus.Extract` will iterate all tags on from `grpc_ctxtags` it is therefore expensive so it is advised that you
extract once at the start of the function from the context and reuse it for the remainder of the function (see examples).

Please see examples and tests for examples of use.

## <a name="pkg-imports">Imported Packages</a>

- [github.com/grpc-ecosystem/go-grpc-middleware/tags](./../../../tags)
- [github.com/sirupsen/logrus](https://godoc.org/github.com/sirupsen/logrus)
- [golang.org/x/net/context](https://godoc.org/golang.org/x/net/context)

## <a name="pkg-index">Index</a>
* [func AddFields(ctx context.Context, fields logrus.Fields)](#AddFields)
* [func Extract(ctx context.Context) \*logrus.Entry](#Extract)
* [func ToContext(ctx context.Context, entry \*logrus.Entry) context.Context](#ToContext)

#### <a name="pkg-files">Package files</a>
[context.go](./context.go) [doc.go](./doc.go) [noop.go](./noop.go)

## <a name="AddFields">func</a> [AddFields](./context.go#L21)
``` go
func AddFields(ctx context.Context, fields logrus.Fields)
```
AddFields adds logrus fields to the logger.

## <a name="Extract">func</a> [Extract](./context.go#L35)
``` go
func Extract(ctx context.Context) *logrus.Entry
```
Extract takes the call-scoped logrus.Entry from ctx_logrus middleware.

If the ctx_logrus middleware wasn't used, a no-op `logrus.Entry` is returned. This makes it safe to
use regardless.

## <a name="ToContext">func</a> [ToContext](./context.go#L59)
``` go
func ToContext(ctx context.Context, entry *logrus.Entry) context.Context
```
ToContext adds the logrus.Entry to the context for extraction later.
Returning the new context that has been created.

- - -
Generated by [godoc2ghmd](https://github.com/GandalfUK/godoc2ghmd)
File renamed without changes.
65 changes: 65 additions & 0 deletions logging/logrus/ctxlogrus/context.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package ctxlogrus

import (
"github.com/grpc-ecosystem/go-grpc-middleware/tags"
"github.com/sirupsen/logrus"
"golang.org/x/net/context"
)

type ctxLoggerMarker struct{}

type ctxLogger struct {
logger *logrus.Entry
fields logrus.Fields
}

var (
ctxLoggerKey = &ctxLoggerMarker{}
)

// AddFields adds logrus fields to the logger.
func AddFields(ctx context.Context, fields logrus.Fields) {
l, ok := ctx.Value(ctxLoggerKey).(*ctxLogger)
if !ok || l == nil {
return
}
for k, v := range fields {
l.fields[k] = v
}
}

// Extract takes the call-scoped logrus.Entry from ctx_logrus middleware.
//
// If the ctx_logrus middleware wasn't used, a no-op `logrus.Entry` is returned. This makes it safe to
// use regardless.
func Extract(ctx context.Context) *logrus.Entry {
l, ok := ctx.Value(ctxLoggerKey).(*ctxLogger)
if !ok || l == nil {
return logrus.NewEntry(nullLogger)
}

fields := logrus.Fields{}

// Add grpc_ctxtags tags metadata until now.
tags := grpc_ctxtags.Extract(ctx)
for k, v := range tags.Values() {
fields[k] = v
}

// Add logrus fields added until now.
for k, v := range l.fields {
fields[k] = v
}

return l.logger.WithFields(fields)
}

// ToContext adds the logrus.Entry to the context for extraction later.
// Returning the new context that has been created.
func ToContext(ctx context.Context, entry *logrus.Entry) context.Context {
l := &ctxLogger{
logger: entry,
fields: logrus.Fields{},
}
return context.WithValue(ctx, ctxLoggerKey, l)
}
4 changes: 2 additions & 2 deletions tags/logrus/doc.go → logging/logrus/ctxlogrus/doc.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
`ctx_logrus` is a ctxlogger that is backed by logrus
`ctxlogrus` is a ctxlogger that is backed by logrus

It accepts a user-configured `logrus.Logger` that will be used for logging. The same `logrus.Logger` will
be populated into the `context.Context` passed into gRPC handler code.
Expand All @@ -11,4 +11,4 @@ extract once at the start of the function from the context and reuse it for the

Please see examples and tests for examples of use.
*/
package ctx_logrus
package ctxlogrus
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package ctx_logrus_test
package ctxlogrus_test

import (
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus"
"github.com/grpc-ecosystem/go-grpc-middleware/tags"
"github.com/grpc-ecosystem/go-grpc-middleware/tags/logrus"
pb_testproto "github.com/grpc-ecosystem/go-grpc-middleware/testing/testproto"
"github.com/sirupsen/logrus"
"golang.org/x/net/context"
Expand All @@ -16,7 +16,7 @@ func Example_HandlerUsageUnaryPing() {
// Add fields the ctxtags of the request which will be added to all extracted loggers.
grpc_ctxtags.Extract(ctx).Set("custom_tags.string", "something").Set("custom_tags.int", 1337)
// Extract a single request-scoped logrus.Logger and log messages.
l := ctx_logrus.Extract(ctx)
l := ctxlogrus.Extract(ctx)
l.Info("some ping")
l.Info("another ping")
return &pb_testproto.PingResponse{Value: ping.Value}, nil
Expand Down
2 changes: 1 addition & 1 deletion tags/logrus/noop.go → logging/logrus/ctxlogrus/noop.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package ctx_logrus
package ctxlogrus

import (
"io/ioutil"
Expand Down
3 changes: 2 additions & 1 deletion logging/logrus/server_interceptors.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"time"

"github.com/grpc-ecosystem/go-grpc-middleware"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/logrus/ctxlogrus"
"github.com/grpc-ecosystem/go-grpc-middleware/tags/logrus"
"github.com/sirupsen/logrus"
"golang.org/x/net/context"
Expand Down Expand Up @@ -124,5 +125,5 @@ func newLoggerForCall(ctx context.Context, entry *logrus.Entry, fullMethodString
}

callLog = callLog.WithFields(ctx_logrus.Extract(ctx).Data)
return ctx_logrus.ToContext(ctx, callLog)
return ctxlogrus.ToContext(ctx, callLog)
}
9 changes: 5 additions & 4 deletions logging/zap/DOC.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ Please see examples and tests for examples of use.
- [github.com/golang/protobuf/proto](https://godoc.org/github.com/golang/protobuf/proto)
- [github.com/grpc-ecosystem/go-grpc-middleware](./../..)
- [github.com/grpc-ecosystem/go-grpc-middleware/logging](./..)
- [github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap](./ctxzap)
- [github.com/grpc-ecosystem/go-grpc-middleware/tags/zap](./../../tags/zap)
- [go.uber.org/zap](https://godoc.org/go.uber.org/zap)
- [go.uber.org/zap/zapcore](https://godoc.org/go.uber.org/zap/zapcore)
Expand Down Expand Up @@ -155,7 +156,7 @@ var (
func AddFields(ctx context.Context, fields ...zapcore.Field)
```
AddFields adds zap fields to the logger.
Deprecated: should use the ctx_zap.AddFields instead
Deprecated: should use the ctxzap.AddFields instead

## <a name="DefaultClientCodeToLevel">func</a> [DefaultClientCodeToLevel](./options.go#L127)
``` go
Expand Down Expand Up @@ -187,7 +188,7 @@ DurationToTimeMillisField converts the duration to milliseconds and uses the key
func Extract(ctx context.Context) *zap.Logger
```
Extract takes the call-scoped Logger from grpc_zap middleware.
Deprecated: should use the ctx_zap.Extract instead
Deprecated: should use the ctxzap.Extract instead

## <a name="PayloadStreamClientInterceptor">func</a> [PayloadStreamClientInterceptor](./payload_interceptors.go#L74)
``` go
Expand Down Expand Up @@ -232,7 +233,7 @@ func StreamClientInterceptor(logger *zap.Logger, opts ...Option) grpc.StreamClie
```
StreamServerInterceptor returns a new streaming client interceptor that optionally logs the execution of external gRPC calls.

## <a name="StreamServerInterceptor">func</a> [StreamServerInterceptor](./server_interceptors.go#L50)
## <a name="StreamServerInterceptor">func</a> [StreamServerInterceptor](./server_interceptors.go#L51)
``` go
func StreamServerInterceptor(logger *zap.Logger, opts ...Option) grpc.StreamServerInterceptor
```
Expand All @@ -244,7 +245,7 @@ func UnaryClientInterceptor(logger *zap.Logger, opts ...Option) grpc.UnaryClient
```
UnaryClientInterceptor returns a new unary client interceptor that optionally logs the execution of external gRPC calls.

## <a name="UnaryServerInterceptor">func</a> [UnaryServerInterceptor](./server_interceptors.go#L24)
## <a name="UnaryServerInterceptor">func</a> [UnaryServerInterceptor](./server_interceptors.go#L25)
``` go
func UnaryServerInterceptor(logger *zap.Logger, opts ...Option) grpc.UnaryServerInterceptor
```
Expand Down
10 changes: 5 additions & 5 deletions logging/zap/context.go
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package grpc_zap

import (
"github.com/grpc-ecosystem/go-grpc-middleware/tags/zap"
"github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"golang.org/x/net/context"
)

// AddFields adds zap fields to the logger.
// Deprecated: should use the ctx_zap.AddFields instead
// Deprecated: should use the ctxzap.AddFields instead
func AddFields(ctx context.Context, fields ...zapcore.Field) {
ctx_zap.AddFields(ctx, fields...)
ctxzap.AddFields(ctx, fields...)
}

// Extract takes the call-scoped Logger from grpc_zap middleware.
// Deprecated: should use the ctx_zap.Extract instead
// Deprecated: should use the ctxzap.Extract instead
func Extract(ctx context.Context) *zap.Logger {
return ctx_zap.Extract(ctx)
return ctxzap.Extract(ctx)
}
65 changes: 65 additions & 0 deletions logging/zap/ctxzap/DOC.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# ctxzap
`import "github.com/grpc-ecosystem/go-grpc-middleware/logging/zap/ctxzap"`

* [Overview](#pkg-overview)
* [Imported Packages](#pkg-imports)
* [Index](#pkg-index)

## <a name="pkg-overview">Overview</a>
`ctxzap` is a ctxlogger that is backed by Zap

It accepts a user-configured `zap.Logger` that will be used for logging. The same `zap.Logger` will
be populated into the `context.Context` passed into gRPC handler code.

You can use `ctxzap.Extract` to log into a request-scoped `zap.Logger` instance in your handler code.

As `ctxzap.Extract` will iterate all tags on from `grpc_ctxtags` it is therefore expensive so it is advised that you
extract once at the start of the function from the context and reuse it for the remainder of the function (see examples).

Please see examples and tests for examples of use.

## <a name="pkg-imports">Imported Packages</a>

- [github.com/grpc-ecosystem/go-grpc-middleware/tags](./../../../tags)
- [go.uber.org/zap](https://godoc.org/go.uber.org/zap)
- [go.uber.org/zap/zapcore](https://godoc.org/go.uber.org/zap/zapcore)
- [golang.org/x/net/context](https://godoc.org/golang.org/x/net/context)

## <a name="pkg-index">Index</a>
* [func AddFields(ctx context.Context, fields ...zapcore.Field)](#AddFields)
* [func Extract(ctx context.Context) \*zap.Logger](#Extract)
* [func TagsToFields(ctx context.Context) []zapcore.Field](#TagsToFields)
* [func ToContext(ctx context.Context, logger \*zap.Logger) context.Context](#ToContext)

#### <a name="pkg-files">Package files</a>
[context.go](./context.go) [doc.go](./doc.go)

## <a name="AddFields">func</a> [AddFields](./context.go#L23)
``` go
func AddFields(ctx context.Context, fields ...zapcore.Field)
```
AddFields adds zap fields to the logger.

## <a name="Extract">func</a> [Extract](./context.go#L35)
``` go
func Extract(ctx context.Context) *zap.Logger
```
Extract takes the call-scoped Logger from grpc_zap middleware.

It always returns a Logger that has all the grpc_ctxtags updated.

## <a name="TagsToFields">func</a> [TagsToFields](./context.go#L48)
``` go
func TagsToFields(ctx context.Context) []zapcore.Field
```
TagsToFields transforms the Tags on the supplied context into zap fields.

## <a name="ToContext">func</a> [ToContext](./context.go#L59)
``` go
func ToContext(ctx context.Context, logger *zap.Logger) context.Context
```
ToContext adds the zap.Logger to the context for extraction later.
Returning the new context that has been created.

- - -
Generated by [godoc2ghmd](https://github.com/GandalfUK/godoc2ghmd)
File renamed without changes.
Loading