slog

package
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Dec 24, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// DefaultLogFile 默认日志文件路径
	DefaultLogFile = "app.log"
	// DefaultLogDir 默认日志目录
	DefaultLogDir = "logs"
)

默认日志文件路径配置

Functions

func CleanupLogFile

func CleanupLogFile(filepath string) error

CleanupLogFile 清理指定的日志文件

func CleanupLogFiles

func CleanupLogFiles() error

CleanupLogFiles 清理日志文件(主要用于测试)

func Debug

func Debug(msg string, fields ...interface{})

func Debugf

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

func EnsureLogDir

func EnsureLogDir() error

EnsureLogDir 确保日志目录存在

func EnsureLogDirForPath

func EnsureLogDirForPath(logPath string) error

EnsureLogDirForPath 确保指定路径的日志目录存在

func Error

func Error(msg string, fields ...interface{})

func Errorf

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

func Fatal

func Fatal(msg string, fields ...interface{})

func Fatalf

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

func GetDefaultLogDir

func GetDefaultLogDir() string

GetDefaultLogDir 获取默认日志目录

func GetDefaultLogFile

func GetDefaultLogFile() string

GetDefaultLogFile 获取默认日志文件路径

func GetDefaultLogPath

func GetDefaultLogPath() string

GetDefaultLogPath 获取完整的默认日志路径

func Info

func Info(msg string, fields ...interface{})

func Infof

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

func InitSlog

func InitSlog(opts Options)

InitSlog 使用指定选项初始化全局 logger 并替换 slog.Default

func Panic

func Panic(msg string, fields ...interface{})

func Panicf

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

func SetContextExtractor

func SetContextExtractor(extractor ContextExtractor)

SetContextExtractor 设置全局上下文提取器

func SetDefaultLogDir

func SetDefaultLogDir(dir string)

SetDefaultLogDir 设置默认日志目录

func SetDefaultLogFile

func SetDefaultLogFile(filepath string)

SetDefaultLogFile 设置默认日志文件路径

func SetDefaultLogger

func SetDefaultLogger(logger *Logger)

SetDefaultLogger 设置全局 logger

func SetLevel

func SetLevel(level Level)

全局函数

func SetupSlogWithOptions

func SetupSlogWithOptions(opts Options)

SetupSlogWithOptions 设置全局 logger 并替换 slog.Default

func Sync

func Sync() error

func Warn

func Warn(msg string, fields ...interface{})

func Warnf

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

Types

type ContextExtractor

type ContextExtractor interface {
	Extract(ctx context.Context) map[string]interface{}
}

ContextExtractor 上下文信息提取器

func GetContextExtractor

func GetContextExtractor() ContextExtractor

GetContextExtractor 获取全局上下文提取器

type ContextKey

type ContextKey string

ContextKey 定义context key类型以避免冲突

type DefaultContextExtractor

type DefaultContextExtractor struct{}

DefaultContextExtractor 默认上下文提取器

func (*DefaultContextExtractor) Extract

func (d *DefaultContextExtractor) Extract(ctx context.Context) map[string]interface{}

Extract 从context中提取信息

type Entry

type Entry struct {
	Level      slog.Level
	Time       time.Time
	Message    string
	Attributes []slog.Attr
}

Entry slog Hook 数据

type Format

type Format string

Format 日志格式类型

const (
	// FormatJSON JSON格式输出
	FormatJSON Format = "json"
	// FormatConsole 控制台格式输出(带颜色)
	FormatConsole Format = "console"
	// FormatText 文本格式输出(不带颜色)
	FormatText Format = "text"
)

func ParseFormat

func ParseFormat(format string) Format

ParseFormat 解析日志格式

func (Format) String

func (f Format) String() string

String 返回格式字符串

type Hook

type Hook func(entry Entry) error

Hook 日志钩子函数

type Level

type Level int8

Level 日志级别

const (
	DebugLevel Level = iota - 1
	InfoLevel
	WarnLevel
	ErrorLevel
	FatalLevel
)

func GetLevel

func GetLevel() Level

func ParseLevel

func ParseLevel(level string) Level

ParseLevel 解析日志级别

func (Level) String

func (l Level) String() string

String 返回日志级别字符串

type Logger

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

Logger 日志管理器

func FromContext

func FromContext(ctx context.Context) *Logger

func GetDefaultLogger

func GetDefaultLogger() *Logger

GetDefaultLogger 获取全局 logger

func Named

func Named(name string) *Logger

func New

func New() *Logger

