rollingwriter

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 17, 2020 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Index

Constants

View Source
const (
	WithoutRolling = iota
	TimeRolling
	VolumeRolling
)

三种滚动模式

Variables

View Source
var (
	BufferSize      = 0x100000
	QueueSize       = 1024
	Precision       = 1
	DefualtFileMode = os.FileMode(0644)
	DefualtFileFlag = os.O_RDWR | os.O_CREATE | os.O_APPEND

	// 自定义错误
	ErrInternal        = errors.New("error internal")
	ErrClosed          = errors.New("error write on close")
	ErrInvalidArgument = errors.New("error argument invalid")
)

一些默认的全局变量

Functions

func LogFilePath

func LogFilePath(c *Config) (filepath string)

生成日志文件完整路径

Types

type AsynchronousWriter

type AsynchronousWriter struct {
	Writer
	// contains filtered or unexported fields
}

当WriterMode为async时使用的结构,同步writer,并发安全

func (*AsynchronousWriter) Close

func (w *AsynchronousWriter) Close() error

同步并发的Close接口实现

func (*AsynchronousWriter) Write

func (w *AsynchronousWriter) Write(b []byte) (int, error)

同步并发的Write接口实现

type BufferWriter

type BufferWriter struct {
	Writer
	// contains filtered or unexported fields
}

当WriterMode为buffer时使用的结构,异步write, 并发安全

func (BufferWriter) Close

func (w BufferWriter) Close() error

异步并发的Close接口实现

func (*BufferWriter) Write

func (w *BufferWriter) Write(b []byte) (int, error)

异步并发的Write接口实现

type Config

type Config struct {
	TimeTagFormat string `json:"time_tag_format" yaml:"timeTagFormat"` //时间格式化结构
	LogPath       string `json:"log_path" yaml:"logPath"`              // 日志路径
	FileName      string `json:"file_name" yaml:"fileName"`            // 日志文件名称
	MaxRemain     int    `json:"max_remain" yaml:"maxRemain"`          // 日志文件的最大存留数

	// 日志滚动策略,三个选项
	// 0:WithoutRolling:,不滚动
	// 1:TimeRolling,时间滚动策略,
	// 2:VolumeRolling,大小滚动策略
	RollingPolicy      int    `json:"rolling_policy" yaml:"rollingPolicy"`
	RollingTimePattern string `json:"rolling_time_pattern" yaml:"rollingTimePattern"` // 时间滚动策略时的cron表达式
	RollingVolumeSize  string `json:"rolling_volume_size" yaml:"rollingVolumeSize"`   // 大小滚动策略时的截断大小

	WriterMode            string `json:"writer_mode" yaml:"writerMode"`                 // none, lock, async, buffer
	BufferWriterThreshold int    `json:"buffer_threshold" yaml:"bufferWriterThreshold"` // 一部并发是缓存池的大小
	Compress              bool   `json:"compress" yaml:"compress"`                      // 是否压缩历史日志
}

func NewDefaultConfig

func NewDefaultConfig() Config

默认配置

type LockedWriter

type LockedWriter struct {
	Writer
	sync.Mutex
}

当WriterMode为lock时使用的结构,lock保护的writer: 提供由mutex保护的并发安全保障

func (*LockedWriter) Close

func (w *LockedWriter) Close() error

使用lock的Close接口实现

func (*LockedWriter) Write

func (w *LockedWriter) Write(b []byte) (n int, err error)

使用lock的Write接口实现

type Manager

type Manager interface {
	Fire() chan string
	Close()
}

func NewManager

func NewManager(c *Config) (Manager, error)

type Option

type Option func(*Config)

配置构造函数,用于更新配置

func WithAsynchronous

func WithAsynchronous() Option

改为async模式

func WithBuffer

func WithBuffer() Option

改为buffer模式

func WithBufferThreshold

func WithBufferThreshold(n int) Option

修改异步并发时的缓存池大小

func WithCompress

func WithCompress() Option

开启压缩历史日志文件

func WithFileName

func WithFileName(name string) Option

更新日志文件名称

func WithLock

func WithLock() Option

改为lock模式

func WithLogPath

func WithLogPath(path string) Option

更新日志文件目录

func WithMaxRemain

func WithMaxRemain(max int) Option

更新历史文件保存数

func WithRollingTimePattern

func WithRollingTimePattern(pattern string) Option

设置为按时间滚动模式,更新滚动时间表达式

func WithRollingVolumeSize

func WithRollingVolumeSize(size string) Option

设置为按大小滚动模式,更新滚动时截断的最大值

func WithTimeTagFormat

func WithTimeTagFormat(format string) Option

更新时间格式

func WithoutRollingPolicy

func WithoutRollingPolicy() Option

设置为不滚动模式

type RollingWriter

type RollingWriter interface {
	io.Writer
	Close() error
}

func NewWriter

func NewWriter(ops ...Option) (RollingWriter, error)

执行各个构造函数更新配置后生产RollingWriter

func NewWriterFromConfig

func NewWriterFromConfig(c *Config) (RollingWriter, error)

根据配置生成RollingWriter,用于接收日志输入

func NewWriterFromConfigFile

func NewWriterFromConfigFile(path string, typ string) (RollingWriter, error)

从配置文件读取配置,解析后生成RollingWriter,支持json和yaml类型

type Writer

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

当WriterMode为none时使用的结构,无保护的writer: 不提供并发安全保障

func (*Writer) Close

func (w *Writer) Close() error

没有lock的Close接口实现,借助atomic实现原子性操作

func (*Writer) CompressFile

func (w *Writer) CompressFile(oldfile *os.File, cmpname string) error

压缩历史文件

func (*Writer) DoRemove

func (w *Writer) DoRemove()

删除过期的历史日志文件

func (*Writer) Reopen

func (w *Writer) Reopen(file string) error

执行日志滚动, file为生成的历史文件名称

func (*Writer) Write

func (w *Writer) Write(b []byte) (int, error)

没有lock的Write接口实现

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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