qezap

package module
v1.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 12, 2022 License: MIT Imports: 25 Imported by: 2

README

qezap

qelog service, wrap Uber-zap as this service Go client.

Wrap Uber-zap
  • local fs: support rotate written, gzip compress, delete expired log file
  • remote storage: support GRPC and HTTP protocol, data buffer merge transport, exception retry. extension field use to admin filtering.
Usage

go get -u github.com/bbdshow/qelog/qezap

Example
package main

import (
	"context"
	"errors"
	"time"

	"github.com/bbdshow/qelog/qezap"
	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
)

func main() {
	// 500MB rotate,keep 7days
	local := qezap.New(
		qezap.WithFilename("./log/local.log"),
		qezap.WithRotateMaxSizeAge(500<<20, 7*24*time.Hour))
	defer local.Close()

	local.Debug("Debug", zap.String("val", "only written local file"))

	// runtime change logger level
	local.SetEnabledLevel(zapcore.InfoLevel)
	local.Debug("Debug", zap.String("val", "this msg,not written to file"))

	// support remote storage
	multi := qezap.New(qezap.WithAddrsAndModuleName([]string{"127.0.0.1:31082"}, "demo"))
	defer multi.Close()

	multi.Info("local fs and remote storage will be written")

	// extension field
	// context bind traceID
	ctx := multi.WithTraceID(context.Background())
	// multi.FieldTraceID(ctx): get traceId from context, generate zap.Field written this log
	// admin manager can use traceId, find 'trace warn' and 'trace error' log
	multi.Warn("trace warn", zap.String("xx", "xx"), multi.FieldTraceID(ctx))
	multi.Error("trace error", zap.String("xx", "xx"), multi.FieldTraceID(ctx))

	// if we need manger filtering log, we provide multi condition field bind to log
	multi.Info("this msg as init filtering")
	multi.Info("this msg as init filtering", multi.ConditionOne("first condition"),
		multi.ConditionTwo("second condition"), multi.ConditionThree("third condition"))

	// if we need to io.Writer
	// gin.RecoveryWithWriter(ioWrite)
	ioWrite := multi.NewLevelWriter(zapcore.InfoLevel, "GIN logger output")
	ioWrite.Write([]byte("logger impl io.Writer, eg: gin.RecoveryWithWriter(ioWrite)"))

	// how to replace GO Logger
	// use zap.Sugar()
	slg := multi.Sugar()
	slg.Info("This info log")
	slg.Errorf("have err: %s", errors.New("mock error").Error())
}

Documentation

Index

Constants

View Source
const (
	LocalServiceName = "grpc_local_resolver_name"
	LocalScheme      = "local"
)
View Source
const (
	TransportGRPC Transport = "GRPC"
	TransportHTTP Transport = "HTTP"
	TransportMock Transport = "MOCK"

	ModeRelease Mode = "RELEASE"
	ModeDebug   Mode = "DEBUG"
)

Variables

View Source
var (
	DialLocalServiceName = fmt.Sprintf("%s:///%s", LocalScheme, LocalServiceName)
)
View Source
var (
	ErrUnavailable = errors.New("Push Unavailable")
)

Functions

func ConditionOne

func ConditionOne(v string) zap.Field

ConditionOne wrap internal field

func ConditionThree

func ConditionThree(v string) zap.Field

ConditionThree wrap internal field

func ConditionTwo

func ConditionTwo(v string) zap.Field

ConditionTwo wrap internal field

func FieldTraceID

func FieldTraceID(ctx context.Context) zap.Field

FieldTraceID wrap internal field

func TraceID

func TraceID(ctx context.Context) types.TraceID

TraceID from context

func WithTraceID

func WithTraceID(ctx context.Context) context.Context

WithTraceID traceId setting in context

Types

type DataPacket

type DataPacket struct {
	// contains filtered or unexported fields
}

func (*DataPacket) CanPush

func (d *DataPacket) CanPush() bool

func (*DataPacket) Data

func (d *DataPacket) Data() *receiverpb.Packet

func (*DataPacket) IsEmpty

func (d *DataPacket) IsEmpty() bool

type LocalOption

type LocalOption struct {
	// local fs filename
	Filename string
	// single file max size, if 0 do not cut file. default: 500MB
	MaxSize int64
	// cut file max keep time. default: 0 keep forever
	MaxAge time.Duration
	// cut file enable Gzip compress. default: true
	GzipCompress bool
}

func DefaultLocalOption

func DefaultLocalOption() *LocalOption

type LocalResolverBuilder

type LocalResolverBuilder struct {
	// contains filtered or unexported fields
}

LocalResolverBuilder impl local address resolver, used for GRPC load balancing

func NewLocalResolverBuilder

func NewLocalResolverBuilder(address []string) *LocalResolverBuilder

func (*LocalResolverBuilder) Build

func (*LocalResolverBuilder) Scheme

func (*LocalResolverBuilder) Scheme() string

type Logger

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

func New

func New(opts ...Option) *Logger

New wrap zap.Logger, impl multi writer local writer written to fs. remote writer, rely on GRPC or HTTP protocol written remote storage you can also only select local fs.

func (*Logger) Close

func (lg *Logger) Close() error

