Documentation ¶
Overview ¶
logger 基于zap日志库,进行封装的logger库 支持日志自动切割
Index ¶
- Variables
- func DPanic(ctx context.Context, msg string, fields ...interface{})
- func Debug(ctx context.Context, msg string, fields ...interface{})
- func Default(opts ...Option)
- func Error(ctx context.Context, msg string, fields ...interface{})
- func Fatal(ctx context.Context, msg string, fields ...interface{})
- func Info(ctx context.Context, msg string, fields ...interface{})
- func Md5(str string) string
- func NewLogSugar(opts ...Option) *zap.SugaredLogger
- func Panic(ctx context.Context, msg string, fields ...interface{})
- func RandInt64(min, max int64) int64
- func Recover(ctx context.Context, msg string, fields ...interface{})
- func RndUUID() string
- func RndUUIDMd5() string
- func Warn(ctx context.Context, msg string, fields ...interface{})
- type CtxKey
- type Logger
- type Option
- func WithAddCaller(b bool) Option
- func WithCallerSkip(skip int) Option
- func WithCompress(b bool) Option
- func WithEnableColor(b bool) Option
- func WithHostname(hostname string) Option
- func WithJsonFormat(b bool) Option
- func WithLogDir(dir string) Option
- func WithLogFilename(filename string) Option
- func WithLogLevel(level zapcore.Level) Option
- func WithMaxAge(d int) Option
- func WithMaxSize(size int) Option
- func WithStdout(b bool) Option
Constants ¶
This section is empty.
Variables ¶
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"} )
var ( // DefaultLogDir default log dir. DefaultLogDir = "./logs" // DefaultLogFile default log file. DefaultLogFile = "zap.log" )
Functions ¶
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
Panic 抛出panic的时候,先记录日志,然后执行panic,退出当前goroutine 如果没有捕获,就会退出当前程序,建议程序做defer捕获处理
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
Types ¶
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.
type Option ¶
type Option func(z *zapLogWriter)
func WithCallerSkip ¶
* 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 WithLogLevel ¶
WithLogLevel 日志级别,设置日志打印最低级别,如果不设置默认为info级别 zap.InfoLevel is the default logging priority.