log

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2020 License: MIT Imports: 14 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

This section is empty.

Functions

func Debug added in v0.1.1

func Debug(message string)

Debug 内置 logger 以 debug 等级输出日志

func DebugCtx added in v0.1.1

func DebugCtx(ctx context.Context, message string)

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

func DebugCtxF added in v0.1.1

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

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

func DebugCtxWithFields added in v0.1.1

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

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

func DebugF added in v0.1.1

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

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

func DebugWithFields added in v0.1.1

func DebugWithFields(message string, fields Fields)

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

func Error added in v0.1.1

func Error(message string)

func ErrorCtx added in v0.1.1

func ErrorCtx(ctx context.Context, message string)

func ErrorCtxF added in v0.1.1

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

func ErrorCtxWithFields added in v0.1.1

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

func ErrorF added in v0.1.1

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

func ErrorWithFields added in v0.1.1

func ErrorWithFields(message string, fields Fields)

func Fatal added in v0.1.1

func Fatal(message string)

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

func FatalCtx added in v0.1.1

func FatalCtx(ctx context.Context, message string)

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

func FatalCtxF added in v0.1.1

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

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

func FatalCtxWithFields added in v0.1.1

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

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

func FatalF added in v0.1.1

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

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

func FatalWithFields added in v0.1.1

func FatalWithFields(message string, fields Fields)

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

func Info added in v0.1.1

func Info(message string)

func InfoCtx added in v0.1.1

func InfoCtx(ctx context.Context, message string)

func InfoCtxF added in v0.1.1

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

func InfoCtxWithFields added in v0.1.1

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

func InfoF added in v0.1.1

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

func InfoWithFields added in v0.1.1

func InfoWithFields(message string, fields Fields)

func Panic added in v0.1.1

func Panic(message string)

func PanicCtx added in v0.1.1

func PanicCtx(ctx context.Context, message string)

func PanicCtxF added in v0.1.1

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

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

func PanicCtxWithFields added in v0.1.1

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

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

func PanicF added in v0.1.1

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

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

func PanicWithFields added in v0.1.1

func PanicWithFields(message string, fields Fields)

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

func SetLevel added in v0.1.1

func SetLevel(level Level)

SetLevel 设置默认 logger 输出级别

func Warn added in v0.1.1

func Warn(message string)

func WarnCtx added in v0.1.1

func WarnCtx(ctx context.Context, message string)

func WarnCtxF added in v0.1.1

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

func WarnCtxWithFields added in v0.1.1

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

func WarnF added in v0.1.1

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

func WarnWithFields added in v0.1.1

func WarnWithFields(message string, fields Fields)

func WithLogger

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

WithLogger 将 logger 存储到指定 context 中

Types

type Config added in v0.1.1

type Config = zap.Config

Config 类型别名,日志配置

func NewDefaultConfig added in v0.1.1

func NewDefaultConfig(format string, level Level) Config

NewDefaultConfig 默认日志配置

type EncoderConfig added in v0.1.1

type EncoderConfig = zapcore.EncoderConfig

EncoderConfig 类型别名,棉麻配置

func NewDefaultEncoderConfig added in v0.1.1

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 added in v0.1.0

func NewLogger(format string, level Level) *Logger

func NewLoggerWithConfig added in v0.1.1

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

NewLoggerWithConfig 通过自定义配置创建 logger

func (*Logger) AddCallerSkip added in v0.1.1

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

AddCallerSkip 创建新的 logger 并且增加 CallerSkip

func (*Logger) Debug

func (l *Logger) Debug(message string)

数据 Debug 日志

func (*Logger) DebugF added in v0.1.1

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 added in v0.1.1

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 added in v0.1.1

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 added in v0.1.1

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 added in v0.1.1

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 added in v0.1.1

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 added in v0.1.1

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 added in v0.1.1

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 added in v0.1.1

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

func (*Logger) WithScope

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

WithScope 派生一个 Logger,并记录 Scope

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 added in v0.1.1

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

func NewWriter

func NewWriter(opts *Options) *RotateWriter

NewWriter 根据配置信息创建 RotateWriter

func (*RotateWriter) Close added in v0.1.1

func (r *RotateWriter) Close() error

func (*RotateWriter) Write added in v0.1.1

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

Write 实现 Writer 接口

type SamplingOption added in v0.1.1

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