logger

package
v0.0.0-...-ad9b26c Latest Latest
Warning

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

Go to latest
Published: Apr 26, 2024 License: MIT Imports: 25 Imported by: 0

Documentation

Index

Constants

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

Variables

This section is empty.

Functions

func Debug

func Debug(msg string)

func DebugContext

func DebugContext(ctx context.Context, msg string)

func DebugContextF

func DebugContextF(ctx context.Context, format string, v ...interface{})

func DebugF

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

func Error

func Error(msg string)

func ErrorContext

func ErrorContext(ctx context.Context, msg string)

func ErrorContextF

func ErrorContextF(ctx context.Context, format string, v ...interface{})

func ErrorF

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

func Info

func Info(msg string)

func InfoContext

func InfoContext(ctx context.Context, msg string)

func InfoContextF

func InfoContextF(ctx context.Context, format string, v ...interface{})

func InfoF

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

func Warn

func Warn(msg string)

func WarnContext

func WarnContext(ctx context.Context, msg string)

func WarnContextF

func WarnContextF(ctx context.Context, format string, v ...interface{})

func WarnF

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

Types

type FileSegmentOptions

type FileSegmentOptions struct {
	// MaxSize 进行切割之前,日志文件的最大大小(MB为单位),默认为100MB
	MaxSize int `json:"max-size" mapstructure:"max-size"`
	// MaxAge 是根据文件名中编码的时间戳保留旧日志文件的最大天数,默认为30天。
	MaxAge int `json:"max-age" mapstructure:"max-age"`
	// MaxBackups 是要保留的旧日志文件的最大数量。默认是保留所有旧的日志文件(尽管 MaxAge 可能仍会导致它们被删除。)
	MaxBackups int `json:"max-backups" mapstructure:"max-backups"`
}

type LoggerHelper

type LoggerHelper interface {
	Debug(msg string)
	DebugF(format string, v ...interface{})
	DebugContext(ctx context.Context, msg string)
	DebugContextF(ctx context.Context, format string, v ...interface{})
	Info(msg string)
	InfoF(format string, v ...interface{})
	InfoContext(ctx context.Context, msg string)
	InfoContextF(ctx context.Context, format string, v ...interface{})
	Warn(msg string)
	WarnF(format string, v ...interface{})
	WarnContext(ctx context.Context, msg string)
	WarnContextF(ctx context.Context, format string, v ...interface{})
	Error(msg string)
	ErrorF(format string, v ...interface{})
	ErrorContext(ctx context.Context, msg string)
	ErrorContextF(ctx context.Context, format string, v ...interface{})
}

LoggerHelper 打印日志的顶层接口,有Context传入的参数,会根据配置将日志发送到远程分析程序

func GetLogger

func GetLogger() LoggerHelper

func Init

func Init(opts *Options, zapOptions ...ZapLoggerOption) LoggerHelper

type NameLog

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

func NewNameLog

func NewNameLog(log LoggerHelper, name string) *NameLog

func NewNameLogWithDefaultLog

func NewNameLogWithDefaultLog(name string) *NameLog

func (*NameLog) Debug

func (n *NameLog) Debug(msg string)

func (*NameLog) DebugContext

func (n *NameLog) DebugContext(ctx context.Context, msg string)

func (*NameLog) DebugContextF

func (n *NameLog) DebugContextF(ctx context.Context, format string, v ...interface{})

func (*NameLog) DebugF

func (n *NameLog) DebugF(format string, v ...interface{})

func (*NameLog) Error

func (n *NameLog) Error(msg string)

func (*NameLog) ErrorContext

func (n *NameLog) ErrorContext(ctx context.Context, msg string)

func (*NameLog) ErrorContextF

func (n *NameLog) ErrorContextF(ctx context.Context, format string, v ...interface{})

func (*NameLog) ErrorF

func (n *NameLog) ErrorF(format string, v ...interface{})

func (*NameLog) Info

func (n *NameLog) Info(msg string)

func (*NameLog) InfoContext

func (n *NameLog) InfoContext(ctx context.Context, msg string)

func (*NameLog) InfoContextF

func (n *NameLog) InfoContextF(ctx context.Context, format string, v ...interface{})

func (*NameLog) InfoF

func (n *NameLog) InfoF(format string, v ...interface{})

func (*NameLog) Warn

func (n *NameLog) Warn(msg string)

func (*NameLog) WarnContext

func (n *NameLog) WarnContext(ctx context.Context, msg string)

func (*NameLog) WarnContextF

func (n *NameLog) WarnContextF(ctx context.Context, format string, v ...interface{})

func (*NameLog) WarnF

func (n *NameLog) WarnF(format string, v ...interface{})

type Option

type Option func(*Options)

func WithColor

func WithColor(on bool) Option

func WithConsoleFormat

func WithConsoleFormat() Option

func WithDebugLevel

func WithDebugLevel() Option

func WithFileSegment

func WithFileSegment(on bool) Option

func WithFileSegmentOption

func WithFileSegmentOption(maxSize, maxAge, maxBackups int) Option

func WithInfoLevel

