log

package
v0.0.0-...-50a2d83 Latest Latest
Warning

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

Go to latest
Published: Mar 1, 2023 License: MIT Imports: 11 Imported by: 0

README

log

特性

pkg/log 是基于 zap 包封装一个日志包。具有如下特性:

  • 支持日志级别:DebugInfoWarnErrorPanicFatal
  • 支持自定义配置。
  • 支持文件名和行号。
  • 支持输出到标准输出(stdout)和文件,可以同时输出到多个地方。
  • 支持 JSONText 两种日志格式。
  • 支持颜色输出。
  • 兼容标准的 log 包。
  • 支持结构化日志记录。
  • 支持日志滚动
  • 支持日志文件压缩
  • 支持动态调整日志级别

使用方法

参考 pkg/log/example/example.go

Documentation

Index

Constants

View Source
const (
	KeyRequestID string = "requestID"
	KeyUsername  string = "username"
)

Variables

View Source
var (
	DebugLevel = zapcore.DebugLevel
	InfoLevel  = zapcore.InfoLevel
	WarnLevel  = zapcore.WarnLevel
	ErrorLevel = zapcore.ErrorLevel
	PanicLevel = zapcore.PanicLevel
	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
)

Alias for zap type functions.

Functions

func Debug

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

func Debugf

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

func Debugw

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

func Error

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

func Errorf

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

func Errorw

func Errorw(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(opts *Options)

Init 用指定的 Options 初始化 log 包的全局 logger.

func L

func L(ctx context.Context) *zapLogger

func New

func New(opts *Options) *zapLogger

New 通过 opts 新建 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 SugaredLogger

func SugaredLogger() *zap.SugaredLogger

SugaredLogger returns global sugared logger.

func Warn

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

func Warnf

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

func Warnw

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

func WithContext

func WithContext(ctx context.Context) context.Context

func ZapLogger

func ZapLogger() *zap.Logger

ZapLogger 用于其它 log 的包装

Types

type Field

type Field = zapcore.Field

type Level

type Level = zapcore.Level

type Logger

type Logger interface {
	// Debug 日志
	Debug(msg string, fields ...Field)
	Debugf(format string, v ...interface{})
	Debugw(msg string, keysAndValues ...interface{})

	// Info 日志
	Info(msg string, fields ...Field)
	Infof(format string, v ...interface{})
	Infow(msg string, keysAndValues ...interface{})

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

	// Error 日志
	Error(msg string, fields ...Field)
	Errorf(format string, v ...interface{})
	Errorw(msg string, keysAndValues ...interface{})

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

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

	// WithValues 向 logger 添加一些上下文的 key-value 对。
	WithValues(keysAndValues ...interface{}) Logger

	// WithName 向 logger 的名称添加一个新元素,建议名称段仅包含字母、数字和连字符
	WithName(name string) Logger

	// WithContext 返回设置了日志值的 context 副本
	WithContext(ctx context.Context) context.Context

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

Logger 表示记录日志的能力

func FromContext

func FromContext(ctx context.Context) Logger

FromContext returns the value of the log key on the ctx.

func NewLogger

func NewLogger(l *zap.Logger) Logger

NewLogger 使用给定的 Zap Logger 来创建一个 Logger

func WithName

func WithName(s string) Logger

func WithValues

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

type Options

type Options struct {
	// Logger 的名字
	Name string `json:"name" mapstructure:"name"`
	// 是否是开发模式。如果是开发模式,会对 WarnLevel 以上进行堆栈跟踪;
	//               非开发模式,会对 PanicLevel 以上进行堆栈跟踪。
	Development bool `json:"development" mapstructure:"development"`
	// 日志级别,优先级从低到高依次为:Debug, Info, Warn, Error, Dpanic, Panic, Fatal
	Level string `json:"level" mapstructure:"level"`
	// 是否开启颜色输出,true,是;false,否
	EnableColor bool `json:"enable-color" mapstructure:"enable-color"`
	// 是否开启 caller,如果开启会在日志中显示调用日志所在的文件、函数和行号
	DisableCaller bool `json:"disable-caller" mapstructure:"disable-caller"`
	// 是否在 WarnLevel(开发模式) 或 PanicLevel(非开发模式) 及以上级别禁止打印堆栈信息
	DisableStacktrace bool `json:"disable-stacktrace" mapstructure:"disable-stacktrace"`
	// 支持的日志输出格式,支持 Console 和 JSON 两种
	Format string `json:"format" mapstructure:"format"`
	// 支持输出到多个输出,用逗号分开。支持输出到标准输出(stdout)和文件
	OutputPaths []string `json:"output-paths" mapstructure:"output-paths"`
	// zap 内部 (非业务) 错误日志输出路径,多个输出,用逗号分开
	ErrorOutputPaths []string `json:"error-output-paths" mapstructure:"error-output-paths"`
	// MaxSize 是日志文件轮转之前的最大大小(MB)。默认为 100MB。
	MaxSize int `json:"maxsize" mapstructure:"maxsize"`
	// MaxAge 是根据文件名中编码的时间戳保留旧日志文件的最大天数。
	MaxAge int `json:"maxage" mapstructure:"maxage"`
	// MaxBackups 是要保留的旧日志文件的最大数量。
	MaxBackups int `json:"maxbackups" mapstructure:"maxbackups"`
	// Compress 确定是否应使用 gzip 压缩轮转的日志文件。
	Compress bool `json:"compress" mapstructure:"compress"`
	// Port 用于指定动态修改日志输出级别端口,默认为 0 (不开启)。
	Port int `json:"port" mapstructure:"port"`
}

Options 包含所有日志相关的配置项

func NewOptions

func NewOptions() *Options

func (*Options) Build

func (o *Options) Build() error

Build 从 Config 和 Options 构建 zap 包的 logger。

func (*Options) String

func (o *Options) String() string

func (*Options) Validate

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

Validate 验证 options 字段

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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