zapctx

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Feb 18, 2022 License: MIT Imports: 11 Imported by: 0

README

Abstract

zapctx is an encapsulation of zap, adding WithContext to the logger.

Features

  • Logging with context, base on zap.
  • Integrated with lumberjack, which is a log rotation library.
  • Support for Gorm, base on zapgorm2

Installation

# Go Modules
require github.com/lehoqi/brickset
go get github.com/lehoqi/zapctx

Usage

  1. Use zapctx.New() to create a logger,with zapctx.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}

  1. 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
	}
}

  1. 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()
}
  1. 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"))
}

Documentation

Index

Constants

View Source
const (
	DEBUG  = "debug"
	INFO   = "info"
	WARN   = "warn"
	ERROR  = "error"
	FATAL  = "fatal"
	PANIC  = "panic"
	DPANIC = "dpanic"
)

Variables

This section is empty.

Functions

func ContextField

func ContextField(key string, ctx context.Context) zap.Field

func Gorm2

func Gorm2(l *Logger) logger.Interface

func Gorm2WithZap

func Gorm2WithZap(l *zap.Logger) logger.Interface

func Rotate

func Rotate(filePath string, cfg *RotateConfig) io.Writer

Types

type CtxDecoderFunc

type CtxDecoderFunc func(ctx context.Context, l *zap.Logger) *zap.Logger

type Logger

type Logger struct {
	*zap.Logger
	// contains filtered or unexported fields
}

func New

func New(opts ...Option) *Logger

func NewWithZap

func NewWithZap(logger *zap.Logger) *Logger

func (*Logger) Named

func (l *Logger) Named(name string) *Logger

func (*Logger) WithContext

func (l *Logger) WithContext(ctx context.Context) *Logger

func (*Logger) Zap

func (l *Logger) Zap() *zap.Logger

type Option

type Option func(cfg *config)

func WithCtxDecoderOption

func WithCtxDecoderOption(decoder CtxDecoderFunc) Option

func WithDebugOption

func WithDebugOption(debug bool) Option

func WithLevelOption

func WithLevelOption(l string) Option

func WithLogPathOption

func WithLogPathOption(path string) Option

func WithRotateOption

func WithRotateOption(maxAge, maxBackups int, compress bool) Option

func WithTraceIDOption

func WithTraceIDOption(traceID string) Option

type RotateConfig

type RotateConfig struct {
	MaxAge     int
	MaxBackups int
	Compress   bool
}

func (*RotateConfig) Init

func (r *RotateConfig) Init()

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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