zlog

package module
v0.0.0-...-0b86307 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2024 License: MIT Imports: 16 Imported by: 2

README

zlog 是对zap三方库的封装,主要是统一日志文件的引入和调用

特性

  1. 无侵入,保留zap提供的能力:
  2. 统一配置文件类型、路径和文件名:./etc/conf.yaml
  3. 统一配置文件中的段配置
log:
  appName: admin-server
  level: -1
  debug: true
  logPath: ./log/admin-server.log
  maxSize: 128
  maxAge: 7
  maxBackups: 30
  compress: false
  1. 提供直接获取zap对象接口
  2. 每个错误种类,提供三种不同类型的日志输出:Debug/DebugF/DebugO

快速上手

现有配置文件内容如下:

log:
  appName: test-server
  level: -1
  debug: true
  logPath: ./log/test-server.log
  maxSize: 128
  maxAge: 7
  maxBackups: 30
  compress: false
//如果有自定义的配置信息,可进行重新初始化

InitLogger(&Config{
  LogPath:    "./log/test.log",
  AppName:    "log-sample",
  Level:      -1,
  MaxSize:    0,
  MaxAge:     0,
  MaxBackups: 0,
  Compress:   true,
})



  // 按map输出
  Debug("info log", Fields{"abc": 11})
  Info("info log", Fields{"abc": 11})
  Warn("info log", Fields{"abc": 11})
  Error("info log", Fields{"abc": 11})
	

  // 按字符串输出
  Debugf("format log : %d", 12)
  Infof("format log : %d", 12)
  Warnf("format log : %d", 12)
  Errorf("format log : %d", 12)

  // 输出任意对象
  DebugO("object log ", time.Now())
  InfoO("object log ", time.Now())
  WarnO("object log ", time.Now())
  ErrorO("object log ", time.Now())

  // Panic和Fatal的输出都会直接导致程序退出,所以慎用

  //FatalO("object log ", time.Now())
  //DPanic("info log", Fields{"abc": 11})
  //Panic("info log", Fields{"abc": 11})
  //Fatal("info log", Fields{"abc": 11})

  Logger().Debug("info log", Fields{"abc": 11})


开始使用

 go get github.com/aixj1984/golibs/zlog

代码覆盖率

go test -cover ./...

go test -coverprofile=coverage ./...

go tool cover -html=coverage

Documentation

Overview

Package zlog is a wrapper for zap.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DPanic

func DPanic(msg string, fields Fields)

DPanic 输出dpanic级别的日志,同时进程退出

func DPanicO

func DPanicO(msg string, object interface{})

DPanicO 输出dpanic级别的任意对象到日志,同时进程退出

func DPanicf

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

DPanicf 输出dpanic级别的format日志,同时进程退出

func Debug

func Debug(msg string, fields Fields)

Debug 输出debug级别的日志

func DebugO

func DebugO(msg string, object interface{})

DebugO 输出debug级别的任意对象到日志

func Debugf

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

Debugf 输出debug级别的format日志

func Empty

func Empty() bool

Empty 是将当前的日志对象设置为null

func Error

func Error(msg string, fields Fields)

Error 输出error级别的日志

func ErrorO

func ErrorO(msg string, object interface{})

ErrorO 输出error级别的任意对象到日志

func Errorf

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

Errorf 输出error级别的format日志

func Fatal

func Fatal(msg string, fields Fields)

Fatal 输出fatal级别的日志,同时进程退出

func FatalO

func FatalO(msg string, object interface{})

FatalO 输出fatal级别的任意对象到日志,同时进程退出

func Fatalf

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

Fatalf 输出fatal级别的format日志,同时进程退出

func Info

func Info(msg string, fields Fields)

Info 输出info级别的日志

func InfoO

func InfoO(msg string, object interface{})

InfoO 输出info级别的任意对象到日志

func Infof

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

Infof 输出info级别的format日志

func InitLogger

func InitLogger(config *Config)

InitLogger 通过传入的config,来初始化日志对象

func Panic

func Panic(msg string, fields Fields)

Panic 输出fatal级别的日志,同时进程退出

func PanicO

func PanicO(msg string, object interface{})

PanicO 输出panic级别的任意对象到日志,同时进程退出

func Panicf

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

Panicf 输出panic级别的format日志,同时进程退出

func Warn

func Warn(msg string, fields Fields)

Warn 输出warn级别的日志

func WarnO

func WarnO(msg string, object interface{})

WarnO 输出warn级别的任意对象到日志

func Warnf

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

Warnf 输出warn级别的format日志

Types

type Config

type Config struct {
	LogPath    string `mapstructure:"logPath"`
	AppName    string `mapstructure:"appName"`
	Debug      bool   `mapstructure:"debug"`
	Level      int8   `mapstructure:"level"`
	MaxSize    int    `mapstructure:"maxSize"`
	MaxAge     int    `mapstructure:"maxAge"`
	MaxBackups int    `mapstructure:"maxBackups"`
	Compress   bool   `mapstructure:"compress"`
}

Config 是log文件的参数配置

func GetConfig

func GetConfig() *Config

GetConfig 获取当前的配置文件信息

type Entry

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

Entry 是对zap的一个封装

func Logger

func Logger() *Entry

Logger 获取当前的日志对象

func NewEntry

func NewEntry(l *zap.Logger) *Entry

NewEntry 通过传入zap的logger对象,构造一个entry的对象

func (*Entry) DPanic

func (e *Entry) DPanic(msg string, fields interface{})

DPanic 输出DPanic级别的日志,同时进程退出

func (*Entry) Debug

func (e *Entry) Debug(msg string, fields interface{})

Debug 输出debug级别的日志

func (*Entry) Error

func (e *Entry) Error(msg string, fields interface{})

Error 输出error级别的日志

func (*Entry) Fatal

func (e *Entry) Fatal(msg string, fields interface{})

Fatal 输出fatal级别的日志,同时进程退出

func (*Entry) Fields

func (e *Entry) Fields() []zapcore.Field

Fields 获取实例中的所有元素

func (*Entry) GinLogger

func (e *Entry) GinLogger() gin.HandlerFunc

GinLogger 是给gin框架提供访问日志输出的中间件

func (*Entry) GinRecovery

func (e *Entry) GinRecovery(stack bool) gin.HandlerFunc

GinRecovery 是给gin框架提供访异常回复时的日志中间件;错误处理,也可以不写自己处理错误使用gin写好的错误处理

func (*Entry) Info

func (e *Entry) Info(msg string, fields interface{})

Info 输出info级别的日志

func (*Entry) Panic

func (e *Entry) Panic(msg string, fields interface{})

Panic 输出panic级别的日志,同时进程退出

func (*Entry) Warn

func (e *Entry) Warn(msg string, fields interface{})

Warn 输出warn级别的日志

func (*Entry) WithContext

func (e *Entry) WithContext(ctx context.Context) *Entry

WithContext 通过上下文获取跟踪ID的信息,构造一个实例

func (*Entry) WithError

func (e *Entry) WithError(err error) *Entry

WithError 向实例中添加err

func (*Entry) WithEvent

func (e *Entry) WithEvent(event string) *Entry

WithEvent 通过事件信息,构造一个实例

func (*Entry) WithField

func (e *Entry) WithField(key string, value interface{}) *Entry

WithField 向实例中添加日志元素

func (*Entry) WithFields

func (e *Entry) WithFields(fields ...zapcore.Field) *Entry

WithFields 向实例中添加多个日志元素

type Fields

type Fields map[string]interface{}

Fields is standardize the format type of output logs.

Jump to

Keyboard shortcuts

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