logger

package
v1.1.9 Latest Latest
Warning

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

Go to latest
Published: Jan 10, 2021 License: MIT, MIT Imports: 15 Imported by: 4

README

golang logger

 Logger module based on uber zap package,
 which can be used to record operation logs in go applications
     1) Support automatic log cutting and maximum retention time
     2) Support log json formatting processing
     3) Support log output to file and terminal at the same time
     4) Support log printing level and log dyeing function
     
基于uber zap封装而成的logger模块,可用于go应用中记录操作日志
    1)支持日志自动切割和最大保留时长
    2)支持日志json格式化处理
    3)支持日志同时输出到文件和终端
    4)支持日志打印级别和日志染色功能
    5)支持默认日志句柄调用,比如Info,Warn等方法调用

how to use logger

please see example

zap

https://github.com/uber-go/zap

Documentation

Overview

logger 基于zap日志库,进行封装的logger库 支持日志自动切割

Index

Constants

This section is empty.

Variables

View Source
var (
	// XRequestID request_id
	XRequestID = CtxKey{"x-request-id"}

	// ReqClientIP  client_ip
	ReqClientIP = CtxKey{"client_ip"}

	// RequestMethod request method
	RequestMethod = CtxKey{"request_method"}

	// RequestURI request uri
	RequestURI = CtxKey{"request_uri"}

	// LocalTime local_time
	LocalTime = CtxKey{"local_time"}

	// CurHostname current hostname
	CurHostname = CtxKey{"hostname"}

	// Fullstack full stack
	Fullstack = CtxKey{"full_stack"}
)
View Source
var (

	// DefaultLogDir default log dir.
	DefaultLogDir = "./logs"

	// DefaultLogFile default log file.
	DefaultLogFile = "zap.log"
)

Functions

func DPanic added in v1.1.1

func DPanic(ctx context.Context, msg string, fields ...interface{})

DPanic 调试模式下的panic,程序不退出,继续运行

func Debug added in v1.1.1

func Debug(ctx context.Context, msg string, fields ...interface{})

Debug debug级别日志

func Default added in v1.1.1

func Default(opts ...Option)

Default 默认zap logger对象

func Error added in v1.1.1

func Error(ctx context.Context, msg string, fields ...interface{})

Error 错误类型的日志

func Fatal added in v1.1.1

func Fatal(ctx context.Context, msg string, fields ...interface{})

Fatal 抛出致命错误,然后退出程序

func Info added in v1.1.1

func Info(ctx context.Context, msg string, fields ...interface{})

Info info级别日志

func Md5

func Md5(str string) string

Md5 md5 func

func NewLogSugar

func NewLogSugar(opts ...Option) *zap.SugaredLogger

NewLogSugar zap log sugar语法糖 支持Debug,Info,Error,Panic,Warn,Fatal等方法 返回一个*zap.SugaredLogger

func Panic added in v1.1.1

func Panic(ctx context.Context, msg string, fields ...interface{})

Panic 抛出panic的时候,先记录日志,然后执行panic,退出当前goroutine 如果没有捕获,就会退出当前程序,建议程序做defer捕获处理

func RandInt64

func RandInt64(min, max int64) int64

RandInt64 crete a num [m,n]

func Recover added in v1.1.1

func Recover(ctx context.Context, msg string, fields ...interface{})

Recover 用来捕获程序运行出现的panic信息,并记录到日志中 这个panic信息,将采用 DPanic 方法进行记录

func RndUUID

func RndUUID() string

RndUUID realizes unique uuid based on time ns and random number There is no duplication of uuid on a single machine If you want to generate non-duplicate uuid on a distributed architecture Just add some custom strings in front of rndStr Return format: eba1e8cd-0460-4910-49c6-44bdf3cf024d

func RndUUIDMd5

func RndUUIDMd5() string

RndUUIDMd5 make an md5 uuid

func Warn added in v1.1.1

func Warn(ctx context.Context, msg string, fields ...interface{})

Warn 警告类型的日志

Types

type CtxKey

type CtxKey struct {
	Name string
}

CtxKey ctx key struct.

func (CtxKey) String

func (c CtxKey) String() string

String CtxKey string.

type Logger

type Logger interface {
	// Debug debug级别日志
	Debug(ctx context.Context, msg string, fields ...interface{})

	// Info info级别日志
	Info(ctx context.Context, msg string, fields ...interface{})

	// Error 错误类型的日志
	Error(ctx context.Context, msg string, fields ...interface{})

	// Warn 警告类型的日志
	Warn(ctx context.Context, msg string, fields ...interface{})

	// DPanic 调试模式下的panic,程序不退出,继续运行
	DPanic(ctx context.Context, msg string, fields ...interface{})

	// Recover 用来捕获程序运行出现的panic信息,并记录到日志中
	// 这个panic信息,将采用 DPanic 方法进行记录
	Recover(ctx context.Context, msg string, fields ...interface{})

	// Panic 抛出panic的时候,先记录日志,然后执行panic,退出当前goroutine
	// 如果没有捕获,就会退出当前程序,建议程序做defer捕获处理
	Panic(ctx context.Context, msg string, fields ...interface{})

	// Fatal 抛出致命错误,然后退出程序
	Fatal(ctx context.Context, msg string, fields ...interface{})
}

Logger interface.

func New

func New(opts ...Option) Logger

New 创建一个Logger interface.

type Option

type Option func(z *zapLogWriter)

func WithAddCaller

func WithAddCaller(b bool) Option

WithAddCaller 是否输出文件名和行号

func WithCallerSkip

func WithCallerSkip(skip int) Option

* WithCallerSkip 设置callerSkip addCaller = true,并且 callerSkip > 0 会设置zap.AddCallerSkip zap源码包中logger.go#260 check func check must always be called directly by a method in the Logger interface (e.g., Check, Info, Fatal). const callerSkipOffset = 2 这里的callerSkipOffset默认是2 如果基于这个Logger包,再包装一次,这个 skip = 2,以此类推 否则 skip=1

func WithCompress

func WithCompress(b bool) Option

WithCompress 日志是否压缩

func WithEnableCatchStack

func WithEnableCatchStack(b bool) Option

WithEnableCatchStack 当出现panic的时候,在使用Panic记录日志是否捕获stack信息

func WithEnableColor

func WithEnableColor(b bool) Option

WithEnableColor 是否日志染色

func WithHostname

func WithHostname(hostname string) Option

WithHostname 自定义hostname

func WithJsonFormat

func WithJsonFormat(b bool) Option

WithJsonFormat 是否json格式化

func WithLogDir

func WithLogDir(dir string) Option

WithLogDir 日志目录

func WithLogFilename

func WithLogFilename(filename string) Option

WithLogFilename 日志文件名

func WithLogLevel

func WithLogLevel(level zapcore.Level) Option

WithLogLevel 日志级别,设置日志打印最低级别,如果不设置默认为info级别 zap.InfoLevel is the default logging priority.

func WithMaxAge

func WithMaxAge(d int) Option

WithMaxAge 日志保留时间,单位day

func WithMaxSize

func WithMaxSize(size int) Option

WithMaxSize 日志大小,单位MB

func WithStdout

func WithStdout(b bool) Option

WithStdout 是否输出到终端

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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