Close will be sync logger data and close file handle.

func (*Logger) ConditionOne

func (lg *Logger) ConditionOne(v interface{}) zap.Field

ConditionOne internal field extension, used for first condition filtering

func (*Logger) ConditionThree

func (lg *Logger) ConditionThree(v interface{}) zap.Field

ConditionThree internal field extension, used for third condition filtering

func (*Logger) ConditionTwo

func (lg *Logger) ConditionTwo(v interface{}) zap.Field

ConditionTwo internal field extension, used for second condition filtering

func (*Logger) FieldTraceID

func (lg *Logger) FieldTraceID(ctx context.Context) zap.Field

FieldTraceID internal field extension, used for trace context filtering

func (*Logger) NewLevelWriter

func (lg *Logger) NewLevelWriter(level zapcore.Level, msg string) *Writer

NewLevelWriter returns io.Writer impl fixed msg for remote storage index performance

func (*Logger) SetEnabledLevel

func (lg *Logger) SetEnabledLevel(lvl zapcore.Level) *Logger

SetEnabledLevel runtime change logger level

func (*Logger) TraceID

func (lg *Logger) TraceID(ctx context.Context) types.TraceID

TraceID get traceId from context

func (*Logger) WithTraceID

func (lg *Logger) WithTraceID(ctx context.Context) context.Context

WithTraceID generate traceID(bson._id), set value in context,relation context

type Mode

type Mode string

type Option

type Option interface {
	// contains filtered or unexported methods
}

Option setting options

func WithAddrsAndModuleName

func WithAddrsAndModuleName(addrs []string, moduleName string) Option

WithAddrsAndModuleName remote endpoint address, module name address eg HTTP:["http://xxx.com:31081/v1/receiver/packet"] GRPC:["192.168.10.1:31082","192.168.10.2:31082"] admin manager register module name, used to permission verify. eg "example"

func WithFilename

func WithFilename(filename string) Option

WithFilename setting logger filename

func WithGzipCompress

func WithGzipCompress(enable bool) Option

WithGzipCompress setting logger rotate gzip compress

func WithLevel

func WithLevel(lvl zapcore.Level) Option

WithLevel setting logger level

func WithMode

func WithMode(mode Mode) Option

WithMode setting logger mode

func WithRemoteConcurrent

func WithRemoteConcurrent(n uint) Option

WithRemoteConcurrent setting logger remote max concurrent

func WithRemotePacketSize

func WithRemotePacketSize(n uint) Option

WithRemotePacketSize setting logger remote max packet size, unit KB, limit 4MB

func WithRemoteWriteTimeout

func WithRemoteWriteTimeout(timeout time.Duration) Option

WithRemoteWriteTimeout setting logger remote timeout

func WithRotateMaxSizeAge

func WithRotateMaxSizeAge(size uint64, age time.Duration) Option

WithRotateMaxSizeAge setting logger local fs rotate

func WithTransport

func WithTransport(trans Transport) Option

WithTransport setting remote transport, MOCK used for test Pusher

func WithZapOptions

func WithZapOptions(opts ...zap.Option) Option

WithZapOptions setting zap option

type Packet

type Packet struct {
	// contains filtered or unexported fields
}

func (*Packet) Append

func (p *Packet) Append(b []byte) *DataPacket

Append warning: concurrent not safe

func (*Packet) DataPacket

func (p *Packet) DataPacket() *DataPacket

func (*Packet) PoolPutDataPacket

func (p *Packet) PoolPutDataPacket(d *DataPacket)

func (*Packet) SetCanPush

func (p *Packet) SetCanPush(d *DataPacket)

func (*Packet) SwitchNextDataPacket

func (p *Packet) SwitchNextDataPacket()

SwitchNextDataPacket current data have been processed, so set nil, switch next data packet

type Pusher

type Pusher interface {
	PushPacket(ctx context.Context, in *receiverpb.Packet) error
	Concurrent() int
	Close() error
}

type Transport

type Transport string

type WriteLocal

type WriteLocal struct {
	// contains filtered or unexported fields
}

WriteLocal local fs writer impl support rotate, gzip, maxAge

func NewWriteLocal

func NewWriteLocal(opt *LocalOption) *WriteLocal

func (*WriteLocal) Close

func (w *WriteLocal) Close() error

Close file handle

func (*WriteLocal) Sync

func (w *WriteLocal) Sync() error

Sync empty impl

func (*WriteLocal) Write

func (w *WriteLocal) Write(b []byte) (n int, err error)

Write impl

type WriteRemote

type WriteRemote struct {
	// contains filtered or unexported fields
}

func (*WriteRemote) Close

func (w *WriteRemote) Close() error

func (*WriteRemote) Sync

func (w *WriteRemote) Sync() error

Sync write final content

func (*WriteRemote) Write

func (w *WriteRemote) Write(b []byte) (n int, err error)

type Writer

type Writer struct {
	// contains filtered or unexported fields
}

Writer Rely on zap.Logger to achieve IO write,use in io.Writer scenarios data will be written local | remote log file

func (*Writer) Write

func (w *Writer) Write(b []byte) (n int, err error)

Write rely on zap core write, actual data is written DATA field

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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