internal

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 5, 2025 License: MIT Imports: 22 Imported by: 0

Documentation

Index

Constants

View Source
const (
	TraceLevel uint32 = iota
	// DebugLevel logs everything
	DebugLevel
	// InfoLevel includes info, warnings,errors
	InfoLevel
	// WarnLevel includes warnings,errors
	WarnLevel
	// ErrorLevel includes errors
	ErrorLevel
	// DisableLevel doesn't log any messages
	DisableLevel = 0xff

	LevelInfo    = "INF"
	LevelError   = "ERR"
	LevelWarn    = "WAR"
	LevelDebug   = "DEB"
	LevelTrace   = "TRA"
	LevelDisable = "OFF"
)

Variables

View Source
var (
	ErrLogPathNotSet        = errors.New("log path must be set")
	ErrLogServiceNameNotSet = errors.New("log service name must be set")
)
View Source
var (
	ErrLogFileClosed = errors.New("error: log file closed")
)

Functions

func AddWriter

func AddWriter(w Writer)

AddWriter 添加一个新的日志写入器 如果已经存在写入器,新的写入器将被添加到写入器链中 例如,要同时将日志写入文件和控制台,如果已经存在文件写入器: qlog.AddWriter(qlog.NewWriter(os.Stdout))

func Close

func Close() error

Close 关闭日志系统

func CloseOnExec

func CloseOnExec(file *os.File)

CloseOnExec makes sure closing the file on process forking.

func Debug

func Debug(v ...any)

Debug 将参数写入调试日志

func Debugf

func Debugf(format string, v ...any)

Debugf 将参数写入调试日志

func Error

func Error(v ...any)

Error 将参数写入错误日志

func Errorf

func Errorf(format string, v ...any)

Errorf 将参数写入错误日志

func GetDirOnDiskFreeSize

func GetDirOnDiskFreeSize(path string) (int64, error)

GetDirOnDiskFreeSize 返回给定目录所在磁盘的可用空间大小(单位:字节)

func GetDirOnDiskTotalSize

func GetDirOnDiskTotalSize(path string) (int64, error)

GetDirOnDiskTotalSize 返回给定目录所在磁盘的总大小(单位:字节)

func GetLevel

func GetLevel() uint32

GetLevel 获取日志级别

func GetOutputStringFormatted

func GetOutputStringFormatted(level string, val any) string

func Info

func Info(v ...any)

Info 将参数写入访问日志

func Infof

func Infof(format string, v ...any)

Infof 将参数写入访问日志

func ReprOfDuration

func ReprOfDuration(duration time.Duration) string

ReprOfDuration returns the string representation of given duration in ms.

func SetLevel

func SetLevel(level uint32)

SetLevel 设置日志级别,可用于抑制某些日志的输出

func SetUp

func SetUp(c LogConf) (err error)

SetUp 初始化日志系统 如果已经初始化过,返回 nil

func SetWriter

func SetWriter(w Writer)

SetWriter 设置日志写入器,可用于自定义日志配置

func Trace

func Trace(v ...any)

Trace 将参数写入调试日志

func Tracef

func Tracef(format string, v ...any)

Tracef 将参数写入调试日志

func Warn

func Warn(v ...any)

Warn 将参数写入警告日志

func Warnf

func Warnf(format string, v ...any)

Warnf 将参数写入警告日志

Types

type BatchError

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

BatchError 是一个可以存储多个错误的错误类型

func (*BatchError) Add

func (be *BatchError) Add(errs ...error)

Add 添加一个或多个非空错误到 BatchError 实例中

func (*BatchError) Err

func (be *BatchError) Err() error

Err 返回一个代表所有累积错误的错误对象 如果没有错误则返回 nil

func (*BatchError) NotNil

func (be *BatchError) NotNil() bool

NotNil 检查 BatchError 中是否至少存在一个错误

type DailyRotateRule

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

