log

package
v0.0.0-...-455e706 Latest Latest
Warning

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

Go to latest
Published: Mar 7, 2022 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const (
	FormatJson = "json"
	FormatText = "console"
)
View Source
const (
	KeyRequestID string = "requestID"
	KeyUsername  string = "username"
)

Variables

View Source
var (
	//func Int8(key string, val int8) Field {
	//	return Field{Key: key, Type: zapcore.Int8Type, Integer: int64(val)}
	//}
	Any         = zap.Any
	Array       = zap.Array
	Object      = zap.Object
	Binary      = zap.Binary
	Bool        = zap.Bool
	Bools       = zap.Bools
	ByteString  = zap.ByteString
	ByteStrings = zap.ByteStrings
	Complex64   = zap.Complex64
	Complex64s  = zap.Complex64s
	Complex128  = zap.Complex128
	Complex128s = zap.Complex128s
	Duration    = zap.Duration
	Durations   = zap.Durations
	Error       = zap.Error
	Errors      = zap.Errors
	Float32     = zap.Float32
	Float32s    = zap.Float32s
	Float64     = zap.Float64
	Float64s    = zap.Float64s
	Int         = zap.Int
	Ints        = zap.Ints
	Int8        = zap.Int8
	Int8s       = zap.Int8s
	Int16       = zap.Int16
	Int16s      = zap.Int16s
	Int32       = zap.Int32
	Int32s      = zap.Int32s
	Int64       = zap.Int64
	Int64s      = zap.Int64s
	Namespace   = zap.Namespace
	Reflect     = zap.Reflect
	Stack       = zap.Stack
	String      = zap.String
	Stringer    = zap.Stringer
	Strings     = zap.Strings
	Time        = zap.Time
	Times       = zap.Times
	Uint        = zap.Uint
	Uints       = zap.Uints
	Uint8       = zap.Uint8
	Uint8s      = zap.Uint8s
	Uint16      = zap.Uint16
	Uint16s     = zap.Uint16s
	Uint32      = zap.Uint32
	Uint32s     = zap.Uint32s
	Uint64      = zap.Uint64
	Uint64s     = zap.Uint64s
	Uintptr     = zap.Uintptr
	Uintptrs    = zap.Uintptrs
)

Functions

func Debug

func Debug(msg string, f ...Field)

func Debugf

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

调用默认的 (info)

func Debugw

func Debugw(msg string, keysAndValues ...interface{})

func Err

func Err(msg string, fields ...Field)

func Errf

func Errf(format string, v ...interface{})

func Errw

func Errw(msg string, keysAndValues ...interface{})

func Fatal

func Fatal(msg string, fields ...Field)

func Fatalf

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

func Fatalw

func Fatalw(msg string, keysAndValues ...interface{})

func Flush

func Flush()

func Info

func Info(msg string, fields ...Field)

func Infof

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

func Infow

func Infow(msg string, keysAndValues ...interface{})

func Init

func Init(opt *Option)

func New

func New(o *Option) *logger

根据参数得到相关的日志初始化

func Panic

func Panic(msg string, fields ...Field)

func Panicf

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

func Panicw

func Panicw(msg string, keysAndValues ...interface{})

func Waring

func Waring(msg string, fields ...Field)

func Waringf

func Waringf(format string, v ...interface{})

func Waringw

func Waringw(msg string, keysAndValues ...interface{})

func WithContext

func WithContext(c context.Context) context.Context

Types

type Field

type Field = zapcore.Field

Field是基础日志框中字段结构的别名。

type InfoLogger

type InfoLogger interface {
	Info(msg string, fields ...Field)
	Infof(format string, v ...interface{})
	Infow(msg string, keysAndValues ...interface{})
	Enabled() bool
}

介绍:Info 使用指定的 key/value 记录日志。 Infof 格式化记录日志。 Infow 也是使用指定的 key/value 记录日志, 跟 Info 的区别是:使用 Info 需要指定值的类型,通过指定值的日志类型,日志库底层不需要进行反射操作,所以使用 Info 记录日志性能最高

func V

func V(level int) InfoLogger

type Logger

