ctxzap

package module
v1.0.1 Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2020 License: MIT Imports: 14 Imported by: 0

README

ctxzap

install

go get github.com/liguangsheng/go-ctxzap

example

simple

package main

func init() {
	ctxzap.BetterDefault() // this will replace global zap logger with a better default logger
}

func SomeFunction(ctx context.Context) {
    logger := ctxzap.L(ctx)
    logger.Info("some log") 
    ...
}

func main() {
	originContext := context.Background()
	newCtx := zapctx.ToContext(originContext, zap.L())
	SomeFunction(newCtx)
}

gin middleware

engine := gin.Default()
engine.Use(ctxzap.Gin(zap.L()))

grpc middleware

grpc.NewServer(
	grpc.UnaryInterceptor(grpc_middleware.ChainUnaryServer(
		zapctx.UnaryServerInterceptor(zap.L()),
	)))

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BetterDefaults

func BetterDefaults()

func Extract

func Extract(ctx context.Context) *_context

Extract

func Gin

func Gin(logger *zap.Logger, hooks ...HookFunc) gin.HandlerFunc

func L

func L(ctx context.Context) *zap.Logger

L return logger from context or zap.L()

func N

func N(ctx context.Context) *zap.Logger

N return logger from context or nopLogger

func OpenTraceFields

func OpenTraceFields(ctx context.Context) context.Context

func S

S return logger from context or zap.S()

func ToContext

func ToContext(ctx context.Context, logger *zap.Logger) context.Context

ToContext place *zap.Logger to context

func UnaryServerInterceptor

func UnaryServerInterceptor(logger *zap.Logger, hooks ...HookFunc) grpc.UnaryServerInterceptor

Types

type Context

type Context = *_context

type HookFunc

type HookFunc func(ctx context.Context) context.Context

Jump to

Keyboard shortcuts

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