log

package
v0.1.5 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2020 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// JSONFormat 以 JSON 形式输出
	JSONFormat = "json"
	// TextFormat 以 TEXT 形式输出
	TextFormat = "console"
)

日志输出格式

View Source
const (
	DebugLevel = zap.DebugLevel
	InfoLevel  = zap.InfoLevel
	WarnLevel  = zap.WarnLevel
	ErrorLevel = zap.ErrorLevel
	PanicLevel = zap.PanicLevel
	FatalLevel = zap.FatalLevel
)
View Source
const (
	IdKey    = "@logId"
	ErrorKey = "@error"
	ScopeKey = "@scope"
)
View Source
const CallerSkipOffset = 2

CallerSkipOffset 调用栈便宜,用于输出日志位置

View Source
const (
	// ContextLoggerKey 在 context 中存储 logger 的 key
	ContextLoggerKey = "context.logger.key"
)
View Source
const (
	// SampleCallerSkipOffset 通过内置 logger 输出日志, Caller + 1
	SampleCallerSkipOffset = 1
)

Variables

View Source
var (
	// Debug 内置 logger 以 debug 等级输出日志
	Debug = simpleLogger.Debug
	// DebugF 通过内置 logger, 以 debug 等级格式化输出日志
	DebugF = simpleLogger.DebugF
	// DebugWithFields 通过内置 logger, 以 debug 等级输出日志,并附加 fields 信息
	DebugWithFields = simpleLogger.DebugWithField

	// Info 内置 logger 以 info 等级输出日志
	Info = simpleLogger.Info
	// InfoF 通过内置 logger, 以 info 等级格式化输出日志
	InfoF = simpleLogger.InfoF
	// InfoWithFields 通过内置 logger, 以 info 等级输出日志,并附加 fields 信息
	InfoWithFields = simpleLogger.InfoWithField

	// Warn 内置 logger 以 warn 等级输出日志
	Warn = simpleLogger.Warn
	// WarnF 通过内置 logger, 以 warn 等级格式化输出日志
	WarnF = simpleLogger.WarnF
	// WarnWithFields 通过内置 logger, 以 warn 等级输出日志,并附加 fields 信息
	WarnWithFields = simpleLogger.WarnWithField

	// Error 内置 logger 以 error 等级输出日志
	Error = simpleLogger.Error
	// ErrorF 通过内置 logger, 以 error 等级格式化输出日志
	ErrorF = simpleLogger.ErrorF
	// ErrorWithFields 通过内置 logger, 以 error 等级输出日志,并附加 fields 信息
	ErrorWithFields = simpleLogger.ErrorWithField
)

Functions

func DebugCtx

func DebugCtx(ctx context.Context, message string)

DebugCtx 通过 ctx 获取 logger, 并以 debug 等级输出日志

func DebugCtxF

func DebugCtxF(ctx context.Context, format string, args ...interface{})

DebugCtxF 通过 ctx 获取 logger, 并以 debug 等级格式化输出日志

func DebugCtxWithFields

func DebugCtxWithFields(ctx context.Context, message string, fields Fields)

DebugCtxWithFields 通过 ctx 获取 logger, 以 debug 等级输出日志,并附加 fields 信息

func ErrorCtx

func ErrorCtx(ctx context.Context, message string)

func ErrorCtxF

func ErrorCtxF(ctx context.Context, format string, args ...interface{})

func ErrorCtxWithFields

func ErrorCtxWithFields(ctx context.Context, message string, fields Fields)

func Fatal

func Fatal(message string)

Fatal 使用内置logger,以 fatal 等级输出日志

func FatalCtx

func FatalCtx(ctx context.Context, message string)

FatalCtx 通过 ctx 获取 logger,以 fatal 等级输出日志

func FatalCtxF

func FatalCtxF(ctx context.Context, format string, args ...interface{})

FatalCtxF 通过 ctx 获取 logger,以 fatal 等级格式化输出日志

func FatalCtxWithFields

func FatalCtxWithFields(ctx context.Context, message string, fields Fields)