type Logger interface {
	InfoLogger // 默认为info

	Debug(msg string, fields ...Field)
	Debugf(format string, v ...interface{})
	Debugw(msg string, keysAndValues ...interface{})

	Waring(msg string, fields ...Field)
	Waringf(format string, v ...interface{})
	Waringw(msg string, keysAndValues ...interface{})

	Err(msg string, fields ...Field)
	Errf(format string, v ...interface{})
	Errw(msg string, keysAndValues ...interface{})

	Panic(msg string, fields ...Field)
	Panicf(format string, v ...interface{})
	Panicw(msg string, keysAndValues ...interface{})

	Fatal(msg string, fields ...Field)
	Fatalf(format string, v ...interface{})
	Fatalw(msg string, keysAndValues ...interface{})

	// V默认只支持info等级的日志 实现功能为打印等级较高的日志
	// l.V(1).Infow("level1") l.V(2).Infow("level2")
	// 会打印 V(1) 的内容 因为实际是按照等级*-1来判断的;因为>0的值为默认的日志等级
	// 注意在使用初始化时需要保证按照第一个等级进行初始化,然后若是下个等级>其 则会将其覆盖
	V(level int) InfoLogger // 这里存在稍许的问题

	// 根据log配置写入相应的位置
	Write(p []byte) (n int, err error)

	// WithValues 可以返回一个携带指定 key-value 的 Logger,供后面使用 即在最后追加相应的 key val 值
	// lv := log.WithValues("X-Request-ID", "7a7b9f24-4cae-4b2a-9464-69088b45b904")
	// lv.Infow("Info message printed with [WithValues] logger")
	// 打印结果:2021-07-06 14:15:28.555 INFO Info message printed with [WithValues] logger {"X-Request-ID": "7a7b9f24-4cae-4b2a-9464-69088b45b904"}
	WithValues(keysAndValues ...interface{}) Logger

	WithName(name string) Logger

	// WithContext 和 FromContext 用来将指定的 Logger 添加到某个 Context 中,以及从某个 Context 中获取 Logger
	WithContext(c context.Context) context.Context

	// L() 方法Context 中提取出指定的 key-value 对,作为上下文添加到日志输出中。
	// 该项目会从传入的 Context 中提取出 requestID 和 username ,追加到 Logger 中,并返回 Logger。
	L(ctx context.Context) Logger

	Flush()
}

构建 Log 接口

func FromContext

func FromContext(ctx context.Context) Logger

从context得到相关的日志接口

func L

func L(ctx context.Context) Logger

func NewLogger

func NewLogger(l *zap.Logger) Logger

根据zapLog初始化logger

func WithName

func WithName(s string) Logger

func WithValues

func WithValues(keysAndValues ...interface{}) Logger

type Option

type Option struct {
	OutputPaths       []string `json:"output-paths"`       // 输出位置 log写入位置 或者打印等
	ErrorOutputPaths  []string `json:"error-output-paths"` // 因为是基于zap包封装而来的所以若调用zap发生错误时,可以指定进行输出
	Level             string   `json:"level"`              // 日志等级
	Encoding          string   `json:"format"`             // 日志格式 目前 json / text
	DisableCaller     bool     `json:"disable-caller"`     // 是否支持得到日志所在位置,行号,函数名
	DisableStacktrace bool     `json:"disable-stacktrace"` // 是否在 Panic 及以上级别禁止打印堆栈信息。
	EnableColor       bool     `json:"enable-color"`       // 是否支持颜色输出 (不同等级打印颜色不同) true,是;false,否。
	Name              string   `json:"name"`               // 日志名
	//在调试时使用的
	Development bool `json:"development"` // 是否开发者模式 如果是开发模式,会对 DPanicLevel 进行堆栈跟踪
}

构建log配置参数

func NewOptions

func NewOptions() *Option

初始化选项 默认配置

func (*Option) AddFlags

func (o *Option) AddFlags(flag *pflag.FlagSet)

AddFlags 方法可以将 Options 的各个字段追加到传入的 pflag.FlagSet 变量中

func (*Option) Build

func (o *Option) Build() error

Build 方法可以根据 Options 在 zap 包中构建一个全局的 Logger

func (*Option) Validate

func (o *Option) Validate() []error

Validate 测试

Directories

Path Synopsis
Package klog init klog logger.
Package klog init klog logger.

Jump to

Keyboard shortcuts

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