Documentation ¶
Overview ¶
Package logs 简单封装了在日常使用 zap 打日志时的常用方法.
提供快速使用 zap 打印日志的全部方法,所有日志打印方法开箱即用
提供多种快速创建 logger 的方法
支持在使用 Error 及其以上级别打印日志时自动将该事件上报到 Sentry
支持从 context.Context/gin.Context 中创建、获取带有 Trace ID 的 logger [go语言zap日志自定义输出](https://docs.lvrui.io/2020/03/25/go语言zap日志自定义输出/)
Index ¶
- Constants
- Variables
- func AttachCore(l *zap.Logger, c zapcore.Core) *zap.Logger
- func CallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder)
- func CloneLogger(name string, fields ...zap.Field) *zap.Logger
- func CtxLogger(c context.Context, fields ...zap.Field) *zap.Logger
- func CtxTraceId(c context.Context) string
- func Debug(c context.Context, msg string, fields ...zap.Field)
- func Debugf(c context.Context, template string, args ...interface{})
- func Debugs(c context.Context, args ...interface{})
- func Debugw(c context.Context, msg string, kvs ...interface{})
- func Error(c context.Context, msg string, fields ...zap.Field)
- func Errorf(c context.Context, template string, args ...interface{})
- func Errors(c context.Context, args ...interface{})
- func Errorw(c context.Context, msg string, kvs ...interface{})
- func ExtraField(kvs ...interface{}) zap.Field
- func Fatal(c context.Context, msg string, fields ...zap.Field)
- func Fatalf(c context.Context, template string, args ...interface{})
- func Fatals(c context.Context, args ...interface{})
- func Fatalw(c context.Context, msg string, kvs ...interface{})
- func FuncName(pc uintptr) string
- func Info(c context.Context, msg string, fields ...zap.Field)
- func Infof(c context.Context, template string, args ...interface{})
- func Infos(c context.Context, args ...interface{})
- func Infow(c context.Context, msg string, kvs ...interface{})
- func NewCtxLogger(c context.Context, logger *zap.Logger, traceId string) (context.Context, *zap.Logger)
- func NewLogger(options Options) (*zap.Logger, error)
- func Panic(c context.Context, msg string, fields ...zap.Field)
- func Panicf(c context.Context, template string, args ...interface{})
- func Panics(c context.Context, args ...interface{})
- func Panicw(c context.Context, msg string, kvs ...interface{})
- func PublicIP() string
- func RegisterLumberjackSink(sink *LumberjackSink) error
- func ReplaceLogger(newLogger *zap.Logger) func()
- func ServerIP() string
- func SetLevel(lvl string)
- func TextLevel() string
- func TimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder)
- func Warn(c context.Context, msg string, fields ...zap.Field)
- func Warnf(c context.Context, template string, args ...interface{})
- func Warns(c context.Context, args ...interface{})
- func Warnw(c context.Context, msg string, kvs ...interface{})
- func ZapFields(data map[string]interface{}) []zap.Field
- type AtomicLevelServerOption
- type LumberjackSink
- type Options
Constants ¶
const ( // SentryDSNEnvKey 引入包时默认创建 logger 将尝试从该环境变量名中获取 sentry dsn SentryDSNEnvKey = "SENTRY_DSN" // SentryDebugEnvKey 尝试从该环境变量中获取 sentry 是否开启 debug 模式 SentryDebugEnvKey = "SENTRY_DEBUG" // AtomicLevelAddrEnvKey 初始化时尝试获取该环境变量用于设置动态修改日志级别的 http 服务运行地址 AtomicLevelAddrEnvKey = "ATOMIC_LEVEL_ADDR" )
const (
// LogFilename 默认日志文件名
LogFilename = "/tmp/logs.log"
)
Variables ¶
var ( // CtxLoggerName define the ctx logger name CtxLoggerName = "ctx_logger" // CtxTraceKey define the trace id key CtxTraceKey = "trace" )
var ( // EncoderConfig 默认的日志字段名配置 EncoderConfig = zapcore.EncoderConfig{ TimeKey: "time", LevelKey: "level", NameKey: "logger", CallerKey: "caller", MessageKey: "msg", StacktraceKey: "stacktrace", LineEnding: zapcore.DefaultLineEnding, EncodeLevel: zapcore.CapitalLevelEncoder, EncodeTime: TimeEncoder, EncodeDuration: zapcore.SecondsDurationEncoder, EncodeCaller: CallerEncoder, } // AtomicLevelMap string level mapping zap AtomicLevel AtomicLevelMap = map[string]zap.AtomicLevel{ "debug": zap.NewAtomicLevelAt(zap.DebugLevel), "info": zap.NewAtomicLevelAt(zap.InfoLevel), "warn": zap.NewAtomicLevelAt(zap.WarnLevel), "error": zap.NewAtomicLevelAt(zap.ErrorLevel), "dpanic": zap.NewAtomicLevelAt(zap.DPanicLevel), "panic": zap.NewAtomicLevelAt(zap.PanicLevel), "fatal": zap.NewAtomicLevelAt(zap.FatalLevel), } )
Functions ¶
func AttachCore ¶
AttachCore add a core to zap logger
func CallerEncoder ¶
func CallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder)
CallerEncoder serializes a caller in package/file:funcname:line format
func CloneLogger ¶
CloneLogger return the global logger copy which add a new name
func Debugf ¶
Debugf 尝试从 Context 中获取带 trace id 的 sugared logger 来模板字符串记录 debug 级别的日志 logs.Debugf(nil, "str:%s", "abd")
func Debugs ¶
Debugs 尝试从 Context 中获取带 trace id 的 sugared logger 来记录 debug 级别的日志 logs.Debugs(nil, "abc", 123)
func Debugw ¶
Debugw 尝试从 Context 中获取带 trace id 的 sugared logger 来 kv 记录 debug 级别的日志 logs.Debugw(nil, "msg", "k1", "v1", "k2", "v2")
func ExtraField ¶
ExtraField 顺序传入 kv 对,返回以 extra 为 key ,传入的 kv 对组成的 map 为值的 zap Reflect Field 在需要固定日志外层 json 字段有需要添加新字段时可以使用
func NewCtxLogger ¶
func NewCtxLogger(c context.Context, logger *zap.Logger, traceId string) (context.Context, *zap.Logger)
NewCtxLogger return a context with logger and trace id and a logger with trace id
func RegisterLumberjackSink ¶
func RegisterLumberjackSink(sink *LumberjackSink) error
RegisterLumberjackSink 注册 lumberjack sink 在 OutputPaths 中指定输出为 sink.Scheme://log_filename 即可使用 path url 中不指定日志文件名则使用默认的名称 一个 scheme 只能对应一个文件名,相同的 scheme 注册无效,会全部写入同一个文件
func ReplaceLogger ¶
ReplaceLogger 替换默认的全局 logger 为传入的新 logger 返回函数,调用它可以恢复全局 logger 为上一次的 logger
func TimeEncoder ¶
func TimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder)
TimeEncoder 自定义日志时间格式, 不带时区信息, YYYY-mm-dd H:M:S.xxxxxx
Types ¶
type AtomicLevelServerOption ¶
type AtomicLevelServerOption struct { Addr string // http 动态修改日志级别服务运行地址 Path string // 设置 url path ,可选 Username string // 请求时设置 basic auth 认证的用户名,可选 Password string // 请求时设置 basic auth 认证的密码,可选,与 username 同时存在才开启 basic auth }
AtomicLevelServerOption AtomicLevel server 相关配置
type LumberjackSink ¶
type LumberjackSink struct { *lumberjack.Logger Scheme string }
LumberjackSink 将日志输出到 lumberjack 进行 rotate
func NewLumberjackSink ¶
func NewLumberjackSink(scheme, filename string, maxAge, maxBackups, maxSize int, compress, localtime bool) *LumberjackSink
NewLumberjackSink 创建 LumberjackSink 对象
func (LumberjackSink) Sync ¶
func (LumberjackSink) Sync() error
Sync lumberjack Logger 默认已实现 Sink 的其他方法,这里实现 Sync 后就成为一个 Sink 对象
type Options ¶
type Options struct { Name string // logger 名称 Level string // 日志级别 debug, info, warn, error dpanic, panic, fatal Format string // 日志格式 OutputPaths []string // 日志输出位置 InitialFields map[string]interface{} // 日志初始字段 DisableCaller bool // 是否关闭打印 caller DisableStacktrace bool // 是否关闭打印 stackstrace EncoderConfig *zapcore.EncoderConfig // 配置日志字段 key 的名称 LumberjackSink *LumberjackSink // lumberjack sink 支持日志文件 rotate AtomicLevelServer AtomicLevelServerOption // AtomicLevel server 相关配置 }
Options new logger options