DailyRotateRule 是一个按天轮转日志文件的规则

func (*DailyRotateRule) BackupFileName

func (r *DailyRotateRule) BackupFileName() string

BackupFileName 返回轮转时的备份文件名

func (*DailyRotateRule) FilePathPattern

func (r *DailyRotateRule) FilePathPattern() string

func (*DailyRotateRule) MarkRotated

func (r *DailyRotateRule) MarkRotated()

MarkRotated 将轮转时间标记为当前时间

func (*DailyRotateRule) OutdatedFiles

func (r *DailyRotateRule) OutdatedFiles() []string

OutdatedFiles 返回超过保留天数的文件列表

func (*DailyRotateRule) ShallRotate

func (r *DailyRotateRule) ShallRotate(_ int64) bool

ShallRotate 检查文件是否应该进行轮转

type HealthChecker

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

HealthChecker 负责日志系统的健康检查和恢复

func NewHealthChecker

func NewHealthChecker(logger *RotateLogger) *HealthChecker

NewHealthChecker 创建新的健康检查器

func (*HealthChecker) ReportError

func (h *HealthChecker) ReportError(err error)

ReportError 报告错误

func (*HealthChecker) Start

func (h *HealthChecker) Start()

Start 启动健康检查器

func (*HealthChecker) WaitRecover

func (h *HealthChecker) WaitRecover() bool

WaitRecover 等待恢复信号

type LogConf

type LogConf struct {
	// ServiceName 表示服务名称
	ServiceName string `json:",optional"`
	// Mode 表示日志模式,默认为 `console`
	// console: 输出到控制台
	// file: 输出到文件
	Mode string `json:",default=console,options=[console,file]"`
	// ServerLogDir 表示服务日志目录路径,默认为 `logs`
	ServerLogDir string `json:",default=logs"`
	// ManagerLogDir 表示管理日志目录路径,默认为 `logs`
	ManagerLogDir string `json:",default=logs"`
	// Level 表示日志级别,默认为 `ERR`
	Level string `json:",default=ERR,options=[DEB,INF,WAR,ERR]"`
	// MaxContentLength 表示最大内容字节数,默认无限制
	MaxContentLength uint32 `json:",optional"`
	// Compress 表示是否压缩日志文件,默认为 `false`
	Compress bool `json:",optional"`
	// KeepDays 表示日志文件保留天数,默认保留所有文件
	// 仅在 Mode 为 `file` 时生效,对 Rotation 为 `daily` 或 `size` 都有效
	KeepDays int `json:",optional"`
	// MaxBackups 表示要保留的备份日志文件数量,0表示永久保留所有文件
	// 仅在 RotationRuleType 为 `size` 时生效
	// 即使 MaxBackups 设置为0,如果达到 KeepDays 限制,日志文件仍会被删除
	MaxBackups int `json:",default=0"`
	// MaxSize 表示正在写入的日志文件可占用的最大空间,0表示无限制,单位为MB
	// 仅在 RotationRuleType 为 `size` 时生效
	MaxSize int `json:",default=0"`
	// Rotation 表示日志轮转规则类型,默认为 `daily`
	// daily: 按天轮转
	// size: 按大小轮转
	Rotation string `json:",default=daily,options=[daily,size]"`
	// colorConsole 表示是否在控制台输出彩色日志,默认为 `false`
	ColorConsole bool `json:",default=false"`
}

LogConf 是日志配置结构体

type LogOption

type LogOption func(options *logOptions)

LogOption 定义了自定义日志配置的方法

func WithGzip

func WithGzip() LogOption

WithGzip 自定义日志文件自动使用 gzip 压缩

func WithKeepDays

func WithKeepDays(days int) LogOption

WithKeepDays 自定义日志保留天数

func WithMaxBackups

func WithMaxBackups(count int) LogOption

WithMaxBackups 自定义保留的日志文件备份数量

func WithMaxSize

