logger

package
v0.0.0-...-72a72ae Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2026 License: GPL-3.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

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

Debug 记录 debug 级别日志

func Error

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

Error 记录 error 级别日志

func Fatal

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

Fatal 记录 fatal 级别日志并退出程序

func Info

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

Info 记录 info 级别日志

func Initialize

func Initialize(config Config) error

Initialize 初始化全局日志实例

func Sync

func Sync() error

Sync 刷新缓冲区

func Warn

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

Warn 记录 warn 级别日志

Types

type Config

type Config struct {
	Type         LoggerType // 日志驱动类型: zap 或 zerolog
	Level        Level      // 日志级别
	Output       OutputType // 输出类型: stdout, file, both
	File         FileConfig // 文件输出配置
	EnableCaller bool       // 是否显示调用者信息
	JSONFormat   bool       // 是否使用 JSON 格式
}

Config 日志配置

type Field

type Field struct {
	Key   string
	Value interface{}
}

Field 日志字段

func Any

func Any(key string, value interface{}) Field

func Bool

func Bool(key string, value bool) Field

func Err

func Err(err error) Field

func Float64

func Float64(key string, value float64) Field

func Int

func Int(key string, value int) Field

func Int64

func Int64(key string, value int64) Field

func String

func String(key, value string) Field

字段构造函数

type FileConfig

type FileConfig struct {
	Filename   string // 日志文件路径
	MaxSize    int    // 单个文件最大大小(MB)
	MaxBackups int    // 保留的旧文件最大数量
	MaxAge     int    // 保留旧文件的最大天数
	Compress   bool   // 是否压缩旧文件
}

FileConfig 文件输出配置

type GormLogger

type GormLogger struct {
	LogLevel                  gormlogger.LogLevel
	SlowThreshold             time.Duration
	IgnoreRecordNotFoundError bool
	// contains filtered or unexported fields
}

GormLogger GORM 日志适配器

func NewGormLogger

func NewGormLogger(logger Logger) *GormLogger

NewGormLogger 创建 GORM 日志适配器

func (*GormLogger) Error

func (l *GormLogger) Error(ctx context.Context, msg string, data ...interface{})

Error 记录 error 级别日志

func (*GormLogger) Info

func (l *GormLogger) Info(ctx context.Context, msg string, data ...interface{})

Info 记录 info 级别日志

func (*GormLogger) LogMode

LogMode 设置日志级别

func (*GormLogger) Trace

func (l *GormLogger) Trace(ctx context.Context, begin time.Time, fc func() (sql string, rowsAffected int64), err error)

Trace 记录 SQL 执行日志

func (*GormLogger) Warn

func (l *GormLogger) Warn(ctx context.Context, msg string, data ...interface{})

Warn 记录 warn 级别日志

type Level

type Level string

Level 日志级别

const (
	LevelDebug Level = "debug"
	LevelInfo  Level = "info"
	LevelWarn  Level = "warn"
	LevelError Level = "error"
	LevelFatal Level = "fatal"
)

type Logger

type Logger interface {
	Debug(msg string, fields ...Field)
	Info(msg string, fields ...Field)
	Warn(msg string, fields ...Field)
	Error(msg string, fields ...Field)
	Fatal(msg string, fields ...Field)

	With(fields ...Field) Logger // 添加字段
	Named(name string) Logger    // 添加命名空间
	Sync() error                 // 刷新缓冲区
}

Logger 统一日志接口

func Default

func Default() Logger

Default 获取默认日志实例

func Named

func Named(name string) Logger

Named 添加命名空间

func With

func With(fields ...Field) Logger

With 添加字段

type LoggerType

type LoggerType string

LoggerType 日志驱动类型

const (
	LoggerTypeZap     LoggerType = "zap"
	LoggerTypeZerolog LoggerType = "zerolog"
)

type OutputType

type OutputType string

OutputType 输出类型

const (
	OutputStdout OutputType = "stdout"
	OutputFile   OutputType = "file"
	OutputBoth   OutputType = "both"
)

Jump to

Keyboard shortcuts

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