log

package
v0.0.0-...-533a6ae Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2024 License: Apache-2.0 Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	OutputTypeConsole = "console" // 日志输出类型:console
	OutputTypeFile    = "file"    // 日志输出类型:文件
)

Variables

This section is empty.

Functions

func Debug

func Debug(msg string)

Debug 打印Debug等级的日志

func DebugContext

func DebugContext(ctx context.Context, msg string)

DebugContext 打印Debug等级的日志,优先使用ctx中的logger

func DebugContextf

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

DebugContextf 使用fmt.Sprintf打印Debug等级的日志,优先使用ctx中的logger

func Debugf

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

Debugf 使用fmt.Sprintf打印Debug等级的日志

func Error

func Error(msg string)

Error 打印Error等级的日志

func ErrorContext

func ErrorContext(ctx context.Context, msg string)

ErrorContext 打印Error等级的日志,优先使用ctx中的logger

func ErrorContextf

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

ErrorContextf 使用fmt.Sprintf打印Error等级的日志,优先使用ctx中的logger

func Errorf

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

Errorf 使用fmt.Sprintf打印Error等级的日志

func Fatal

func Fatal(msg string)

Fatal 打印Fatal等级的日志

func FatalContext

func FatalContext(ctx context.Context, msg string)

FatalContext 打印Fatal等级的日志,优先使用ctx中的logger

func FatalContextf

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

FatalContextf 使用fmt.Sprintf打印Fatal等级的日志,优先使用ctx中的logger

func Fatalf

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

Fatalf 使用fmt.Sprintf打印Fatal等级的日志

func Info

func Info(msg string)

Info 打印Info等级的日志

func InfoContext

func InfoContext(ctx context.Context, msg string)

InfoContext 打印Info等级的日志,优先使用ctx中的logger

func InfoContextf

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

InfoContextf 使用fmt.Sprintf打印Info等级的日志,优先使用ctx中的logger

func Infof

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

Infof 使用fmt.Sprintf打印Info等级的日志

func NewDefaultLogger

func NewDefaultLogger(configs ...*Config) error

NewDefaultLogger 使用新的配置覆盖默认日志器

func ToContext

func ToContext(ctx context.Context, logger Logger) context.Context

ToContext 向ctx中放入日志器

func Warn

func Warn(msg string)

Warn 打印Warn等级的日志

func WarnContext

func WarnContext(ctx context.Context, msg string)

WarnContext 打印Warn等级的日志,优先使用ctx中的logger

func WarnContextf

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

WarnContextf 使用fmt.Sprintf打印Warn等级的日志,优先使用ctx中的logger

func Warnf

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

Warnf 使用fmt.Sprintf打印Warn等级的日志

Types

type Config

type Config struct {
	Type  string // 日志类型:console/file
	Level string // 日志等级:debug/info/warn/error/fatal

	// 以下配置仅在类型为file时生效,核心逻辑为按照文件大小切割文件
	Path       string // 文件目录路径
	Filename   string // 文件名称
	MaxSize    int64  // 文件最大大小,单位MB
	MaxAge     int64  // 文件最大保存时间,单位天
	MaxBackups int64  // 文件最大保存数量
	Compress   bool   // 是否压缩文件
}

Config 日志配置

type Fields

type Fields map[string]interface{}

Fields 用户定义的字段

type Logger

type Logger interface {
	// Debug 打印Debug等级的日志
	Debug(msg string)
	// Info 打印Info等级的日志
	Info(msg string)
	// Warn 打印Warn等级的日志
	Warn(msg string)
	// Error 打印Error等级的日志
	Error(msg string)
	// Fatal 打印Fatal等级的日志
	Fatal(msg string)

	// Debugf 使用fmt.Sprintf打印Debug等级的日志
	Debugf(template string, args ...interface{})
	// Infof 使用fmt.Sprintf打印Info等级的日志
	Infof(template string, args ...interface{})
	// Warnf 使用fmt.Sprintf打印Warn等级的日志
	Warnf(template string, args ...interface{})
	// Errorf 使用fmt.Sprintf打印Error等级的日志
	Errorf(template string, args ...interface{})
	// Fatalf 使用fmt.Sprintf打印Fatal等级的日志
	Fatalf(template string, args ...interface{})

	// WithField 向日志增加一个自定义字段
	WithField(key string, value interface{}) Logger
	// WithFields 向日志增加多个自定义字段
	WithFields(fields Fields) Logger
}

Logger 日志接口

func FromContext

func FromContext(ctx context.Context) Logger

FromContext 从ctx中获取日志器,如果没有则返回默认日志器

func New

func New(configs ...*Config) (Logger, error)

New 创建新的日志器

func NewWithCallerSkip

func NewWithCallerSkip(skip int, configs ...*Config) (Logger, error)

NewWithCallerSkip 创建新的日志器,额外加上skip调用者的个数

func WithContextField

func WithContextField(ctx context.Context, key string, value interface{}) Logger

WithContextField 向ctx中的日志器中增加一个自定义字段,不会修改ctx中的日志器

func WithContextFields

func WithContextFields(ctx context.Context, fields Fields) Logger

WithContextFields 向ctx中的日志器中增加多个自定义字段,不会修改ctx中的日志器

func WithField

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

WithField 向日志增加一个自定义字段

func WithFields

func WithFields(fields Fields) Logger

WithFields 向日志增加多个自定义字段

type Writer

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

Writer 日志文件写入器

func NewWriter

func NewWriter(filename string, maxSize, maxAge, maxBackups int64, compress bool) (*Writer, error)

NewWriter 创建一个写入器

func (*Writer) Close

func (w *Writer) Close() error

Close 关闭当前文件

func (*Writer) Write

func (w *Writer) Write(v []byte) (n int, err error)

Write 向文件中写入内容

Jump to

Keyboard shortcuts

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