https://github.com/acrazing/ctxzap
Bind zap to context, built-in support for grpc.
https://github.com/acrazing/ctxzap
context grpc logging zap
Last synced: 2 months ago
JSON representation
Bind zap to context, built-in support for grpc.
- Host: GitHub
- URL: https://github.com/acrazing/ctxzap
- Owner: acrazing
- License: mit
- Created: 2023-11-01T11:48:30.000Z (over 2 years ago)
- Default Branch: production
- Last Pushed: 2023-11-02T01:07:00.000Z (over 2 years ago)
- Last Synced: 2025-05-29T16:19:02.181Z (about 1 year ago)
- Topics: context, grpc, logging, zap
- Language: Go
- Homepage: https://pkg.go.dev/github.com/acrazing/ctxzap
- Size: 10.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ctxzap
Bind zap to context, built-in support for grpc.
## Install
```bash
go get github.com/acrazing/ctxzap
```
## Quick start
```go
package main
import (
"context"
"github.com/acrazing/ctxzap"
"github.com/acrazing/ctxzap/grpc_zap"
"go.uber.org/zap"
"google.golang.org/grpc"
)
func main() {
// get your logger
logger := zap.NewExample()
// inject interceptors
_ = grpc.NewServer(
grpc.ChainUnaryInterceptor(
grpc_zap.UnaryServerInterceptor(logger, grpc_zap.WithAccessLog(true, true)),
),
grpc.ChainStreamInterceptor(
grpc_zap.StreamServerInterceptor(logger, grpc_zap.WithEventLog(true, true)),
),
)
// print log
// In any context based method called by grpc servers
_ = func(ctx context.Context) {
// add a field to the context, this field will always be printed in the next
// log calls with the context.
ctxzap.AddFields(ctx, zap.String("data", "1"))
// replace an existing field or append it to the context
ctxzap.ReplaceField(ctx, zap.String("data", "2"))
// print a log
ctxzap.Info(ctx, "call with data", zap.String("value", "2"))
}
}
```
## License
MIT