Documentation
¶
Index ¶
- Constants
- Variables
- func AddWriter(w Writer)
- func Close() error
- func CloseOnExec(file *os.File)
- func Debug(v ...any)
- func Debugf(format string, v ...any)
- func Error(v ...any)
- func Errorf(format string, v ...any)
- func GetDirOnDiskFreeSize(path string) (int64, error)
- func GetDirOnDiskTotalSize(path string) (int64, error)
- func GetLevel() uint32
- func GetOutputStringFormatted(level string, val any) string
- func Info(v ...any)
- func Infof(format string, v ...any)
- func ReprOfDuration(duration time.Duration) string
- func SetLevel(level uint32)
- func SetUp(c LogConf) (err error)
- func SetWriter(w Writer)
- func Trace(v ...any)
- func Tracef(format string, v ...any)
- func Warn(v ...any)
- func Warnf(format string, v ...any)
- type BatchError
- type DailyRotateRule
- type HealthChecker
- type LogConf
- type LogOption
- type Logger
- type PlaceholderType
- type RotateLogger
- type RotateRule
- type SizeLimitRotateRule
- type Writer
Constants ¶
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 ¶
var ( ErrLogPathNotSet = errors.New("log path must be set") ErrLogServiceNameNotSet = errors.New("log service name must be set") )
var (
ErrLogFileClosed = errors.New("error: log file closed")
)
Functions ¶
func AddWriter ¶
func AddWriter(w Writer)
AddWriter 添加一个新的日志写入器 如果已经存在写入器,新的写入器将被添加到写入器链中 例如,要同时将日志写入文件和控制台,如果已经存在文件写入器: qlog.AddWriter(qlog.NewWriter(os.Stdout))
func CloseOnExec ¶
CloseOnExec makes sure closing the file on process forking.
func GetDirOnDiskFreeSize ¶
GetDirOnDiskFreeSize 返回给定目录所在磁盘的可用空间大小(单位:字节)
func GetDirOnDiskTotalSize ¶
GetDirOnDiskTotalSize 返回给定目录所在磁盘的总大小(单位:字节)
func ReprOfDuration ¶
ReprOfDuration returns the string representation of given duration in ms.
Types ¶
type BatchError ¶
type BatchError struct {
// contains filtered or unexported fields
}
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 创建新的健康检查器
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 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 ¶
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 实例,给定文件名和规则等
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