zapctx

package
v0.0.0-...-499462a Latest Latest
Warning

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

Go to latest
Published: Dec 5, 2024 License: MIT Imports: 8 Imported by: 0

README

zapctx

zap logger from context.

example

simple

package main

func init() {
	zapctx.BetterDefault()
}

func SomeFunction(ctx context.Context) {
    logger := zapctx.L(ctx)
    
	logger.Info("some log") 
	// {"level":"info","time":"2019-10-11T10:28:18.492+0800","caller":"_playground/main.go:64","msg":"some log","hello":"world"}
	
	logger.Info("some log") 
	// {"level":"info","time":"2019-10-11T10:28:18.492+0800","caller":"_playground/main.go:64","msg":"some log","hello":"world"}
	
	sampledLogger := zapctx.Sampled(ctx)
	sampledLogger.Info("some log") 
	// nothing, because Sampled is false
}

func main() {
	originContext := context.Background()
	newCtx := zapctx.ToContext(originContext, zap.L())
	_ctx := zapctx.Extract(newCtx)
	_ctx.AddFields(zap.String("hello", "world"))
	_ctx.Sampled = false

	SomeFunction(newCtx)
}

gin middleware

engine := gin.Default()
engine.Use(zapctx.GinMiddleware(zap.L(), zapctx.OpenTraceInject))

grpc middleware

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

TODO

  • payload middleware

Jump to

Keyboard shortcuts

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