FatalCtxWithFields 通过 ctx 获取 logger,以 fatal 等级输出日志,并 附加 fields 信息

func FatalF

func FatalF(format string, args ...interface{})

FatalF 通过 ctx 获取 logger,以 fatal 等级格式化输出日志

func FatalWithFields

func FatalWithFields(message string, fields Fields)

FatalWithFields 通过 内置 logger,以 fatal 等级输出日志,并 附加 fields 信息

func InfoCtx

func InfoCtx(ctx context.Context, message string)

func InfoCtxF

func InfoCtxF(ctx context.Context, format string, args ...interface{})

func InfoCtxWithFields

func InfoCtxWithFields(ctx context.Context, message string, fields Fields)

func Panic

func Panic(message string)

func PanicCtx

func PanicCtx(ctx context.Context, message string)

func PanicCtxF

func PanicCtxF(ctx context.Context, format string, args ...interface{})

PanicCtxF 通过 ctx 获取 logger,以 panic 等级格式化输出日志

func PanicCtxWithFields

func PanicCtxWithFields(ctx context.Context, message string, fields Fields)

PanicCtxWithFields 通过 ctx 获取logger,以 panic 等级输出日志,并 附加 fields 信息

func PanicF

func PanicF(format string, args ...interface{})

PanicF 通过内置 logger,以 panic 等级格式化输出日志

func PanicWithFields

func PanicWithFields(message string, fields Fields)

PanicWithFields 通过内置 logger,以 panic 等级输出日志,并 附加 fields 信息

func SetLevel

func SetLevel(level Level)

SetLevel 设置默认 logger 输出级别

func WarnCtx

func WarnCtx(ctx context.Context, message string)

func WarnCtxF

func WarnCtxF(ctx context.Context, format string, args ...interface{})

func WarnCtxWithFields

func WarnCtxWithFields(ctx context.Context, message string, fields Fields)

func WithLogger

func WithLogger(ctx context.Context, logger *Logger) context.Context

WithLogger 将 logger 存储到指定 context 中

Types

type Config

type Config = zap.Config

Config 类型别名,日志配置

func NewDefaultConfig

func NewDefaultConfig(format string, level Level) Config

NewDefaultConfig 默认日志配置

type EncoderConfig

type EncoderConfig = zapcore.EncoderConfig

EncoderConfig 类型别名,棉麻配置

func NewDefaultEncoderConfig

func NewDefaultEncoderConfig() EncoderConfig

NewDefaultEncoderConfig 创建默认编码配置信息

type Fields

type Fields map[string]interface{}

type Level

type Level = zapcore.Level

Level 类型别名,日志级别

type Logger

type Logger struct {
	// contains filtered or unexported fields
}

func ExtractLogger

func ExtractLogger(ctx context.Context) *Logger

func NewLogger

func NewLogger(format string, level Level) *Logger

func NewLoggerWithConfig

func NewLoggerWithConfig(config Config) (*Logger, error)

NewLoggerWithConfig 通过自定义配置创建 logger

func (*Logger) AddCallerSkip

func (l *Logger) AddCallerSkip(skip int) *Logger

AddCallerSkip 创建新的 logger 并且增加 CallerSkip

func (*Logger) Debug

func (l *Logger) Debug(message string)

数据 Debug 日志

func (*Logger) DebugF

func (l *Logger) DebugF(format string, args ...interface{})

func (*Logger) DebugWithField

func (l *Logger) DebugWithField(message string, fields Fields)

输出自定义 Key-Value 对 性能优于 WithField

func (*Logger) Error

func (l *Logger) Error(message string)

输出 Error 日志

func (*Logger) ErrorF

func (l *Logger) ErrorF(format string, args ...interface{})

func (*Logger) ErrorWithField

func (l *Logger) ErrorWithField(message string, fields Fields)

func (*Logger) Fatal

func (l *Logger) Fatal(message string)

Fatal 输出 Fatal 日志

func (*Logger) FatalF

func (l *Logger) FatalF(format string, args ...interface{})

func (*Logger) FatalWithField