func WithInfoLevel() Option

func WithJsonFormat

func WithJsonFormat() Option

func WithOutPutPath

func WithOutPutPath(outPutPath, errorOutPutPath []string) Option

type Options

type Options struct {
	// 日志级别,选项(debug,info,warn,error,dpanic,panic,fatal)
	Level string `json:"level"              mapstructure:"level"`
	// json的格式,选项(console,json)
	Format string `json:"format"             mapstructure:"format"`
	// 停止使用调用函数的文件名和行号注释日志, 在默认情况下 所有日志都有注释
	DisableCaller bool `json:"disable-caller"     mapstructure:"disable-caller"`
	// 禁用自动捕获堆栈跟踪。默认情况下,在 dev 环境中捕获WarnLevel及以上级别的日志,在 pro 环境中捕获ErrorLevel及以上级别的日志
	DisableStacktrace bool `json:"disable-stacktrace" mapstructure:"disable-stacktrace"`
	EnableColor       bool `json:"enable-color"       mapstructure:"enable-color"`
	// 是否是开发模式
	Development bool `json:"development"        mapstructure:"development"`
	// 日志名称
	Name string `json:"name"               mapstructure:"name"`
	//是否开启traceID
	EnableTraceID bool `json:"enable-trace-id"     mapstructure:"enable-trace-id"`
	//是否开启traceStack
	EnableTraceStack bool `json:"enable-trace-stack" mapstructure:"enable-trace-stack"`
	// 输出 默认输出到标准输出,如果不是标准输出,则认为是文件输出
	OutputPaths []string `json:"output-paths"       mapstructure:"output-paths"`
	// 错误输出 默认输出到标准错误
	ErrorOutputPaths []string `json:"error-output-paths" mapstructure:"error-output-paths"`

	//是否开启文件分割,默认不分割
	EnableFileSegment bool `json:"enable-file-segment"     mapstructure:"enable-file-segment"`

	// 文件分割策略
	FileSegment FileSegmentOptions `json:"file-segment" mapstructure:"file-segment"`
}

Options 日志的配置类,推荐使用NewOptions方法创建默认的配置,然后再根据配置文件进行填充个性化配置

func NewOptions

func NewOptions(opts ...Option) *Options

func (*Options) AddFlags

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

func (*Options) Validate

func (o *Options) Validate() error

type ZapLogger

type ZapLogger struct {
	*zap.Logger
	// contains filtered or unexported fields
}

ZapLogger log的zap实现类,推荐调用NewZapLog方法创建

func NewZapLog

func NewZapLog(opts *Options, zapOptions ...ZapLoggerOption) *ZapLogger

func (*ZapLogger) Debug

func (l *ZapLogger) Debug(msg string)

func (*ZapLogger) DebugContext

func (l *ZapLogger) DebugContext(ctx context.Context, msg string)

func (*ZapLogger) DebugContextF

func (l *ZapLogger) DebugContextF(ctx context.Context, format string, v ...interface{})

func (*ZapLogger) DebugF

func (l *ZapLogger) DebugF(format string, v ...interface{})

func (*ZapLogger) Error

func (l *ZapLogger) Error(msg string)

func (*ZapLogger) ErrorContext

func (l *ZapLogger) ErrorContext(ctx context.Context, msg string)

func (*ZapLogger) ErrorContextF

func (l *ZapLogger) ErrorContextF(ctx context.Context, format string, v ...interface{})

func (*ZapLogger) ErrorF

func (l *ZapLogger) ErrorF(format string, v ...interface{})

func (*ZapLogger) Flush

func (l *ZapLogger) Flush()

func (*ZapLogger) Info

func (l *ZapLogger) Info(msg string)

func (*ZapLogger) InfoContext

func (l *ZapLogger) InfoContext(ctx context.Context, msg string)

func (*ZapLogger) InfoContextF

func (l *ZapLogger) InfoContextF(ctx context.Context, format string, v ...interface{})

func (*ZapLogger) InfoF

func (l *ZapLogger) InfoF(format string, v ...interface{})

func (*ZapLogger) Warn

func (l *ZapLogger) Warn(msg string)

func (*ZapLogger) WarnContext

func (l *ZapLogger) WarnContext(ctx context.Context, msg string)

func (*ZapLogger) WarnContextF

func (l *ZapLogger) WarnContextF(ctx context.Context, format string, v ...interface{})

func (*ZapLogger) WarnF

func (l *ZapLogger) WarnF(format string, v ...interface{})

func (*ZapLogger) WithOptions

func (l *ZapLogger) WithOptions(opts ...zap.Option) *ZapLogger

WithOptions clones the current Logger, applies the supplied Options, and returns the resulting Logger. It's safe to use concurrently.

type ZapLoggerOption

type ZapLoggerOption func(*ZapLogger)

func WithCaller

func WithCaller(on bool) ZapLoggerOption

func WithStackTrace

func WithStackTrace(on bool) ZapLoggerOption

func WithSugar

func WithSugar(on bool) ZapLoggerOption

Jump to

Keyboard shortcuts

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