New 创建默认 slog Logger

func NewWithOptions

func NewWithOptions(opts Options) *Logger

NewWithOptions 根据选项创建 slog Logger

func With

func With(fields ...interface{}) *Logger

func WithError

func WithError(err error) *Logger

func WithFields

func WithFields(fields map[string]interface{}) *Logger

func (*Logger) AddHook

func (l *Logger) AddHook(hook Hook)

AddHook 添加钩子函数

func (*Logger) Clone

func (l *Logger) Clone() *Logger

Clone 克隆日志记录器

func (*Logger) Debug

func (l *Logger) Debug(msg string, fields ...interface{})

Debug 输出调试日志

func (*Logger) Debugf

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

Debugf 输出格式化调试日志

func (*Logger) Error

func (l *Logger) Error(msg string, fields ...interface{})

Error 输出错误日志

func (*Logger) Errorf

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

Errorf 输出格式化错误日志

func (*Logger) Fatal

func (l *Logger) Fatal(msg string, fields ...interface{})

Fatal 输出致命错误日志并退出

func (*Logger) Fatalf

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

Fatalf 输出格式化致命错误日志并退出

func (*Logger) GetLevel

func (l *Logger) GetLevel() Level

GetLevel 获取日志级别

func (*Logger) GetSlog

func (l *Logger) GetSlog() *slog.Logger

GetSlog 获取底层 slog.Logger

func (*Logger) Info

func (l *Logger) Info(msg string, fields ...interface{})

Info 输出信息日志

func (*Logger) Infof

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

Infof 输出格式化信息日志

func (*Logger) IsEnabled

func (l *Logger) IsEnabled(level Level) bool

IsEnabled 检查日志级别是否启用

func (*Logger) Named

func (l *Logger) Named(name string) *Logger

Named 创建命名的日志记录器

func (*Logger) Panic

func (l *Logger) Panic(msg string, fields ...interface{})

Panic 输出panic日志并panic

func (*Logger) Panicf

func (l *Logger) Panicf(format string, args ...interface{})

Panicf 输出格式化panic日志并panic

func (*Logger) RemoveHooks

func (l *Logger) RemoveHooks()

RemoveHooks 移除所有钩子函数

func (*Logger) SetLevel

func (l *Logger) SetLevel(level Level)

SetLevel 设置日志级别

func (*Logger) Sync

func (l *Logger) Sync() error

Sync 同步日志缓冲区(对 slog 无操作)

func (*Logger) Warn

func (l *Logger) Warn(msg string, fields ...interface{})

Warn 输出警告日志

func (*Logger) Warnf

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

Warnf 输出格式化警告日志

func (*Logger) With

func (l *Logger) With(fields ...interface{}) *Logger

With 创建带字段的日志记录器

func (*Logger) WithContext

func (l *Logger) WithContext(ctx context.Context) *Logger

WithContext 创建带上下文的日志记录器

func (*Logger) WithError

func (l *Logger) WithError(err error) *Logger

WithError 创建带错误字段的日志记录器

func (*Logger) WithFields

func (l *Logger) WithFields(fields map[string]interface{}) *Logger

WithFields 创建带字段的日志记录器

type Options

type Options struct {
	Level            Level                  // 日志级别
	Format           Format                 // 输出格式 (FormatJSON, FormatConsole, FormatText)
	TimeFormat       string                 // 时间格式
	Caller           bool                   // 是否显示调用者信息
	Stacktrace       bool                   // 是否显示堆栈跟踪
	EnableFileOutput bool                   // 是否启用文件输出
	Color            bool                   // 是否启用彩色输出(仅控制台/文本格式)
	Sampling         *SamplingConfig        // 采样配置
	Rotate           *RotateConfig          // 日志轮转配置
	Fields           map[string]interface{} // 默认字段
	Hooks            []Hook                 // 钩子函数
}

Options 日志选项

type RotateConfig

type RotateConfig struct {
	Filename   string // 日志文件名
	MaxSize    int    // 最大文件大小(MB)
	MaxBackups int    // 最大备份数量
	MaxAge     int    // 最大保留天数
	Compress   bool   // 是否压缩
	LocalTime  bool   // 是否使用本地时间
}

RotateConfig 日志轮转配置

type SamplingConfig

type SamplingConfig struct {
	Initial    int           // 初始采样数量
	Thereafter int           // 后续采样数量
	Tick       time.Duration // 采样周期
}

SamplingConfig 采样配置

Jump to

Keyboard shortcuts

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