func (l *Logger) FatalWithField(message string, fields Fields)

func (*Logger) Info

func (l *Logger) Info(message string)

输出 Info 日志

func (*Logger) InfoF

func (l *Logger) InfoF(format string, args ...interface{})

func (*Logger) InfoWithField

func (l *Logger) InfoWithField(message string, fields Fields)

func (*Logger) NewNamespace

func (l *Logger) NewNamespace(name string) *Logger

NewNamespace 派生 Logger 并创建一个 NameSpace

func (*Logger) Panic

func (l *Logger) Panic(message string)

Panic 输出 panic 日志

func (*Logger) PanicF

func (l *Logger) PanicF(format string, args ...interface{})

func (*Logger) PanicWithField

func (l *Logger) PanicWithField(message string, fields Fields)

func (*Logger) SetLevel

func (l *Logger) SetLevel(level Level)

SetLevel 设置当前 logger 以所有派生 logger 日志等级

func (*Logger) SetLevelHandler

func (l *Logger) SetLevelHandler(w http.ResponseWriter, r *http.Request)

SetLevelHandler 设置 Web handler, 动态改变日志输出等级

func (*Logger) SetName

func (l *Logger) SetName(name string) *Logger

SetName 派生 Logger 并设置 Logger Name

func (*Logger) Sync

func (l *Logger) Sync()

Sync 刷新数据 flushing any buffered log entries, take care to call sync before exiting.

func (*Logger) Warn

func (l *Logger) Warn(message string)

输出 Warn 日志

func (*Logger) WarnF

func (l *Logger) WarnF(format string, args ...interface{})

func (*Logger) WarnWithField

func (l *Logger) WarnWithField(message string, fields Fields)

func (*Logger) WithError

func (l *Logger) WithError(err error) *Logger

WithError 派生一个 Logger,并附加 Error 信息

func (*Logger) WithField

func (l *Logger) WithField(key string, value interface{}) *Logger

设置 Key-Value 对

func (*Logger) WithLogId

func (l *Logger) WithLogId(id string) *Logger

WithLogId 写入 LogID

func (*Logger) WithOutFile

func (l *Logger) WithOutFile(outputPaths []string, level Level) (*Logger, func(), error)

WithOutFile 派生一个Logger,将日志写入指定文件列表

func (*Logger) WithOutput

func (l *Logger) WithOutput(writer io.Writer, level Level, format string) *Logger

WithOutput 派生一个Logger,附加 writer, 收集日志信息

func (*Logger) WithSampling

func (l *Logger) WithSampling(opts *SamplingOption) *Logger

func (*Logger) WithScope

func (l *Logger) WithScope(scope string) *Logger

WithScope 派生一个 Logger,并记录 Scope

func (*Logger) Write added in v0.1.5

func (l *Logger) Write(msg []byte) (int, error)

Write impl io.Writer

type Options

type Options struct {
	// 日志文件名称
	FileName string `json:"file_name" mapstructure:"file_name"`

	// 日志备份路径
	BackupPath string `json:"backup_path" mapstructure:"backup_path"`

	// 日志文件最大字节数,单位为MB
	MaxSize int `json:"max_size" mapstructure:"max_size"`

	// 备份文件,日期后缀格式
	SuffixFormat string `json:"suffix_format" mapstructure:"suffix_format"`
}

Options 日志自动备份配置

type RotateWriter

type RotateWriter struct {
	// contains filtered or unexported fields
}

func NewWriter

func NewWriter(opts *Options) *RotateWriter

NewWriter 根据配置信息创建 RotateWriter

func (*RotateWriter) Close

func (r *RotateWriter) Close() error

func (*RotateWriter) Write

func (r *RotateWriter) Write(data []byte) (n int, err error)

Write 实现 Writer 接口

type SamplingOption

type SamplingOption struct {
	Tick       time.Duration
	Initial    int `json:"initial" yaml:"initial"`
	Thereafter int `json:"thereafter" yaml:"thereafter"`
}

SamplingOption Sampling 配置参数

Jump to

Keyboard shortcuts

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