func WithMaxSize(size int) LogOption

WithMaxSize 自定义单个日志文件的最大大小(MB)

func WithRotation

func WithRotation(r string) LogOption

WithRotation 自定义使用的日志轮转规则

type Logger

type Logger interface {
	// Printf logs a message with timestamp but without level and module.
	Printf(string, ...any)
	// Print logs a message with timestamp but without level and module.
	Print(...any)

	// Trace logs a message at trace level.
	Trace(...any)
	// Tracef logs a message at trace level.
	Tracef(string, ...any)
	// Debug logs a message at debug level.
	Debug(...any)
	// Debugf logs a message at debug level.
	Debugf(string, ...any)
	// Warn logs a message at warn level.
	Warn(...any)
	// Warnf logs a message at warn level.
	Warnf(string, ...any)
	// Info logs a message at info level.
	Info(...any)
	// Infof logs a message at info level.
	Infof(string, ...any)
	// Error logs a message at error level.
	Error(...any)
	// Errorf logs a message at error level.
	Errorf(string, ...any)

	// WriteRawString writes a raw message with module.
	WriteRawString(string)

	// WithTraceId returns a new logger with trace id.
	WithTraceId(string) Logger
}

A Logger represents a logger.

func WithModuleName

func WithModuleName(moduleName string) Logger

type PlaceholderType

type PlaceholderType = struct{}
var Placeholder PlaceholderType

type RotateLogger

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

RotateLogger 是一个可以按照给定规则轮转日志文件的日志器

func NewLogger

func NewLogger(filename string, rule RotateRule, compress bool) (*RotateLogger, error)

NewLogger 返回一个 RotateLogger 实例,给定文件名和规则等

func (*RotateLogger) Close

func (l *RotateLogger) Close() error

Close 关闭 RotateLogger

func (*RotateLogger) Write

func (l *RotateLogger) Write(data []byte) (int, error)

type RotateRule

type RotateRule interface {
	BackupFileName() string
	MarkRotated()
	OutdatedFiles() []string
	ShallRotate(size int64) bool
	FilePathPattern() string // 返回文件名pattern
}

RotateRule 接口用于定义日志轮转规则

func DefaultRotateRule

func DefaultRotateRule(filename, delimiter string, days int, gzip bool) RotateRule

DefaultRotateRule 返回默认的日志轮转规则,目前是 DailyRotateRule

func NewSizeLimitRotateRule

func NewSizeLimitRotateRule(filename, delimiter string, days, maxSize, maxBackups int, gzip bool) RotateRule

NewSizeLimitRotateRule 返回一个基于大小限制的轮转规则

type SizeLimitRotateRule

type SizeLimitRotateRule struct {
	DailyRotateRule
	// contains filtered or unexported fields
}

SizeLimitRotateRule 是一个基于文件大小的日志轮转规则

func (*SizeLimitRotateRule) BackupFileName

func (r *SizeLimitRotateRule) BackupFileName() string

func (*SizeLimitRotateRule) FilePathPattern

func (r *SizeLimitRotateRule) FilePathPattern() string

func (*SizeLimitRotateRule) MarkRotated

func (r *SizeLimitRotateRule) MarkRotated()

func (*SizeLimitRotateRule) OutdatedFiles

func (r *SizeLimitRotateRule) OutdatedFiles() []string

func (*SizeLimitRotateRule) ShallRotate

func (r *SizeLimitRotateRule) ShallRotate(size int64) bool

type Writer

type Writer interface {
	Close() error
	Trace(v any)
	Debug(v any)
	Warn(v any)
	Error(v any)
	Info(v any)
	AccessRecord(v any)
	WriteRawString(v string)
}

func NewWriter

func NewWriter(w io.Writer) Writer

NewWriter creates a new Writer with the given io.Writer.

func Reset

func Reset() Writer

Reset 清除写入器并重置日志级别

Jump to

Keyboard shortcuts

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