logger

package module
v1.8.5 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2021 License: GPL-3.0 Imports: 20 Imported by: 0

README

Logger 组件

说明

本包为zap日志组件封装,用于golang项目统一日志输出和收集。

使用示例

  • 日志组件使用:zap
  • 日志输出选项:stdout stderr | 日志目录 (指定存储目录)
  • 日志级别选项:panic fatal error warning info debug
  • 生产环境级别:info
  • 开发测试级别:debug
  • 日志输出格式:json
// instance
// 第一参数为日志级别
// 第二参数为日志存储路径,文件形式记录日志则是文件路径,标准输出则 stderr
var log = logger.New("debug", "stderr")

// 直接使用记录一段字符串日志
log.Debug("debug data")
log.Error("debug error")

// 直接使用底层zap记录多字段日志,性能更佳,推荐方式
log.Zap.Info(
    "msg",
    zap.String("module", "module-can-use-kibana"),
    zap.String("sql", "your sql"),
    zap.Int("num", 101),
)

本库亦封装实现了rediselasticgorm的logger实现,以使用底层zap日志组件。

版本说明

logger/v1.1.x 适配go-redis v7

logger/v1.2.x 适配go-redis v8

Documentation

Index

Constants

View Source
const (
	XRequestID          = "x-request-id"       // 请求ID名称
	TextGinPanic        = "gin.panic.recovery" // gin panic日志标记
	TextGinRequest      = "gin.request"        // gin request请求日志标记
	TextGinResponseFail = "gin.response.fail"  // gin 业务层面失败响应日志标记
	TextGinPreflight    = "gin.preflight"      // gin preflight 御剑options请求类型日志
)

XRequestID 为每个请求分配的请求编号key和名称 1、优先从header头里读由nginx维护的并且转发过来的x-request-id 2、如果读取不到则使用当前纳秒时间戳字符串

Variables

This section is empty.

Functions

func GetRequestBody added in v1.8.3

func GetRequestBody(ctx *gin.Context) string

GetRequestBody 获取请求body体

func GetRequestID added in v1.8.4

func GetRequestID(ctx *gin.Context) string

GetRequestID 暴露方法:读取当前请求ID

func GinCors added in v1.8.5

func GinCors(ctx *gin.Context)

GinCors 为gin开启跨域功能<尽量通过nginx反代处理>

func GinLogHttpFail added in v1.8.4

func GinLogHttpFail(ctx *gin.Context, err error)

GinLogHttpFail gin框架失败响应日志处理

func GinLogger added in v1.8.3

func GinLogger(ctx *gin.Context)

GinLogger zap实现的gin-logger日志中间件<gin.HandlerFunc的实现>

func GinRecovery added in v1.8.3

func GinRecovery(ctx *gin.Context)

GinRecovery zap实现的gin-recovery日志中间件<gin.HandlerFunc的实现>

func NewGorm2Logger added in v1.8.3

func NewGorm2Logger() logger.Interface

NewGorm2Logger 创建gorm2的logger实例

Types

type DBLogger

type DBLogger struct{}

DBLogger gorm日志记录器

func (DBLogger) Print

func (l DBLogger) Print(values ...interface{})

Print db log record

type EsLogger

type EsLogger struct{}

EsLogger es 日志记录器

func (EsLogger) Printf

func (l EsLogger) Printf(format string, values ...interface{})

Print es log record

type InterFace

type InterFace interface {
	Info(msg string)
	Debug(msg string)
	Error(msg string)
}

InterFace 通用logger接口定义

type Logger

type Logger struct {
	Zap *zap.Logger
}

Logger logger封装, 实现第三方库的日志接口

func New

func New(level, path string) *Logger

New 初始化单例logger @param level 日志级别:debug、info、warning 等 @param path 文件形式的日志路径 or 标准输出 stderr

func (Logger) Debug

func (l Logger) Debug(msg string)

func (Logger) Debugf

func (l Logger) Debugf(format string, args ...interface{})

func (Logger) Error

func (l Logger) Error(msg string)

func (Logger) Errorf

func (l Logger) Errorf(format string, args ...interface{})

func (Logger) Info

func (l Logger) Info(msg string)

func (Logger) Infof

func (l Logger) Infof(format string, args ...interface{})

func (Logger) Print

func (l Logger) Print(v ...interface{})

func (Logger) Printf

func (l Logger) Printf(format string, v ...interface{})

func (Logger) Warn

func (l Logger) Warn(msg string)

func (Logger) Warnf

func (l Logger) Warnf(format string, args ...interface{})

type RedisHook

type RedisHook struct{}

RedisHook redis hook implement

func (RedisHook) AfterProcess

func (r RedisHook) AfterProcess(ctx context.Context, cmd redis.Cmder) error

BeforeProcess redis执行命令后hook

func (RedisHook) AfterProcessPipeline

func (r RedisHook) AfterProcessPipeline(ctx context.Context, cmdItems []redis.Cmder) error

BeforeProcess redis执行pipe后hook

func (RedisHook) BeforeProcess

func (r RedisHook) BeforeProcess(ctx context.Context, cmd redis.Cmder) (context.Context, error)

BeforeProcess redis执行命令前hook

func (RedisHook) BeforeProcessPipeline

func (r RedisHook) BeforeProcessPipeline(ctx context.Context, cmdItems []redis.Cmder) (context.Context, error)

BeforeProcess redis执行pipe前hook

Jump to

Keyboard shortcuts

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