hlog

package
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Sep 22, 2022 License: Apache-2.0 Imports: 8 Imported by: 0

README

封装 zap 日志注入 trace 信息 Trace Id(内含 gin 例子)

hlog (源码地址)
  • 实现自动切割文件 (基于 lumberjack 实现)
  • 实现可传递 trace 信息 (基于 Context 实现)
配置
  • Development bool // 是否开发模式
  • LogFileDir string // 日志路径
  • AppName string // APP名字
  • MaxSize int //文件多大开始切分
  • MaxBackups int //保留文件个数
  • MaxAge int //文件保留最大实际
  • Level string // 日志打印等级
  • CtxKey string //通过 ctx 传递 hlog 信息
  • WriteFile bool // 是否写入文件
  • WriteConsole bool // 是否控制台打印
实现自动切割文件核心代码
 zapcore.AddSync(&lumberjack.Logger{
		Filename:   l.opts.LogFileDir + "/" + l.opts.AppName + ".log",
		MaxSize:    l.opts.MaxSize,
		MaxBackups: l.opts.MaxBackups,
		MaxAge:     l.opts.MaxAge,
		Compress:   true,
		LocalTime:  true,
	})
实现可传递 trace 信息核心代码
func (l *Logger) GetCtx(ctx context.Context) *zap.Logger {
	log, ok := ctx.Value(l.opts.CtxKey).(*zap.Logger)
	if ok {
		return log
	}
	return l.Logger
}

func (l *Logger) AddCtx(ctx context.Context, field ...zap.Field) (context.Context, *zap.Logger) {
	log := l.With(field...)
	ctx = context.WithValue(ctx, l.opts.CtxKey, log)
	return ctx, log
}
例子(普通)
hlog.NewLogger()
hlog.GetLogger().Info("hconf example success")

{"L":"INFO","T":"2021-12-14T11:43:13.276+0800","C":"hlog/zap.go:34","M":"[initLogger] zap plugin initializing completed"}
{"L":"INFO","T":"2021-12-14T11:43:13.277+0800","C":"hlog/zap_test.go:12","M":"hconf example success"}
例子(gin)
func AddTraceId() gin.HandlerFunc {
	return func(g *gin.Context) {
		traceId := g.GetHeader("traceId")
		if traceId == "" {
			traceId = uuid.New().String()
		}
		ctx, log := hlog.GetLogger().AddCtx(g.Request.Context(), zap.Any("traceId", traceId))
		g.Request = g.Request.WithContext(ctx)
		log.Info("AddTraceId success")
		g.Next()
	}
}

log := hlog.GetLogger().GetCtx(context.Request.Context())
		log.Info("test")
		log.Debug("test")	
例子(gin)开发模式
hlog.NewLogger()	

curl http://127.0.0.1:8888/test

{"L":"INFO","T":"2021-12-14T11:46:00.170+0800","C":"example/main.go:35","M":"hconf example success"}
{"L":"INFO","T":"2021-12-14T11:46:03.956+0800","C":"example/main.go:19","M":"AddTraceId success","traceId":"b1471a7c-5ae8-4bfd-bbdc-5312e072719c"}
{"L":"INFO","T":"2021-12-14T11:46:03.956+0800","C":"example/main.go:31","M":"test","traceId":"b1471a7c-5ae8-4bfd-bbdc-5312e072719c"}
{"L":"DEBUG","T":"2021-12-14T11:46:03.956+0800","C":"example/main.go:32","M":"test","traceId":"b1471a7c-5ae8-4bfd-bbdc-5312e072719c"}
例子(gin)生产模式
hlog.NewLogger(
	hlog.SetDevelopment(false))

curl http://127.0.0.1:8888/test
	
{"level":"info","ts":1639453661.4718382,"caller":"example/main.go:36","msg":"hconf example success"}
{"level":"info","ts":1639453664.7402327,"caller":"example/main.go:19","msg":"AddTraceId success","traceId":"68867b89-c949-45a4-b325-86866c9f869a"}
{"level":"info","ts":1639453664.7402515,"caller":"example/main.go:32","msg":"test","traceId":"68867b89-c949-45a4-b325-86866c9f869a"}
{"level":"debug","ts":1639453664.7402549,"caller":"example/main.go:33","msg":"test","traceId":"68867b89-c949-45a4-b325-86866c9f869a"}
		

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type HLogger

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

func Default

func Default() *HLogger

GetLogger returns logger

func NewLogger

func NewLogger(opts ...LogOptions) *HLogger

func (*HLogger) AddCtx

func (l *HLogger) AddCtx(ctx context.Context, field ...zap.Field) (context.Context, *zap.Logger)

func (*HLogger) GetCtx

func (l *HLogger) GetCtx(ctx context.Context) *zap.Logger

func (*HLogger) GetLevel

func (l *HLogger) GetLevel() (level zapcore.Level)

func (*HLogger) WithContext

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

type LogOptions

type LogOptions func(*Options)

func SetAppName

func SetAppName(appName string) LogOptions

func SetCtxKey

func SetCtxKey(ctxKey string) LogOptions

func SetDevelopment

func SetDevelopment(development bool) LogOptions

func SetLevel

func SetLevel(level string) LogOptions

func SetLogFileDir

func SetLogFileDir(logFileDir string) LogOptions

func SetMaxAge

func SetMaxAge(maxAge int) LogOptions

func SetMaxBackups

func SetMaxBackups(maxBackups int) LogOptions

func SetMaxSize

func SetMaxSize(maxSize int) LogOptions

func SetTimeFormat

func SetTimeFormat(format string) LogOptions

func SetWriteConsole

func SetWriteConsole(writeConsole bool) LogOptions

func SetWriteFile

func SetWriteFile(writeFile bool) LogOptions

type Options

type Options struct {
	Development  bool
	LogFileDir   string
	AppName      string
	Format       string
	MaxSize      int //文件多大开始切分 单位:M
	MaxBackups   int //保留文件个数
	MaxAge       int //文件保留最大实际
	Level        string
	CtxKey       string //通过 ctx 传递 hlog 对象
	WriteFile    bool
	WriteConsole bool
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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