log

package module
v1.4.0 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2023 License: MIT Imports: 10 Imported by: 6

README

log

基于 zap 包封装的日志库。

Documentation

Index

Constants

View Source
const (
	KeyRequestID = "REQUEST_ID"
	KeyEID       = "E_ID"
)

定义常见的日志字段。

Variables

View Source
var (
	// DebugLevel 日志通常是大量的,并且通常在生产中被禁用。
	DebugLevel = zapcore.DebugLevel
	// InfoLevel 是默认的日志记录优先级。
	InfoLevel = zapcore.InfoLevel
	// WarnLevel 日志比 Info 更重要,但不需要单独人工审查。
	WarnLevel = zapcore.WarnLevel
	// ErrorLevel 日志是高优先级的。 如果应用程序运行顺利,则不应生成任何错误级别的日志。
	ErrorLevel = zapcore.ErrorLevel
	// PanicLevel 记录一条消息,然后 Panic。
	PanicLevel = zapcore.PanicLevel
	// FatalLevel 记录一条消息,然后调用 os.Exit(1)。
	FatalLevel = zapcore.FatalLevel
)

错误级别

View Source
var (
	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
	Err         = 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
)

key-value 函数

Functions

func Debug

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

Debug method output debug level log.

func Debugf

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

Debugf method output debug level log.

func Debugw

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

Debugw method output debug level log.

func Error

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

Error method output error level log.

func Errorf

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

Errorf method output error level log.

func Errorw

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

Errorw method output error level log.

func Fatal

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

Fatal method output fatal level log.

func Fatalf

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

Fatalf method output fatal level log.

func Fatalw

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

Fatalw method output Fatalw level log.

func Flush

func Flush()

Flush 调用底层 Core 的 Sync 方法,刷新所有缓冲的日志条目。 应用程序应注意在退出前调用 Sync。

func Info

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

Info method output info level log.

func Infof

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

Infof method output info level log.

func Infow

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

Infow method output info level log.

func Init

func Init(opts *Options)

Init 使用指定的选项初始 logger。

func L

func L(ctx context.Context) *zapLogger

L method output with specified context value.

func New

func New(opts *Options) *zapLogger

New 通过 opts 创建 logger。

func Panic

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

Panic method output panic level log and shutdown application.

func Panicf

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

Panicf method output panic level log and shutdown application.

func Panicw

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

Panicw method output panic level log.

func StdErrLogger

func StdErrLogger() *log.Logger

StdErrLogger 返回标准库的 logger,该 logger 在 error level 写入提供的 zap logger。

func StdInfoLogger

func StdInfoLogger() *log.Logger

StdInfoLogger 返回标准库的 logger,该 logger 在 info level 写入提供的 zap logger。

func SugaredLogger

func SugaredLogger() *zap.SugaredLogger

SugaredLogger 返回全局 sugared logger.

func Warn

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

Warn method output warning level log.

func Warnf

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

Warnf method output warning level log.

func Warnw

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

Warnw method output warning level log.

func WithContext

func WithContext(ctx context.Context) context.Context

WithContext 返回设置日志值的上下文副本。

Types

type Field

type Field = zapcore.Field

Field 字段类型

type InfoLogger

type InfoLogger interface {
	// Info 使用给定的键/值对作为上下文记录非错误消息。
	//
	// msg 参数应该用于向日志行添加一些常量描述。
	// 然后可以使用键/值对添加额外的变量信息。
	// 键/值对应该交替使用字符串键和任意值。
	Info(msg string, fields ...Field)
	Infof(format string, v ...interface{})
	Infow(msg string, keysAndValues ...interface{})

	// Enabled 测试是否启用了这个 InfoLogger。
	// 例如,命令行标志可用于设置日志记录详细程度并禁用某些信息日志。
	Enabled() bool
}

InfoLogger 表示能够以特定的详细程度记录非错误消息。

func V

func V(level int) InfoLogger

V 返回一个分级的 InfoLogger。

type Level

type Level = zapcore.Level

Level 级别类型

type Logger

type Logger interface {
	// InfoLogger 所有 Logger 都实现了 InfoLogger。
	// 直接在 Logger 值上调用 InfoLogger 方法等效于在 V(0) InfoLogger 上调用它们。
	// 例如,logger.Info() 产生与 logger.V(0).Info 相同的结果。
	InfoLogger

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

	Warn(msg string, fields ...Field)
	Warnf(format string, v ...interface{})
	Warnw(msg string, keysAndValues ...interface{})

	Error(msg string, fields ...Field)
	Errorf(format string, v ...interface{})
	Errorw(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 返回特定详细级别的 InfoLogger 值。
	// 一个更高的详细级别意味着日志消息不太重要。
	// 传递小于零的日志级别是违法的。
	V(level int) InfoLogger

	Write(p []byte) (n int, err error)

	// WithValues 向 logger 添加一些上下文的键值对。
	// 有关键/值对如何工作的文档,请参阅 Info。
	WithValues(keysAndValues ...interface{}) Logger

	// WithName 向记录器的名称添加一个新元素。
	// 使用 WithName 的连续调用继续向记录器的名称附加后缀。
	// 强烈建议名称段仅包含字母、数字和连字符。
	WithName(name string) Logger

	// WithContext 返回设置日志值的上下文副本。
	WithContext(ctx context.Context) context.Context

	// Flush 调用底层 Core 的 Sync 方法,刷新所有缓冲的日志条目。
	// 应用程序应注意在退出前调用 Sync。
	Flush()
}

Logger 记录日志消息。

func FromContext

func FromContext(ctx context.Context) Logger

FromContext 返回 ctx 上的 Logger。

func NewLogger

func NewLogger(l *zap.Logger) Logger

NewLogger 使用给定的 Zap Logger 创建一个新的 log.Logger 来记录日志。

func WithName

func WithName(s string) Logger

WithName 为 logger 的名称添加一个新的路径段。默认情况下,记录器是未命名的。

func WithValues

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

WithValues 创建一个 child logger 并向其添加 Zap 字段。

type Options

type Options struct {
	OutputPaths       []string `json:"output-paths"       mapstructure:"output-paths"`
	ErrorOutputPaths  []string `json:"error-output-paths" mapstructure:"error-output-paths"`
	Level             string   `json:"level"              mapstructure:"level"`
	Format            string   `json:"format"             mapstructure:"format"`
	DisableCaller     bool     `json:"disable-caller"     mapstructure:"disable-caller"`
	DisableStacktrace bool     `json:"disable-stacktrace" mapstructure:"disable-stacktrace"`
	EnableColor       bool     `json:"enable-color"       mapstructure:"enable-color"`
	EncodeFullCaller  bool     `json:"enable-full-caller" mapstructure:"enable-full-caller"`
	Development       bool     `json:"development"        mapstructure:"development"`
	Name              string   `json:"name"               mapstructure:"name"`
}

Options 日志相关的配置项。

func NewOptions

func NewOptions() *Options

NewOptions 创建一个带有默认参数的 Options 对象。

func (*Options) AddFlags

func (o *Options) AddFlags(fs *pflag.FlagSet)

AddFlags 将 Options 的各个字段追加到传入的 pflag.FlagSet 变量中。

func (*Options) Build

func (o *Options) Build() error

Build 根据 Options 构建一个全局的 Logger。

func (*Options) String

func (o *Options) String() string

String 将 Options 的值以 JSON 格式字符串返回。

func (*Options) Validate

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

Validate 验证选项字段。

Jump to

Keyboard shortcuts

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