Documentation
¶
Index ¶
Constants ¶
const ( CompressTypeUnknown = iota CompressTypeGzip CompressTypeZstd CompressTypeSnappy )
const ( GzipBestSpeed = gzip.BestSpeed GzipBestCompression = gzip.BestCompression GzipDefaultCompression = gzip.DefaultCompression GzipHuffmanOnly = gzip.HuffmanOnly )
Gzip压缩的等级
const ( // DefaultPeriod 默认保存的天数,30天 DefaultPeriod = 30 // DefaultMaxCount 默认保存的最大文件数量,100个 DefaultMaxCount = 100 // DefaultMaxSize 默认单个日志文件保存的最大大小,100MB DefaultMaxSize = 1024 * 1024 * 100 )
const ( ReadOnlyFile os.FileMode = 0o444 // 只读文件 ReadWriteFile os.FileMode = 0o644 // 读写文件 )
文件系统操作权限组
const ( RotateSizeThreshold = 0.8 RotateInterval = time.Millisecond * 100 )
const Layout = "20060102"
Variables ¶
This section is empty.
Functions ¶
Types ¶
type CleanUp ¶
type CleanUp struct {
// contains filtered or unexported fields
}
CleanUp 根据文件最大数量来确定是否执行清理
func NewFileCountCleanUp ¶
func (*CleanUp) ResetInterval ¶
type CompressStrategy ¶
type CompressStrategy interface {
// Compress 执行压缩逻辑
Compress() error
// Reset 重置压缩
Reset(w io.Writer, f *os.File)
}
CompressStrategy 压缩策略,对文件执行压缩操作
type FileInfo ¶
type FileInfo struct {
UpDir string // 父目录
Name string // 文件名称
Date time.Time // 文件时间(年月日)
Sequence int64 // 文件序列号
}
FileInfo 文件信息
type MixStrategy ¶
type MixStrategy struct {
// contains filtered or unexported fields
}
MixStrategy 混合策略包括两个触发因子:定时和当前文件大小。 文件大小的优先级高于定时,当在一个定时的时间窗口周期内,数据写入时文件 的大小超过了最大限制,比如:100M,则立即返回需要执行轮转操作,由调用方 立即执行轮转。当定时任务达到轮转时间后,发送一条轮转事件通知到通知通道, 调用方实时监听信号,收到信号信号后执行轮转操作。每次轮转都需要记录当前轮 转的时间戳,如果是文件大小超过限制而触发的轮转,则无须进行时间判断,立即 进行轮转,如果是定时任务触发的轮转,则需要比较当前时间的时间和上次轮转的 时间的差值是否超过一定的时间范围,比如10分钟,或当前文件的写入大小是否达 到了最大大小的一定比例,比如80%,如果满足其中一条才能进行轮转操作,发送 事件通知,反之则跳过本次定时轮转。
func NewMixStrategy ¶
func NewMixStrategy(maxSize uint64, tp TimingType) (*MixStrategy, error)
func (*MixStrategy) Close ¶
func (s *MixStrategy) Close()
func (*MixStrategy) NotifyRotate ¶
func (s *MixStrategy) NotifyRotate() <-chan struct{}
func (*MixStrategy) ShouldRotate ¶
func (s *MixStrategy) ShouldRotate(writeSize uint64) bool
ShouldRotate 是否需要执行轮转操作
type OnceWithError ¶
type OnceWithError struct {
// contains filtered or unexported fields
}
OnceWithError 用于确保一个初始化函数只执行一次,并保存其返回的错误信息。 结构体包含一个 noCopy 字段防止拷贝,一个 sync.Once 实例保证单次执行,以及一个 error 字段保存执行结果。
func (*OnceWithError) Do ¶
func (o *OnceWithError) Do(f func() error)
Do 方法接收一个无参数、返回 error 的函数 f。 该方法确保 f 只被执行一次,并将其返回值保存在 OnceWithError 实例的 err 字段中。 参数:
f - 初始化函数,返回一个 error。
func (*OnceWithError) Err ¶
func (o *OnceWithError) Err() error
Err 方法用于获取 OnceWithError 实例中保存的错误信息。 返回值:
error - 初始化函数执行后保存的错误信息。
type Option ¶
func WithCompress ¶
WithCompress 开启压缩,压缩算法提供gzip、zstd和snappy三种算法, 当压缩算法为gzip时,可以设置压缩等级/级别,如果不设置,默认压缩级别 为gzip.DefaultCompression
func WithRotate ¶
func WithRotate(maxSize uint64, tp TimingType) Option
WithRotate 设置轮转配置,maxSize设置单个文件写入的最大字节,默认为100MB,当超过 限制后强制立即执行轮转,后台定时轮转的时间类型: Hour:一小时定时执行一次轮转 Day:一天定时执行一次轮转 Week:一周定时执行一次轮转 Month: 一个月定时执行一次轮转 默认定时执行的时间类型是Hour。
type RotateStrategy ¶
type RotateStrategy interface {
// ShouldRotate 写入文件时立即判断否应该执行文件轮转
ShouldRotate(writeSize uint64) bool
// NotifyRotate 获取定时轮转信号
NotifyRotate() <-chan struct{}
// Close 关闭轮转策略
Close()
}
RotateStrategy 文件轮转的策略
type Rotator ¶
type Rotator struct {
// contains filtered or unexported fields
}
Rotator 轮转器入口,执行真正的轮转和写入操作 根据轮转策略确定是否执行轮转,轮转策略包括:根据文件大小、定时以及混合策略, 如果需要轮转,根据新的文件名称执行轮转操作。文件轮转后根据压缩策略确定是否执行压缩操作, 以及根据清除策略确定是否执行清除,过期文件的操作。
func NewRotator ¶
NewRotator 生产环境单例模式
type TimingType ¶
type TimingType string
const ( Hour TimingType = "hour" Day TimingType = "day" Week TimingType = "week" Month TimingType = "month" )
func (TimingType) String ¶
func (t TimingType) String() string
func (TimingType) Valid ¶
func (t TimingType) Valid() bool
