zapx

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2022 License: MIT Imports: 11 Imported by: 8

README

zapx

this is a wrapper of zap. The biggest difference of them is that the wrapper add WithContext method for logger.

How to Use

there are 3 ways to use zapx:

  • packaged-embedded way

nothing to init! just using the method zapx.Info(), zapx.Warnf() and so on.

func main() {
	zapx.Info("Hello")
	zapx.Error("this is an error", zap.Error(errors.New("error!")))
	zapx.Warnf("%v", "warn")
}
  • global logger instance
func main() {
	logger, err := zapx.NewLogger(&zapx.Option{})
	if err != nil {
		panic(err)
	}

	logger.Info("hello")
	logger.Error("this is an error", zap.Error(errors.New("error!")))
	logger.Warnf("%v", "warn")
}
  • overwrite packaged-embedded variable
func main() {
	err = zapx.Use(&zapx.Option{})
	if err != nil {
		panic(err)
	}

	zapx.Info("Hello")
	zapx.Error("this is an error", zap.Error(errors.New("error!")))
	zapx.Warnf("%v", "warn")
}

Feature

context decoder

the CtxDecoder is just an interface:

type CtxDecoder interface {
	DecodeCtx(context.Context) []zap.Field
}

it 'decode' the context and get values from it. Then it converts them into []zap.Field to log.

It is designed to integrated with Tracing: get tracing related info from context, like traceID.

Now, zapx implements some context decoder:

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DPanic

func DPanic(msg string, fields ...zap.Field)

func DPanicf added in v0.2.0

func DPanicf(template string, args ...interface{})

func Debug

func Debug(msg string, fields ...zap.Field)

func Debugf added in v0.2.0

func Debugf(template string, args ...interface{})

func Error

func Error(msg string, fields ...zap.Field)

func Errorf added in v0.2.0

func Errorf(template string, args ...interface{})

func Fatal

func Fatal(msg string, fields ...zap.Field)

func Fatalf added in v0.2.0

func Fatalf(template string, args ...interface{})

func Info

func Info(msg string, fields ...zap.Field)

func Infof added in v0.2.0

func Infof(template string, args ...interface{})

func Panic

func Panic(msg string, fields ...zap.Field)

func Panicf added in v0.2.0

func Panicf(template string, args ...interface{})

func Use

func Use(opt *Option) error

func UseCtxDecoder added in v0.2.1

func UseCtxDecoder(decoder decoder.CtxDecoder)

func Warn

func Warn(msg string, fields ...zap.Field)

func Warnf added in v0.2.0

func Warnf(template string, args ...interface{})

Types

type Logger added in v0.2.0

type Logger struct {
	decoder.CtxDecoder
	// contains filtered or unexported fields
}

func NewLogger added in v0.2.0

func NewLogger(opt *Option) (logger *Logger, err error)

func With

func With(fields ...zap.Field) *Logger

func WithContext

func WithContext(ctx context.Context) *Logger

func (*Logger) DPanic added in v0.2.0

func (log *Logger) DPanic(msg string, fields ...zap.Field)

func (*Logger) DPanicf added in v0.2.0

func (log *Logger) DPanicf(template string, args ...interface{})

func (*Logger) Debug added in v0.2.0

func (log *Logger) Debug(msg string, fields ...zap.Field)

func (*Logger) Debugf added in v0.2.0

func (log *Logger) Debugf(template string, args ...interface{})

func (*Logger) Error added in v0.2.0

func (log *Logger) Error(msg string, fields ...zap.Field)

func (*Logger) Errorf added in v0.2.0

func (log *Logger) Errorf(template string, args ...interface{})

func (*Logger) Fatal added in v0.2.0

func (log *Logger) Fatal(msg string, fields ...zap.Field)

func (*Logger) Fatalf added in v0.2.0

func (log *Logger) Fatalf(template string, args ...interface{})

func (*Logger) Info added in v0.2.0

func (log *Logger) Info(msg string, fields ...zap.Field)

func (*Logger) Infof added in v0.2.0

func (log *Logger) Infof(template string, args ...interface{})

func (*Logger) Panic added in v0.2.0

func (log *Logger) Panic(msg string, fields ...zap.Field)

func (*Logger) Panicf added in v0.2.0

func (log *Logger) Panicf(template string, args ...interface{})

func (*Logger) Warn added in v0.2.0

func (log *Logger) Warn(msg string, fields ...zap.Field)

func (*Logger) Warnf added in v0.2.0

func (log *Logger) Warnf(template string, args ...interface{})

func (*Logger) With added in v0.2.0

func (log *Logger) With(fields ...zap.Field) *Logger

With creates a child logger and adds structured context to it. Fields added to the child don't affect the parent, and vice versa.

func (*Logger) WithContext added in v0.2.0

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

type Option added in v0.2.0

type Option struct {
	LogSyncer []zapcore.WriteSyncer

	// lumberjack log rolling config.
	// to see more information about it, see its github(https://github.com/natefinch/lumberjack)
	RollingOption *lumberjack.Logger
	LogLevel      string
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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