zap

package
v0.0.0-...-e4b7068 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 24, 2019 License: MIT Imports: 6 Imported by: 0

README

zap

Under development.

Usage

import (
	"github.com/uber-go/zap"
	grpczap "github.com/mercari/go-grpc-interceptor/zap"
	"golang.org/x/net/context"
)

func main() {
	logger = zap.New(zap.NewJSONEncoder())
	uIntOpt := grpc.UnaryInterceptor(grpczap.UnaryServerInterceptor(logger))
	sIntOpt := grpc.StreamInterceptor(grpczap.StreamServerInterceptor(logger))
	grpc.NewServer(uIntOpt, sIntOpt)
}
Structured logging with context
import (
	"github.com/uber-go/zap"
	"github.com/mercari/go-grpc-interceptor/zap/zapctx"
	"golang.org/x/net/context"
)

func foo(ctx context.Context) {
	// create new a context with some new zap.Field
	newctx := zapctx.MustNewContextWith(ctx,
		zap.String("user_id", 123456"),
	)

	// call other function with the context
	bar(newctx)
}

func bar(ctx context.Context) {
	// logging with additional context
	logger := zapctx.MustFromContext(ctx)
	logger.Info("message",
		zap.String("function", "bar"),
	)
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL