zapctx is an encapsulation of zap, adding WithContext to the logger.
- Logging with context, base on zap.
- Integrated with lumberjack, which is a log rotation library.
- Support for Gorm, base on zapgorm2
# Go Modules
require github.com/lehoqi/brickset
go get github.com/lehoqi/zapctx
- Use
zapctx.New()
to create a logger,withzapctx.WithTraceIDOption
to create a logger with traceID.
package main
import (
"context"
"math/rand"
"github.com/lehoqi/zapctx"
)
func main() {
var traceID = "trace_id"
logger := zapctx.New(zapctx.WithTraceIDOption(traceID),zapctx.WithDebugOption(true),zapctx.WithLogPathOption("/tmp/main.log"))
defer logger.Sync()
logger = logger.Named("main")
ctx := context.WithValue(context.Background(), traceID, rand.Int())
logger.WithContext(ctx).Info("hello")
logger.WithContext(ctx).Debug("hello")
logger.WithContext(ctx).Error("hello")
logger.Named("sublog").Info("hello")
logger.Debug("hello")
}
➜ zapctx cat /tmp/main.log
2022-02-17 17:40:27.088 INFO main _examples/main.go:23 hello {"trace_id": 5577006791947779410}
2022-02-17 17:40:27.088 DEBUG main _examples/main.go:24 hello {"trace_id": 5577006791947779410}
2022-02-17 17:40:27.088 ERROR main _examples/main.go:25 hello {"trace_id": 5577006791947779410}
2022-02-17 17:40:27.088 INFO main.sublog _examples/main.go:26 hello
2022-02-17 17:40:27.088 DEBUG main _examples/main.go:27 hello
2、Use zapctx.ContextField
to create a zap field.
package main
import (
"context"
"math/rand"
"github.com/lehoqi/zapctx"
"go.uber.org/zap"
)
func main() {
var traceID = "trace_id"
ctx := context.WithValue(context.Background(), traceID, rand.Int())
zapLog, _ := zap.NewDevelopment()
defer zapLog.Sync()
zapLog = zapLog.Named("zaplog")
zapLog.Info("zaplog", zapctx.ContextField(traceID, ctx))
}
➜ zapctx
2022-02-17T17:41:58.173+0800 INFO zaplog _examples/main.go:23 zaplog {"trace_id": 5577006791947779410}
- Use
zapctx.WithCtxDecoderOption
to customize a context decoder.
package main
import (
"context"
"math/rand"
"github.com/lehoqi/zapctx"
"go.uber.org/zap"
)
var traceID = "trace_id"
func main() {
logger := zapctx.New(zapctx.WithCtxDecoderOption(myCtxDecoder(traceID)), zapctx.WithDebugOption(true))
defer logger.Sync()
logger = logger.Named("main")
ctx := context.WithValue(context.Background(), traceID, rand.Int())
logger.WithContext(ctx).Info("hello")
}
func myCtxDecoder(traceTag string) zapctx.CtxDecoderFunc {
return func(ctx context.Context, l *zap.Logger) *zap.Logger {
val := ctx.Value(traceTag)
if val != nil {
return l.With(zap.Any(traceTag, val))
}
return l
}
}
- Use
zapctx.WithRotateOption
to back up log file.
package main
import (
"github.com/lehoqi/zapctx"
)
func main() {
logger := zapctx.New(zapctx.WithLogPathOption("/tmp/main.log"), zapctx.WithRotateOption(7,3,true))
defer logger.Sync()
}
- Use
zapctx.Gorm2
to create a gorm(v2) logger.
package main
import (
"github.com/lehoqi/zapctx"
)
func main() {
logger := zapctx.New()
defer logger.Sync()
gormLogger := zapctx.Gorm2(logger.Named("gorm"))
}